Before learning a process, you must first understand the execution environment of the process.
Main Function The process is always executed from the main function. During programming, the program runs from the main function. Its prototype is as follows:
Int main (INT argc, char * [] argv );
Argc is the number of command line parameters. argv refers to a town array, that is, a pointer to a pointer. You can write code to test it:
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Int I;
For (I = 0; I <argc; I ++)
Printf ("argv [% d]: % s \ n", I, argv [I]);
Exit (0 );
}
Output:
Linux:/home #./test this is a test program
Argv [0]:./test
Argv [1]: This
Argv [2]: Is
Argv [3]:
Argv [4]: Test
Argv [5]: Program
Before calling the main function, the kernel first calls a special startup routine, which obtains the value of command line parameters and environment variables from the kernel, the executable program file specifies this startup routine as the starting address of the program. The kernel uses the exec function to start the C program.
Process Termination There are 5 ways to terminate the process
(1) normal termination
(A) return from the main function
(B) Call exit
(C) Call _ exit
(2) exceptional termination
(A) call abort
(B) terminated by a signal
In the above normal termination, there are exit and _ exit, the difference is: _ Exit immediately enters the kernel after calling; while exit will first execute some cleanup work (including calling and executing various termination handlers, close all standard I/O streams, etc.), and then enter the kernel. They are defined as follows:
# Include <stdlib. h>
Void exit (INT status );
# Include <unistd. h>
Void _ exit (INT status );
Different header files are used, because exit is described by right ansi c, while _ exit is described by posix.1.
Atexit Function
We can register some functions (up to 32) in the process. These functions are called by exit. Such a function is called the exit handler, And the atexit function is used for registration. This looks a bit like a class destructor.
# Include <stdlib. h>
Int atexit (void (* func) (void ));
The parameter is a function address. The exit Call sequence is the opposite of the registration sequence. The same function can be registered multiple times before it is called (such as the stack.
Environment table All processes run in an environment. The environment information is stored in the Environment table. An environment table is an array of character pointers. Each pointer contains an address of a string ending with null. The global variable environ contains the address of the pointer array:
Extern char ** environ;
Program storage space layout The C program consists of the following parts:
Body segment: it is also called a code segment. It is the machine instruction executed by the CPU. Body segments can be shared and read-only.
Initialize a data segment, also known as a data segment, which contains the initialized global and static variables in the program.
Non-initialized Data Segment: Also called BSS segment, which contains uninitialized global variables and static variables. Before the program is executed, the kernel is initialized to 0. You can reduce the code size by not storing the initial values in the code.
STACK: automatically stores variables and the information to be saved for each function call. Every time a function is called, the return address and the variables in the register are put in the stack. The new function automatically allocates storage space for it and temporary variables on the stack.
Heap: variable for dynamic allocation. Manual release is required. The heap is located between the top and bottom of the uninitialized data segment.
Memory Allocation: Refer to here
Setjmp and longjmp In C, you can use the Goto language (although not recommended), but the GOTO statement cannot skip the function. Setjmp and longjmp when executing the jump function. These two functions are very useful for handling errors that occur in deep nested calls. Only functions of the previous layer can be returned when a recursive call is returned. These two functions can be called out of the recursive call.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.