Call a special startup routine before main. The executable file specifies this startup routine as the starting address of the program, which takes command line arguments and environment variable values from the kernel, and then prepares it for the mian call.
4.1 Process Termination
There are eight ways to terminate a process. The first five are normal terminations, as follows
(1) Return from main, equivalent to calling exit()
(2) Call exit
(3) Call _exit and _exit
(4) The last thread returns from the startup routine
(5) Call pthread_exit from the last thread
The following three modes are abnormal termination modes
(1) Call abort
(2) Receive a signal
(3) The last thread breaks the cancellation request accordingly
Exit is usually called immediately after returning from main
exit(main(argc,argv));
Exit function
Void exit(int stat) performs some custom or kernel-defined functions first, then must perform a standard IO library clean close operation, all open streams call fclose note, Return main also calls exit. Return 0 == exit(0) void _Exit(int stat) void _Exit(int stat) Immediately enters the kernel without processingCopy the code
A process can register up to 32 functions, which will be called automatically from exit. These functions can be registered through the atexit function
Int atexit(void (*func)(void)) returns 0 on success. Otherwise, the non-zero argument is a function address, which is called in the opposite order from when it was registered.Copy the code
4.2 Cli Parameters and Environment Parameters
int main(int argc,char* argv[])
Argc starts at 0, argv[argc]==NULL
Argv [0]= the command to call, followed by the arguments
You can write
for(i=0; argv[i]! =NULL; i++)Copy the code
Similarly, the environment table is an array of character Pointers, where each pointer contains the address of a null-terminated C string, and the global variable environ contains the address of that array of Pointers
extern char** environ;
Each environment string is of type “PATH=:abcd\0”
Char * getenv(const char* name) returns a value pointer associated with name, NULL int putenv(const char* STR) returns 0, Otherwise return a non-zero string of the form "name=value\0" into the environment table, Int setenv(const char* name,const char* value,int rewrite) set name to value. Rewrite =1 rewrite=1 int unsetenv(const char* nameCopy the code