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 running, block

(2) If a child process is terminated, wait for the parent process to get its terminating state.

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


In the following instance, wait () is called in the parent process, and if the child process has not finished running, it calls itself into a blocking state.

After the child process has finished running, the child process's resources are recycled and run again.

#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.