This chapter is the basic preparation chapter of the process. The content of this chapter is based on C Programm as an example.
(i) the process begins:
kernel→ C start-up rountine →main function
Among the things C start-up Rountine do include: Get Command-Line arguments & Environment
There seems to be something else to say.
(ii) Completion of the process:
Five normal termination ways:
1. Return from main
2. Exit
3. _exit _exit
4. Return from the last pthread called from start routine
5. Call Pthread_exit in the last Pthread
Three kinds of abnormal termination way: (These three kinds of methods do not learn first)
1. Abort
2. Receipt of a signal
3. Response of the last pthread to a cancellation request
Execute "hook function" when the registration process exits: atexit
Implemented based on the stack structure, so functions registered with atexit are reverse order execution when the process exit (similar to the process cleanup function mentioned by Chapter11)
This part of the code is more intuitive, reading on the P202 Figure7.3 code can be.
apue English Version 3 201 page Figure7.2 is a relatively comprehensive diagram that illustrates the various paths and things to start and end the process :
In simple terms:
1. Return or exit: Call the various Exit handler and standard I/O cleanup before calling _exit or _exit
2. If the direct _exit or _exit is directly forced to quit the process, one step, will not call the various exit handler and cleanup and so on
(iii) command-line Arguments
There is no special explanation that the main passed in two parameters:
1. int argc: Number of parameters
2. Char *argv[]: parameters passed in as a string
Argv[0] is always the command itself, starting from argv[1] is really the parameter of the incoming program
(iv) Environment List
There was this in the parameters of main before, but later found that the use of this thing is too much, the status of the upgrade, gave him a special to engage in some built-in functions to operate
(v) Memory Layout of a C program
The focus is on the Figure7.6 of P206.
The address is from high to low in turn:
First part: Command-Line Arguments & Environment variables
Part Two: Stack (stack) (the stack frame that calls the function is here, and after the function returns, this part of the resource may be overwritten by other content instantaneously)
The third part: Stay White (stack can be down grow, occupy this piece of white, heap can grow up, occupy this block white)
Part IV: Heap (HEAPS) (provides dynamically allocated space: malloc; If you forget free, the memory leak)
Part V: Uninitialized data (global variable without initialization)
Part VI: Initialized data (initialized global variables)
Part VII: Text segment (including CPU executable machine instructions)
Of these, only six or seven of the two parts exist in program file, and five seem to be in the program file, in fact, is not, is set to 0 by kernel.
(vi) GKFX Libraries, (vii) Memory allocation to be used to see again
(eight) Environment Variables
Since the status of the environment variables has improved, there are three functions that can be changed to the environment variable:
1. putenv
2. setenv
3. unsetenv
Here are two points to note:
1. Is this simple environment Variables, why do you have such a few complex functions to set up, modify? Recall memory Layout of a C program, the upper block size is dead: if the EV is shorter than the original, longer than the original, or dynamic increase, you need to use the heap space. This involves some troublesome things. The details of the book are very well written.
2. Scope of the EV is set in the process: the EV set in the current process is valid only for the current process and its child processes, and does not affect other processes.
(ix) setjmp and longjmp
The code is in the book, very intuitive will not stick up.
Goto is a jump in the same function (within a stack frame); setjmp and longjmp are responsible for jumping between functions of a process (boast of several stack frames)
There are a few points to note:
1. setjmp set jump back to where, longjmp is responsible for jumping, with jmp_buf type of variables to coordinate the two, jump to where.
2. A setjmp can be longjmp back from different places
3. F1 () {setjmp; F2 () {F3 (longjmp;)}} If you jump from F3 to F1, the stack frame for F2 and F3 in the stack is not valid (i.e. not F2 and F3 are swapped and may be used quickly by others)
4. Or 3 in the example, if longjmp back to F1, then F1 in the state of the variable? Are they affected by the execution of F1 and F2, or are they saved in the previous state? The answer is not necessarily, especially the autmatic register variable, is not necessarily. The better way is to set the variable to volatile, so even if longjmp back, the variable is definitely affected by the F2 and F3 after the value, there will be no indeterminate state
(10) Getrlimit & Setrlimit
Involves the allocation of the resource allocated by the process, which is looked at later.
"Apue" Chapter7 Process Environment