The function prototype for wait () is: #include <sys/types.h> #include <sys/wait.h> pid_t wait (int *status)
Once the process has called wait, it blocks itself immediately, and the wait automatically analyzes whether a subprocess of the current process has exited. If you let it find a subprocess that has become a zombie, wait collects the information about the child process and then returns it after it has been completely destroyed, and if you do not find such a subprocess, it will always block here until one appears.
Parameter status is used to save some states when the collection process exits, which is a pointer to the type int. But if we don't care about how this subprocess died, just want to wipe out the zombie process (in fact, in most cases, we would think so), we can set this parameter to NULL, as follows: PID = Wait (NULL);
If successful, wait returns the process ID of the child process being collected, and if the calling process does not have a subprocess, the call fails, at which point the wait returns-1 while errno is placed as echild.
wifexited (status) This macro is used to indicate whether a subprocess is exiting normally, and if so, it returns a value other than 0.
Wexitstatus (status) When Wifexited returns a value other than 0, we can use this macro to extract the return value of the subprocess, and if the child process calls exit (5) to exit, Wexitstatus (status) returns 5 if the child process calls exit (7 ), Wexitstatus (status) returns 7. Note that if the process does not exit normally, that is, wifexited returns 0, the value is meaningless.
#include <stdlib.h> #include <stdio.h> #include <signal.h> #include <unistd.h> #include <sys /wait.h> void F () {printf ("This message is SENT by PARENT PROCESS ... \ n "); }
Main () {int i,childid,status=1,c=1; signal (sigusr1,f);//setup the signal value i=fork ();//better if it was:while (i=for k) ==-1); if (i) {printf ("Parent:this is the Parent ID = =%d\n", Getpid ()); sleep (3); printf ("parent:sending signal. \ n "); Kill (I,SIGUSR1); Send the Signal
The status is the ' return code ' of the child process Wait (&status); printf ("Parent is over.. Status = =%d\n ", status);
wifexited return Non-zero If exited normally printf ("parent:wifexited (status) =%d\n", wifexited (status));
Wexitstatus get the Return code printf ("Parent:the Return code wexitstatus (status) = =%d\n", Wexitstatus (status)); else {printf (' child:this is ' child ID = =%d\n ", Getpid ()); while (c<5) {sleep (1); printf (" Chlid TIMER:%d\n ", c); ++; printf ("Child are over". \ n "); Exit (2); } }
Output: Child:this is the child id = = 8158 parent:this is the Parent id = = 8157 chlid timer:1 chlid timer:2 Parent:sendin G Signal. This is SENT by PARENT PROCESS. Chlid timer:3 chlid timer:4 child are over. Parent is over.. Status = = parent:wifexited (Status) = = 1//Normal exit Parent:the return code wexitstatus (status) = = 2//Get child process returned value
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.