Process and signal process related system call

Source: Internet
Author: User

Process Call function Wait (), Waitpid ()

#include <sys/types.h><sys/wait.h>pid_t Wait (int *status)   // waits for all child processes to return the state of either terminating child process, blocking mode pid_t waitpid (pid_t pid,int *status,int  options) // specifies that the child process PID waits for termination to return, option setting is blocked

Status parameter

is empty, a child process that represents the end of any state, or a child process that represents the end of the specified state, if not NULL

Check the wait and Waitpid function to return the macro for the terminating state

  Wifexited/wexitstatus (status)

True if the status returned by the child process is terminated normally

  Wifsignaled/wstopsig (status)

True if the status returned by the child process is terminated for an exception (a signal that cannot be captured is not received)

Option parameter

Wnohang

If the child process specified by the PID does not exit, it returns immediately, then the waitpid is not blocked, at which time the return value is 0

wuntraced

If an implementation supports job control, any child process specified by the PID is paused and its state is returned from the suspend dependency has not been reported.

Process function Wait Labels

#include <stdlib.h>#include<stdio.h>#include<unistd.h>#include<string.h>#include<sys/types.h>#include<sys/wait.h>voidOut_status (intstatus) {    //to analyze what kind of exit a child process is by using a macro    if(wifexited (status)) {printf ("Normal exit:%d\n", Wexitstatus (status)); }    Else if(wifsignaled (status)) {printf ("Abnormal term:%d\n", Wtermsig (status)); }    Else if(wifstopped (status)) {printf ("stopped sig:%d\n", Wstopsig (status)); }    Else{printf ("Unknow sig\n"); }}intMainvoid){    intstatus;        pid_t pid; if((Pid=fork ()) <0) {printf ("Fork Error"); Exit (1); }    Else if(PID = =0)    {        //End Child Processprintf"pid:%d,ppid:%d\n", Getpid (), Getppid ()); Exit (3); }    //sets the wait for the parent process to block, waiting for the child process to be recycledWait (&status);    Out_status (status); printf ("---------------------------\ n"); if((Pid=fork ()) <0) {printf ("Fork Error"); Exit (1); }    Else if(PID = =0)    {        //End Child Processprintf"pid:%d,ppid:%d\n", Getpid (), Getppid ()); intI=3, j=0; intk=i/J; printf ("k:%d\n", K); }    //sets the wait for the parent process to block, waiting for the child process to be recycledWait (&status);    Out_status (status); printf ("---------------------------\ n"); if((Pid=fork ()) <0) {printf ("Fork Error"); Exit (1); }    Else if(PID = =0)    {        //End Child Processprintf"pid:%d,ppid:%d\n", Getpid (), Getppid ()); Pause ();//process paused, through signal end process    }    //sets the wait for the parent process to block, waiting for the child process to be recycledWait (&status);    Out_status (status); printf ("---------------------------\ n"); if((Pid=fork ()) <0) {printf ("Fork Error"); Exit (1); }    Else if(PID = =0)    {        //End Child Processprintf"pid:%d,ppid:%d\n", Getpid (), Getppid ()); //pause (), blocked by dead loop        intI=0;  while(++i) {Sleep (1); }    }    //sets the wait for the parent process to block, waiting for the child process to be recycledWait (&status);    Out_status (status); printf ("---------------------------\ n");}

Compile execution, and in another terminal kill child process, print out the child process end mode

2834 2833  3---------------------------283528338---------- -----------------283728339---------------------------  284128339---------------------------

I. Process-to-signal process-related system calls

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.