Linux programming process (4): Wait/waitpid function and zombie Process

Source: Internet
Author: User

I. botnets

When a child process exits, the kernel sends a sigchld signal to the parent process. The exit of the child process is an asynchronous event (the child process can be terminated at any time when the parent process is running)
When a child process exits, the kernel changes the child process to a zombie state. This process is called a zombie process. It only retains the smallest kernel data structure, so that the parent process can query the exit state of the child process.
You can use the wait/waitpid function to query the exit status of a child process in the parent process.


Ii. How to Avoid zombie Processes

When a child process stops running, its association with its parent process will be maintained until the parent process ends running normally or the parent process stops when wait/waitpid is called.
Data items that represent sub-processes in the progress table are not immediately released. Although the Sub-processes are no longer active, they can stay in the system, because the exit code also needs to be saved for subsequent wait/waitpid calls in the parent process. It is called a "zombie process ".

Call the wait or waitpid function to query the exit status of the sub-process. The parent process of this method will be suspended (waitpid can be set not to be suspended ).
If you do not want the parent process to be suspended, you can add a statement in the parent process: Signal (sigchld, sig_ign); indicates that the parent process ignores the sigchld signal, this signal is sent to the parent process when the child process exits. You can also call wait/waitpid in the signal processing function without ignoring the sigchld signal.

In O & M, the common means is to kill the parent process, so that the process will be taken over by the INIT process, which will clean up the Sub-Process status.


Iii. Wait Functions

Header file <sys/types. h> and <sys/Wait. H>
Function: When we use fork to start a process, the sub-process has its own life and runs independently. Sometimes, we need to know whether a child process has ended. We can arrange the parent process after the child process ends through wait.
Function prototype
Pid_t wait (int * Status)
Function Parameters
Status: This parameter can be used to obtain information about the child process you are waiting.
Return Value:
Successfully wait for the sub-process function to return the ID of the waiting sub-process


A wait System Call will suspend the execution of the parent process until one of its child processes ends.
The PID of the child process is returned, which is usually the child process that has ended.
The status information allows the parent process to determine the exit status of the child process, that is, the exit code returned from the main function of the child process or the exit code of the exit statement in the child process.
If status is not a null pointer, the status information will be written to the position it points.


The following macro definition can be used to obtain the exit status of a sub-process.

Wifexited (Status)
If the sub-process ends normally, a non-zero value is returned.
Wexitstatus (Status) If wifexited is not zero, return the sub-process exit code
The wifsignaled (Status) sub-process is terminated due to signal capture, and a non-zero value is returned.
Wtermsig (Status) If wifsignaled is not zero, the signal code is returned.
If the sub-process is paused, a non-zero value is returned.
Wstopsig (Status) If wifstopped is not zero, a signal code is returned.


Iv. waitpid Function

Function: used to wait for the end of a specific process

Function prototype:
Pid_t waitpid (pid_t PID, int * status, int options)
Parameters:
Status: if it is not empty, the status information is written to the position it points.
Options: Allows changes to waitpid behavior. The most useful option is wnohang, which prevents waitpid from suspending the invocation of the caller.
Returned value: if the process is successful, the system returns the ID of the child process to be waited for. If the process fails, the system returns-1.


The interpretation of the p I d parameter of waitpid is related to its value:
PID =-1 wait for any sub-process. Waitpid is equivalent to wait in terms of this function.
PID> 0: Wait for the sub-process whose process I d is equal to p I D.
PID = 0 wait for the group I d to be equal to any sub-process of the group I d that calls the process. In other words, it is a process in the same group as the caller process.
PID <-1 waits for any sub-process whose group I d is equal to the absolute value of p I D.


V. Differences between wait and waitpid Functions

Both functions are used to wait for changes in the Process status, including normal exit, abnormal termination by the signal, paused by the signal, wake-up by the signal, and continue execution.

Before a sub-process is terminated, wait blocks its callers, while waitpid has a selection option to prevent callers from blocking.
Waitpid does not have to wait for the first child process to terminate-it has several options to control the specific process it is waiting.
In fact, the wait function is a special case of the waitpid function.


Example program:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*************************************** **********************************
> File name: Process _. c
> Author: Simba
> Mail: dameng34@163.com
> Created time: sat 23 Feb 2013 02:34:02 pm CST
**************************************** ********************************/
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <unistd. h>
# Include <fcntl. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/Wait. H>

# Define err_exit (m )\
Do {\
Perror (m );\
Exit (exit_failure );\
} While (0)

Int main (INT argc, char * argv [])
{
Pid_t PID;
PID = fork ();
If (pid =-1)
Err_exit ("fork error ");

If (pid = 0)
{
Sleep (3 );
Printf ("this is child \ n ");
// Exit (100 );
Abort ();
}

Printf ("this is parent \ n ");
Int status;
Int ret;
Ret = wait (& status); // blocking waits for the sub-process to exit
// Ret = waitpid (-1, & status, 0 );
// Ret = waitpid (PID, & status, 0 );
/* Waitpid can wait for a specific process, not just the first child process to exit
* You can set Option to wnohang, that is, wait without blocking */
Printf ("ret = % d, pid = % d \ n", RET, pid );
If (wifexited (Status ))
Printf ("Child exited normal exit status = % d \ n", wexitstatus (Status ));
Else if (wifsignaled (Status ))
Printf ("Child exited abnormal signal number = % d \ n", wtermsig (Status ));
Else if (wifstopped (Status ))
Printf ("child stopped signal number = % d \ n", wstopsig (Status ));

Return 0;
}

Output:

Simba @ Ubuntu :~ /Documents/code/linux_programming/apue/process $./Wait
This is parent
This is child
Ret = 7156, pid = 7156
Child exited abnormal signal number = 6

It indicates that the sub-process is terminated abnormally because we call abort (), that is, the SIGABRT signal is generated to terminate the sub-process. The signal number is 6. If we do not use abort but exit (100), we should output child exited normal exit status = 100, that is, exit normally.


Reference: apue

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.