Linux fork After wait gets the status example of the end of the child process

Source: Internet
Author: User

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.