Process waiting under Linux

Source: Internet
Author: User
Tags define function printf

  Wait (waits for child processes to break or end)

Table header file #include #include define function pid_t Wait (int * status); Function description: Wait () will temporarily stop the current Execution of the process until a signal arrives or the child process ends. If the child process has ended when the wait () is invoked, wait () returns the child process end state value immediately. The end state value of the subprocess is returned by the parameter status, and the process identifier for the child process is returned together. If you do not want to end the state value, the parameter status can be set to NULL. The end state value of the child process refer to Waitpid () returns the subprocess identifier (PID) if the execution succeeds, and returns the return value-1 if an error occurs. The reason for the failure exists in errno. pid_t Pid1; int status=0; I=wait (&status); I return the identification code of the subprocess; PID status is the end state of the child process, available wexitstatus (status) to get the status of the child process exit (3), then 3; Waitpid (wait for child process break or end) header file #include #include definition function pid_t waitpid (pid_t pid , int * status,int options); Function Description: Waitpid () Temporarily stops the execution of the current process until a signal arrives or the child process ends. If the child process has ended when the wait () is invoked, wait () returns the child process end state value immediately. The end state value of the subprocess is returned by the parameter status, and the process identifier for the child process is returned as soon as it is. If you do not want to end the state value, the parameter status can be set to NULL. Parameter PID is to wait for the child process identification code, the other numerical significance is as follows: Pid<-1 waits for the process group to recognize the code as the PID absolute value any child processes. Pid=-1 waits for any child process, equivalent to wait (). Pid=0 waits for the process group identifier to be any child process that is the same as the current process. Pid>0 waits for any child process to recognize the code as PID. The parameter option can be a combination of 0 or below: Wnohang returns immediately if there are no child processes that have ended, and does not wait. Wuntraced returns immediately if the child process enters a suspend execution, but the end state is ignored. The end state of the subprocess returns after the status, with several macros below to determine the end condition: wifexited (status) is a value other than 0 if the child process ends normally. Wexitstatus (status) Gets the end code returned by the subprocess exit (), typically using the wifexited to determine whether the macro will be used before it ends normally. wifsignaled (status) If the child process ends because of a signal, this macro value is true Wtermsig (status) to get the signal code aborted by the child process, the general will use the wifsignaled to judge before using this macro. wifstopped (status) This macro value is true if the child process is in a paused execution condition. This is generally the case only if you are using wuntraced. Wstopsig (status) obtains the signal code that raises the child process pause, usually uses wifstopped to judge before using this macro. Returns a subprocess identifier (PID) if the execution succeeds, and returns a return value of 1 if an error occurs. The reason for the failure exists in errno.

oot@wl-ms-7673:/home/wl/Desktop/c++# cat-n wait.cpp

1 #include

2 #include

3 #include

4 #include

5 #include

6 #include

7 #include

8

9/*

10 * Program Entrance

11 * * * * *

int main (void)

13 {

pid_t child;

15

16/* Create CHILD process * *

if ((Child=fork ()) ==-1)

18 {

printf ("Fork Error n");

Exit (1);

21}

Else

23 {

if (child==0)//child process

25 {

-printf ("The child process is Runn");

Sleep (1); Child process sleeps for one second but does not run the parent process

printf ("I am the Child:%dn", Getpid ());

Exit (0);

30}

The else//parent process

32 {

(NULL); The parent process will not run until the child process exits

printf ("The Father process is Runn");

printf ("I am The Father:%dn", Getpid ());

return 0;

37}

38}

39}

40

41

root@wl-ms-7673:/home/wl/Desktop/c++# g++ wait.cpp-o wait

root@wl-ms-7673:/home/wl/Desktop/c++#./wait

The child process is run

I am the child:19742

The father process is run

I am the father:19741

root@wl-ms-7673:/home/wl/Desktop/c++#./wait

In this example, the parent process waits for the child process to finish before executing. ~

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.