After using fork, you may need to get the health of the fork's process, such as there is no exception, crash.
The key description of wait in man is as follows:
All of these system calls is used to wait for state changes in a child of the calling process, and obtain information abo UT the child whose state has changed. A state change was considered to be:the child terminated; The child is stopped by a signal; Or the child is resumed by a signal.
Example code:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>intMainvoid) {pid_t pid;intStatusprintf("Before fork\n"); Fflush (stdout);if(PID = fork ()) <0) {printf("Fork error\n"); }Else if(PID = =0) {printf("After fork, child\n");//4 kinds of test conditions Exit(7);//-normal termination, Exitstatus = 7 //Abort ();///-abnormal termination, Signalstatus = 6 (SIGABRT) //INT i = 1/0;//-abnormal termination, Signalstatus = 8 (SIGFPE) //char *p = NULL; *p = ' a ';///-abnormal termination, signalstatus = one (SIGSEGV)} wait (&status);if(wifexited (status)) {printf("normal termination, Exitstatus =%d\n", Wexitstatus (status)); }Else if(wifsignaled (status)) {printf("abnormal termination, Signalstatus =%d\n", Wtermsig (status),#ifdef WcoredumpWcoredump (status)?"(core file generated)":"");#Else "");#endif }Else if(wifstopped (status)) {printf("Child stopped, signal number =%d\n", Wstopsig (status)); }printf("After fork, parent\n");return 0;}
Operating effect:
Wait status
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux fork After wait gets the status example of the end of the child process