Linux -- wait for the process to end wait () and waitpid ()

Source: Internet
Author: User
If the child process ends before the parent process, the parent process calls the wait () function and does not call the wait () function to produce two different results: -- if the parent process does not call wait () and waitpid () function, the sub-process will enter the zombie state. -- If the parent process calls the wait () and waitpid () functions...

 

If the child process ends before the parent process, the parent process calls the wait () function and does not call the wait () function to produce two different results:

-- If the parent process does not call the wait () and waitpid () functions, the child process will become frozen.

-- If the parent process calls the wait () and waitpid () functions, the child process will not be changed to a zombie process.

Why? Now let's take a deeper look at the wait () and waitpid () functions.

 

 

I. wait () and waitpid () learning

1. First, let's take a look at their function prototype:

Enter the command in the terminal: man 2 wait

You will see its function prototype:

NAME

Wait, waitpid, waitid-wait for process to change state

 

SYNOPSIS

# Include

# Include

Pid_t wait (int * status );

Pid_t waitpid (pid_t pid, int * status, int options );

Int waitid (idtype_t idtype, id_t id, siginfo_t * infop, int options );

 

We can see that the waitid () function is added in the 2.6 version.

 

2. wait () and waitpid () functions:

1> does the wait () function enable the parent process? And V onion school? Barium suppression? Are you sure you want? Probe ?? STEM? Coal ?? Mprayer and stool intrusion cover, yarn? Sputum? ID. the variable pointed to by the parameter status stores the exit code of the sub-process, that is, the value returned from the main function of the sub-process or the exit () function parameter in the sub-process. If status is not a null pointer, the status information will be written to the variable

Quantity.

2> The header file sys/wait. h defines the macro of the process exit status.

First, let's take a look at the official explanation.

A. WIFEXITED (status) r eturns true if the child terminated normally, that is, by calling exit (3) or _ exit (2), or by returning from main ().

Tiger-John translation:

If the sub-process ends normally, a non-zero value is returned. CALL exit (3), _ exit (3), or the value returned from the main () function.

 

B. WEXITSTATUS (status) returns the exit status of the child. this consists of the least significant 8 bits of the status argument that the child specified in a call to exit (3) or _ exit (2) or as the argument for a return statement in main (). this macro shoshould only be employed if WIFEXITED returned true.

Tiger-John translation:

WEXITSTATUS (status) if the macro WIFEXIED returns a non-zero value, it returns an 8-bit lower value of the exit or _ exit parameter in the child process.

 

C. w ifsignaled (status) returns true if the child process was terminated by a signal.

Tiger-John translation:

WIFSIGNALED (status) returns a non-zero value if the sub-process terminates abnormally.

 

D. WTERMSIG (status) returns the number of the signal that caused the child process to terminate. This macro shocould only be employed if WIFSIGNALED returned true.

Tiger-John translation:

WTERMSIG (status) if the macro WIFSIGNALED returns a non-zero return value, it returns the signal number that causes the child process to terminate abnormally.

 

E. WIFSTOPPED (status) returns true if the child process was stopped by delivery of a signal; this is only possible if the call was done using WUN? TRACED or when the child is being traced (see ptrace (2 )).

Tiger-John translation:

WIFSTOPPED (status) if the sub-process is abnormal? And #? Pray for the plaque, slide, and Slack? UN? This is only possible when TRACED or sub-process is tracked.

 

F. WSTOPSIG (status) returns the number of the signal which caused the child to stop. This macro shoshould only be employed if WIFSTOPPED returned true.

Tiger-John translation:

WSTOPSIG (status) if the macro WIFSTOPPED returns a non-zero value, the system returns the sub-process? Screwed up with 5 male-male malazines? /P>

 

G. WIFCONTINUED (status) (since Linux 2.6.10) returns true if the child process was

Resumed by delivery of SIGCONT.

Tiger-John translation:

WIFCONTINUED (status) (after version 2.6) if the child process recovers through SIGCONT, a non-zero

Value.

 

3> waitpid () function

(1) Let's take A look at A typical waitpid () example: after we download the installation program of software, at the end of the installation, it starts another rogue software installer B. When B is installed, it tells you that all installation is complete. A and B are in different processes. how does a start B and know that B has been installed? You can start B with fork in A, and then use waitpid () to wait for the end of B.

(2) waitpid () is also used to wait for the end of a sub-process, but it is used to wait for the end of a specific process. The pid parameter specifies the PID of the child process to wait. the status parameter has the same meaning as the status in the wait () function. The options parameter can be used to change the behavior of waitpid. If this parameter is assigned to WNOHANG, the parent process will be immediately returned if it is not suspended.

.

(3) value of the pid parameter in the waitpid () function

Let's take a look at the official explanation:

The value of pid can be:

 

<-1 meaning wait for any child process whose process group ID is

Equal to the absolute value of pid.

 

-1 meaning wait for any child process.

 

0 meaning wait for any child process whose process group ID is

Equal to that of the calling process.

 

> 0 meaning wait for the child whose process ID is equal to

Value of pid.

Tiger-John translation:

The pid value can be in the following conditions:

<-1 waits for any sub-process whose group ID is equal to the absolute value of the pid.

=-1 wait for any sub-process

= 0 any process that waits for the group ID to be equal to the group ID of the called process

> 0: wait for the child process whose process ID is pid to exit

