Linux process (1) – the environment in which the process runs
tags (space delimited): Linux
The following is from the advanced programming of the UNIX environment reading notes
Pre-cited
First think about the following questions can be answered:
- How is the main function called when the program is executed?
- What is the layout of the storage space in which the program is stored?
- How are command-line arguments passed to the new program? How does the process read environment variables?
- Use of process heap space
- How the process is terminated
A process is the basic process of execution, which is the activity body of the program execution. The following are some of the environments in which the process runs on the system.
First, main function
int main(int argc, char *argv[]);
When executing a C program, a special start routine is called before the main function is called. Executable program file The secondary startup routine (start Routin) is specified as the start address of the program (this is a linked procedure setting). The boot routine goes to the kernel to take command-line arguments and environment variable values, and then initializes some environments before calling the main function.
Interested in the startup routines, you can refer to the following links to see:
How does Linux execute my main ()?
Main function and start routine
II. termination of the process
The book says there are 8 scenarios in which the process terminates:
Normal termination in 5:
- return from main;
- Call exit;
- Call _exit or _exit;
- The last thread returns from its startup routine;
- Call Pthread_exit from the last thread;
3 Kinds of Abnormal termination:
- Call Abort
- Call a signal
- The last thread responds to a cancellation request
1. Exit function
#include <stdlib.h>voidexit(int status);void _Exit(int status);#include <unistd.h>void _exit(int status);
The first two are ISO C, and the latter belongs to the POSIX
_exit = _exit Function Same
The difference between exit and _exit is that _exit will return directly to the kernel, and exit will do some cleanup first.
The starting routine for C code can be this:
exit(main(int argc, char* argv[]))
1.1 Exit
The book says that for historical reasons, the Exit function always performs a cleanup of the standard IO library: Calling the Fclose function on all open streams, which causes the output buffered data to be flushed (flushed: write to file)
Three functions have a status parameter value, which is the state value of the process termination.
Remember when you first learned that there is a main function as follows:
void main(void)
It is equivalent to the end of the time just return, no return value, to exit the status is empty.
return (0) = exit (0)
One point is that when the main function has no return value, then his return value is random and undefined.
The main function is declared to return int, which is the POSIX and ISO definitions.
1.2 atexit
Process Registration function, ISO Specifies that a process can register up to 32 functions, which have exit auto-invocation, which are terminating handler functions .
#include <stdlib.h>int atexit(void (*func)(void)); //若成功,返回0;若出错,返回非0
parameter is a function address.
The order in which exit calls these functions is the opposite of the Order of registration (the estimate is placed on the stack when it is registered). The same function can be called multiple times if it is registered multiple times.
All of the above can be explained in the following diagram: C program startup and termination
Note that the only way the kernel makes the program execute is to call an EXEC function to invoke the program. The only way for a process to terminate voluntarily is to either explicitly or implicitly (by calling Exit) call _exit or _exit. The process can also be involuntary to have a signal to make it end.
III. environment of the Process 3.1 command-line arguments
A process that calls exec to execute a program can pass command-line arguments to the new program.
The parameters are stored in a single parameter table, similar to the environment table, as follows.
3.2 Environment tables and environment variables
Each program accepts an environment table, which is an array of character pointers, just the first pointer to a string that stores environment variables. The global variable environ contains the address of the array of pointers:
extren char **environ;
The structure of the environment table is as follows:
Environment variables in Linux and other systems belong to something that must be known.
The environment variable string form is in key-value form. Linux environment variable setting and viewing of the underlying function:
#include <stdlib.h>char *getenv(constchar *name)//成功,返回name关联的value指针;没有找到的,返回NULLint putenv(char *str)//成功返回0 失败返回非0int setenv(constcharconstchar *valueint rewrite)int unsetenv(constchar *name)
Here are some simple functions:
- Getenv reads the value of an environment variable, provided that the environment variable exists
- Putenv takes the form of a name=value string, places it in the environment table, and if name already exists, first deletes the original value
- Setenv sets the value of name to value. The last parameter, rewrite, is to control when the name is present, rewrite!=0 its existing definition. Rewrite==0, it does not delete its existing definition (name is not set to the new value, nor does it give an error.) )
- Unsetenv Delete the definition of name. Even if there is no such definition is not wrong.
Four, c program storage space layout
This is a classic storage model.
- Body segment: The part that stores the CPU execution instructions, the body segment is shared, so that those programs that are executed frequently only need to have a copy in memory. The general body segment is read-only.
- Initialize the data segment: Global variables and static variables, and the initialized general store is here.
- Uninitialized data segment: BSS field, meaning "block starting with symbol". Generally in the target file (in the XXX.O file, he does not occupy space), before the program executes, the kernel initializes the data in this segment to 0 or null empty pointers.
- Stacks: Local variables, automatic variables. The field data to be saved for each function call (where the program executes and the stack frame return position) are stored in the stack. A function calls a stack frame.
- Heap: Dynamically allocated blocks of data.
A classic arrangement of these segments is shown in the figure.
The unused virtual space between the top of the stack and the top of the heap is large.
A.out also contains several other types of segments, such as segments containing symbol tables, segments containing debugging information, and sections of dynamically shared library link tables ("Self-cultivation of programmers" is very clear). However, these sections are not loaded into the program image that the process executes.
The figure shows:
- command-line arguments and environment variables are stored in the space above the top of the stack.
- The uninitialized data BSS segment is not stored on the disk, and the kernel is assigned when it is run.
- The program file stores the body execution code and initialization data.
V. Shared Library
A shared library eliminates the need for a common library function in an executable file, and simply saves a copy of the library routine in a store that all processes can reference. This can greatly reduce the length of each execution file.
Another advantage of the shared library is that the new version of the library replaces the older version of the program that does not need to use the library to reconnect the edits (as long as the interface is not changed).
Six, the allocation of dynamic storage space
Storage space Dynamic allocation function:
#include <stdlib.h>void *malloc(size_t size);void *calloc(size_t nobj, size_t size);void *realloc(void *ptr, size_t newsize);//函数返回值:成功返回非空指针,出错返回NULLvoidfree(void *ptr)
These actions are also the most error-prone and difficult to find after an error.
- Cross-border operations: crossing operations means that it is possible to change the data stored in the next heap or change the management information in front of each heap storage, which are fatal errors and are difficult to find.
- Frees a block that has been freed.
- Memory leaks. Invisible memory leaks are dangerous.
The Alloca function is useful in some cases when it requests storage space in the stack.
The above basic is the process running environment. Store, variable, code, address.
Linux process (1)--the environment in which the process runs