Process Control (ii)

Source: Internet
Author: User

5 Wait and Waitpid functions

When a process is normal or abnormally terminated, the kernel sends a SIGCHLD signal to its parent process.

5.1 Processes that call wait or waitpid may:

Blocking (if its child processes are still running)

The termination status of the tape process returns immediately

Error returns immediately

The difference between 5.2wait and waitpid:

Before a child process terminates, wait causes its callers to block, and Waitpid has a selection that allows the caller to not block.

Waitpid does not wait for the first terminating child process-it has several choices that can control the process it waits for.

code example:

#include "ourhdr.h" #include <sys/wait.h>void pr_exit (int status) {if (wifexited (status)) printf ("Normal termination, exit status =%d\n ", Wexitstatus (status)), else if (wifsignaled status) printf (" abnormal termination, Signal Number =%d%s\n ", Wtermsig (status), #ifdefWCOREDUMPWCOREDUMP (status)? "(core file generated)": ""), #else ""), #endifelse if (wifstopped) printf ("Child stopped, signal number =%d\n", WST Opsig (status));} int main (void) {pid_tpid;intstatus;if (PID = fork ()) < 0) Err_sys ("fork Error"), else if (PID = = 0)/* Child */exit (7); (Wait (&status)! = PID)/* Wait for child */err_sys ("Wait Error");p r_exit (status);/* and print its status */if (PID = f Ork ()) < 0) Err_sys ("fork Error"), else if (PID = = 0)/* child */abort ();/* Generates SIGABRT */if (Wait (&status)! = P ID)/* Wait for child */err_sys ("Wait Error");p r_exit (status);/* and print its status */if ((PID = fork ()) < 0) Err_sys (" Fork Error "), else if (PID = = 0)/* Child */status/= 0;/* divide by 0 generateS SIGFPE */if (Wait (&status)! = PID)/* Wait for child */err_sys ("Wait Error");p r_exit (status);/* and print its status */exit (0);}

Printing results:

[[email protected] fork]# gcc wait.c wait.c:in function ' main ': Wait.c:48:warning:division by Zero[[email protected] for k]#./a.out normal termination, exit status = 7abnormal termination, signal number = 6abnormal termination, signal number = 8

5.3 If a process wants F o r k a child process, but does not require it to wait for the child process to terminate, nor want the child process to be in a zombie state until the parent process terminates, the trick to implement this requirement is to call F o r k two times.

code example:

#include "ourhdr.h" #include <sys/wait.h> #include <sys/types.h>intmain (void) {pid_tpid;if (PID = fork ())  < 0) {Err_sys ("fork Error"),} else if (PID = = 0) {/* first child */if (PID = fork ()) < 0) Err_sys ("fork Error"); else if (PID > 0) exit (0);/* Parent from second fork = = First child *//* * We ' re the second child; Our parent becomes init as soon * as we real parent calls exit () in the statement above. * Here's where we ' d continue executing, knowing that if * we ' re done, Init would reap our status. */sleep (2);p rintf ("second child, parent PID =%d\n", Getppid ()); exit (0);} if (Waitpid (PID, NULL, 0)! = PID)/* Wait for first child */err_sys ("Waitpid error");/* * We ' re the parent (the original PR ocess); We continue executing, * knowing that we don't have the parent of the second child. */exit (0);}

  

Printing results:

[Email protected] fork]#/a.out [[email protected] fork]# second child, parent PID = 1

  

6 Wait3 and WAIT4 functions

Process Control (ii)

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.