Process Environment process termination can be terminated in eight ways, of which 5 is normal, they are 1) return from main 2) call exi process environment process termination
There are eight ways to terminate the process, 5 of which are normal termination, they are
1) return from main
2) call exit
3) Call _ exit or _ Exit
4) The last thread returns from its startup routine
5) The last thread calls pthread_exit.
There are three methods to terminate an exception
6) call abort
7) receive a signal and terminate
8) The last thread makes a response to the cancellation request.
Exit function
# Include
Void exit (int status );
Void _ Exit (int status );
# Include
Void _ exit (int status );
All three functions have an integer parameter and become terminated.
The exit function always executes a standard I/O library cleaning and closing operation: to open the stream, call the fclose function, which causes the output data in the buffer zone to be flushed.
Exit (0) is equivalent to return (0 ).
Atexit function
According to iso c, a process can register up to 32 functions, which will be automatically called by exit. We call these functions to terminate the handler and call the atexit function to register these functions.
# Include
Int atexit (void (* function) (void); where, the atexit parameter is a function address. when you call this function, you do not need to transmit any parameters to it, it is not expected to return a value. The order in which exit calls these functions is different from the order in which they are registered by atexit. If the same function is registered multiple times, it will also be called multiple times. The following is a program using atexit. # Include "stdlib. h "# include" stdio. h "static void my_exit1 (void); static void my_exit2 (void); int main (void) {if (atexit (my_exit2 )! = 0) perror ("can't register my_exit2"); if (atexit (my_exit1 )! = 0) perror ("can't register my_exit1"); if (atexit (my_exit1 )! = 0) perror ("can't register my_exit1"); printf ("main is done \ n"); return (0);} static void my_exit1 (void) {printf ("first exit handler \ n");} static void my_exit2 (void) {printf ("second exit handler \ n ");}
The output result is as follows:
Main is done
First exit handler
First exit handler
Second exit handler
Command line parameters
The kernel is the only method for program execution to call the exec function. When executing a program, the exec process can pass command line parameters to the new program.
C program storage space layout
Body segment: this is the machine instruction part executed by the CPU. Generally, the body segments can be shared.
Initialize data segment: This segment is usually used as the data segment. Contains global or static variables explicitly assigned to the initial value in the program.
Non-initialized data segment: This segment is usually referred to as block startedby ymbol. it does not include global variables or static variables with initial values. Before the program is executed, the kernel initializes the data in this segment to 0 or a null pointer.
Stack: Automatic variables and the information to be saved for each function call are stored in this segment.
Heap: it is usually used for dynamic storage allocation in the heap.
Typical C program storage arrangements are as follows:
Use the size command to report the length of the body segment, data segment, and bbs segment.
Environment table and environment variable environment table
An environment table is received to every degree. An environment table is an array of character pointers. each pointer contains an address of a C string ending with null. The global variable environ contains the address of the pointer array:
Extern char ** environ
For example, if the environment contains five strings
Each string ends with a null character. We call environ an environment pointer and an array of pointers an environment Table. each pointer represents an environment string.
Environment variable
The environment string format is usually as follows:
Name = value
Iso c defines a function getenv to take the environment variable value:
# Include
Char * getenv (const char * name );
This function returns a pointer pointing to the value in the name = value string. If no value is found, NULL is returned.
# Include
Int putenv (char * string );
Int setenv (const char * name, const char * value, int overwrite );
Int unsetenv (const char * name );
Put the putenv string in the form of name = value in the environment table. If the name already exists, delete the original definition first.
Setenv sets name to value.
Unsetenv deletes the name definition.
Environment tables and environment strings are usually stored at the top of the space (above the stack)
Setjmp and longjmp
In C language, goto cannot span functions. setjmp and longjmp functions are used to execute such jump functions.
# Include
Int setjmp (jmp_buf env );
Void longjmp (jmp_buf env, int val );
You can use a program to check the different situations of automatic variables, global variables, register variables, static variables, and easy-to-lose variables:
#include
#include
static void f1(int, int, int, int);static void f2(void); static jmp_buf jmpbuffer;static int globval; int main(void){ int autoval; register int regival; volatile int volaval; static int statval; globval = 1; autoval = 2; regival = 3; volaval = 4; statval = 5; if (setjmp(jmpbuffer) != 0) { printf("after longjmp:\n"); printf("globval = %d, autoval = %d, regival = %d,"" volaval = %d, statval = %d\n", globval, autoval, regival, volaval, statval); exit(0); } /* * Change variables after setjmp, but before longjmp. */ globval = 95; autoval = 96; regival = 97; volaval = 98; statval = 99; f1(autoval, regival, volaval, statval); /* never returns */ exit(0);} static voidf1(int i, int j, int k, int l){ printf("in f1():\n"); printf("globval = %d, autoval = %d, regival = %d," " volaval = %d, statval = %d\n", globval, i, j, k, l); f2();} static voidf2(void){ longjmp(jmpbuffer, 1);}
The execution result is as follows:
In f1 ():
Globval = 95, autoval = 96, regival = 97, volaval = 98, statval = 99
After longjmp:
Globval = 95, autoval = 2, regival = 3, volaval = 98, statval = 99
It can be seen that the global variables, static variables, and loss-prone variables are not affected, and whether the automatic variables and register variables change is uncertain (although the restored setjmp value is shown here ).
Getrlimit and setrlimit functions
# Include
# Include
Int getrlimit (int resource, struct rlimit * rlim );
Int setrlimit (int resource, const structrlimit * rlim );
Each process has a set of resource restrictions, some of which can be queried and modified using the getrlimit and setrlimit functions. Process Resource restrictions are generally set up by process 0 during system initialization, and then inherited by each sorted process.