(4) an application of the waitpid () function:

If you want the parent process to periodically check whether a specific sub-process has exited, you can use the following method:

Waitpid (child_pid, (int *) 0, WNOHANG );

If the child process has not exited, it returns 0; if the child process has ended, it returns child_pid. -1 is returned if the call fails. The cause of the failure includes the absence of the sub-process and invalid parameters.

 

3. differences between wait () and waitpid () functions

(1). before a sub-process is terminated, wait () blocks its callers, while waitpid () has an option that prevents callers from blocking.

(2). waitpid () does not wait for the first child process after its call. it has several options to control the process it is waiting.

(3 ). for wait (), the only error is that there are no sub-processes in the calling process; for waitpid (), if the specified process or process Group does not exist, or the process specified by the pid parameter may not be a sub-process that calls the process.

(4 ). waitpid () provides three functions not available in wait (): waitpid () can wait for a specific process; waitpid () provides a wait () the non-blocking version of (sometimes the status of a sub-process to be retrieved, but does not want to block the parent process, waitpid () provides the following options: WNOHANG, it can enable non-blocking by callers); third, waitpid () supports job control.

(5) The wait (& status) function is equal to waitpid (-1, & status, 0 );

 

Function instance: the state of a child process that you want to fetch sometimes, but you do not want to block the parent process. waitpid () provides the option WNOHANG, which can prevent the caller from blocking.

 

Do {

Pr = waitpid (pc, NULL, WNOHANG );

 

If (pr = 0 ){

Rintf ("No child exited \ n ");

Sleep (1 );

}

} While (pr = 0 );

 

If (pr = pc)

Printf ("successfully get child % d \ n", pr );

Else

Printf ("some error occured \ n ");

 

 

Summary of Tiger-John

The kernel sends a SIGCHLD signal to its parent process regardless of whether the process is terminated normally. when the wait or waitpid function is called

(A) if all sub-processes are running, the parent process can be blocked.

(B) if the sub-process is terminated, wait immediately returns the sub-process termination status.

(C) if no sub-process is running, an error is returned immediately.

 

4. function implementation:

Function example 1. (first look at a simple instance to see how the process executes after calling the wait () function ?)

1 # include

2 # include

3 # include

4 # include

5 # include

6

7 int main ()

8 {

9 pid_t child;

10 int I;

11 child = fork ();

12 if (child <0 ){

13 printf ("create failed! \ N ");

14 exit (1 );

15}

16 else if (0 = child ){

17 printf ("this is the child process pid = % d \ n", getpid ());

18 for (I = 0; I <5; I ++ ){

19 printf ("this is the child process print % d! \ N ", I + 1 );

20}

21 printf ("the child end \ n ");

22}

23 else {

24 printf ("this is the father process, ppid = % d \ n", getppid ());

25 printf ("father wait the child end \ n ");

26 wait (& child );

27 printf ("father end \ n ");

28}

29

30

31}

Function compilation:

Think @ ubuntu :~ /Work/process_thread/wait $ gcc wait. c-o wait

Think @ ubuntu :~ /Work/process_thread/wait $./wait

Function execution result:

This is the father process, ppid = 3303

Father wait the child end

This is the child process pid = 3356

This is the child process print 1!

This is the child process print 2!

This is the child process print 3!

This is the child process print 4!

This is the child process print 5!

The child end

Father end

 

Tiger-John Note:

From the above program, we can have a deep understanding of the wait () function execution process:

When the parent process calls the wait () function, it is suspended and waits until the child process ends.

 

Function Example 2 (now we are using an instance to gain a deeper understanding of the wait () function execution process)

 

1 # include

2 # include

3 # include

4 # include

5 # include

6 int main ()

7 {

8 pid_t pid;

9 char * msg;

10 int I;

11 int exit_code;

12

13 printf ("tiger study how to get exit code \ n ");

14 pid = fork ();

15 if (0 = pid ){

16 msg = "child process is running ";

17 I = 5;

18 exit_code = 37;

19}

20 else if (pid> 0 ){

21 exit_code = 0;

22}

23 else {

24 perror ("process creation failed \ n ");

25 exit (1 );

26}

27 if (pid> 0 ){

28

29 int status;

30 pid_t child_pid;

31

32 child_pid = wait (& status );

33

34 printf ("child process has exited, pid = % d \ n", child_pid );

35 if (WIFEXITED (status )){

36 printf ("child exited with code % d \ n", WEXITSTATUS (status ));

37}

38 else {

39 printf ("child exited abnormally \ n ");

40}

41}

42 else {

43 while (I --> 0 ){

44 puts (msg );

45 sleep (1 );

46}

47}

48}

After the function is compiled:

Think @ ubuntu :~ /Work/process_thread/wait $ gcc wait1.c-o wait1

Think @ ubuntu :~ /Work/process_thread/wait $./wait1

Function execution result:

Tiger study how to get exit code

Child process is running

Child process is running

Child process is running

Child process is running

Child process is running

Child process has exited, pid = 3816

Child exited with code 0

 

Tiger-John Note:

After the parent process calls the wait () function, it is suspended (we can open another terminal and enter the command ps aux to see that the execution result of the parent process is S) until the child process ends. After the child process ends, the wait () function returns the pid of the child process that has just ended. the macro WEXITSTATUS gets the exit code of the child process.

Related Article

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.