Today, I also read the Unix Process Environment. When the execution procedure is executed, its main function is called, how is the command line parameter transferred to the executive; what is the typical memory layout? Execution is very useful, be sure to write down.
Let's see if the main function is called, how to exit (I have always thought that Main is a process of a program, it is really confused. First look at the picture below:
The life and death of a process is like this.
The C program always begins with the main function. The prototype of the standard main function is:
INT Main (int Argc, char * argv []);
When the kernel starts the C program, use an EXEC function to call a special starter routine before calling Main. The executable program file specifies this starter program as the start address of the program - which is set by the Link program, while the LINK program is called by the C compiler (e.g., GCC). The starting routine acquires command line parameters from the kernel and enters the main function and starts the execution of the user program. After returning from the main function, (normally) The starting process is turned to the kernel. This process is a usual processing process. But not necessarily, for example, EXIT (Return) can be called in Main, which is more common way.
Note: EXIT and _EXIT in the above picture are normal termination processes. However, EXIT wants to handle a series of cleaning, such as calling executing each termination handler, turning off all standard I / O streams, _exit is directly returning the kernel.
Emphasize what is:
The only way to execute the program execution is to call an EXEC function. The only way process voluntarily terminated is explicitly or implicitly (calling exit) call_exit.
How is the command line argument to the user process?
This is relatively simple. When a program is executed, the process that calls Exec can pass the command line parameters to the program, such as the shell. ARGC is the number of parameters, and Argv is a pointer to the parameter group. Just pay attention to it, Argv [Argc] must be an empty pointer, which indicates the end.
The typical layout in the memory space:
TEXT: It is a machine instruction section executed by the CPU, usually read-only, can be shared. Shared is to make only frequently executed programs in memory only one copy, read-only is to prevent modification.
Initialization Data Section: Store the global variables already have initial values, static variables.
Not initialization data segment (BSS): Storage only declares without initialization, but before the program executes, the initial value is assigned by EXEC.
Heap: For dynamic memory allocation, its length is uncertain.
Stack: The environment information used for local variables and when the function calls switches, the length is uncertain.
It is also possible to see from the figure that the content of the unintroduce data segment is not stored in the disk program file. There is only a text segment and an initialization data segment that needs to be stored in the disk program file. That is, what we often say, declare that a variable does not assign space, only the variable will allocate space.
Use the size command to report the length, data segment, and the length of the BSS segment with the size command.
Exterior:
Last time I saw a company's interview questions: Can you perform some other operations after the main function is executed? Someone answered with Atexit, and checked MSDN at the time, the explanation of Atexit as follows:
INT ATEXIT (Void) (VOID));
Processes The Specified Function At Exit.
The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed To Atexit Cannot Take Parameters.example:
Void Fn1 (Void), FN2 (Void), FN3 (Void), FN4 (Void)
Void main (void)
{
Atexit (fn1);
Atexit (FN2);
Atexit (FN3);
Atexit (FN4);
Printf ("this is executed first./n);
}
Void fn1 ()
{
Printf ("Next./N");
}
Void fn2 ()
{
Printf ("executed");
}
Void fn3 ()
{
Printf ("IS");
}
Void fn4 ()
{
Printf ("this");
}
OUTPUT:
This is executed first.
This is executed next.
At that time, I saw MSDN and only accepted this function and didn't understand the dragon. Today, I understand the calling process of the C program, I understand the true principle, and the Atexit just registers some functions (these functions can be called "exit handler", which will be called before the termination process. Theatexit is supported by ANSI C, early Some systems are not necessarily supported.
OK, I am here today, rest.