Lien000034
2014-10-02
1. Process Termination Method
There are eight methods to terminate a process, five of which are normal termination. They are
1. Return from main.
2. Call exit.
3. Call _ exit or _ exit.
4. The last thread is returned from its startup routine.
5. The last thread calls pthread_exit.
The other three methods are exception termination methods. They are
1. call abort.
2. receive a signal and terminate it.
3. The last thread responds to the cancellation request.
2 exit function
There are three functions used to terminate a program normally: _ exit and _ exit enter the kernel immediately, and exit executes some cleanup operations (including calling and executing each terminate processing program, close all standard I/O streams, etc.) and then enter the kernel.
# Include <stdlib. h>
Void exit (INT status );
Void _ exit (INT status );
# Include <unistd. h>
Void _ exit (INT status );
All three exit functions contain an integer parameter, which is called the termination status (or exit status ). The main function returns an integer value equivalent to calling exit with this value, that is, exit (0) is equivalent to return (0 ).
If (a) Main executes a return statement without a return value, or (B) Main does not declare that the return type is integer, the termination status of the process is undefined. If the return type of the main function is integer and the return value is invisible when the main statement is executed to the last statement (return is not displayed or the exit function is called), the termination status of the previous version is undefined, the ISOC standard 1999 sets that the Process Termination status to 0.
3. Terminate the processing program
The previous section describes calling the exit function. before entering the kernel, the process will call some exit handler programs ). We can call the atexit function to register and terminate the handler. According to iso c, a process can register up to 32 Termination handler functions.
# Include <stdlib. h>
Int atexit (void (* func) (void ));
Return Value: if the request succeeds, 0 is returned. If the request fails, a non-0 value is returned.
The atexit parameter is a function address, and the registered function has no parameters or return values. The order in which exit calls these functions is different from the order in which they are registered. If a function is registered multiple times, it is also called multiple times.
Example:
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <errno.h>static void my_exit1(void);static void my_exit2(void);intmain(void){ if (atexit(my_exit2) != 0) { printf("can‘t register my_exit2: %s\n", strerror(errno)); } if (atexit(my_exit1) != 0) { printf("can‘t register my_exit1: %s\n", strerror(errno)); } if (atexit(my_exit1) != 0) { printf("can‘t register my_exit1: %s\n", strerror(errno)); } printf("main is done\n"); exit(0);}static voidmy_exit1(void){ printf("first exit handler\n");}static voidmy_exit2(void){ printf("second exit handler\n");}
Compile the program, generate the atexitdemo file, and then execute the file,
lienhua34:demo$ gcc -o atexitdemo atexitdemo.clienhua34:demo$ ./atexitdemomain is donefirst exit handlerfirst exit handlersecond exit handler
(Done)
Unix programming learning notes (15) -- Process Termination in Process Management