UNIX environment Advanced Programming----Process Control wait ()

Source: Internet
Author: User

One, wait () function

When you call the Wait () function in a process

(1) If all of its subroutines are still being executed, the blockage

(2) Assume that a child process has been terminated. Waits for the parent process to get its terminating state.

(3) If there are no child processes, an error is returned.


In the following instance. Call Wait () in the parent process, assuming that the child process has not completed yet, and then calls itself into the blocked state.

Wait for the child process to finish executing, after the child process's resources are reclaimed and then executed by itself.

#include <stdio.h> #include <unistd.h> #include <wait.h> #include <stdlib.h>int main () {   int i=0;   int j=0;   int status;   int count =0;   int a = fork ();   if (a>0)      {        printf ("This is parent, PID =%d\n", Getpid ());        for (i= 0;i<=10;i++)        {            printf ("Parent is%d\n", i);            Sleep (1);            Wait (&status);        }        Wait (&status);      }   else     {           for (j=0;j<=10;j++)           {              printf ("%d\n", j);              Sleep (1);           }     } return 0;}



UNIX environment Advanced Programming----Process Control wait ()

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.