Linux process Practice (4)--wait Avoid zombie processes

Source: Internet
Author: User
Tags terminates

the background of wait

When the child process exits, the kernel sends a SIGCHLD signal to the parent process, and the exit of the child process is an asynchronous event (the child process can terminate at any time the parent process runs)

When a child process exits, the kernel resets the subprocess to zombie state, a process called a zombie process that retains only the smallest number of kernel data structures so that the parent process queries the exit status of the child process .

The parent process can query the exit state of the child process with the Wait/waitpid function

#include <sys/types.h> #include <sys/wait.h>pid_t wait (int *status);p id_t waitpid (pid_t pid, int *status, int options);
Wait
pid_t Wait (int *status);

When we start a process with fork, the child process has its own life and will run independently. Sometimes we need to know if a child process is over, so we can schedule the parent process to finish after the child process through wait.

Status: This parameter gives you the information to wait for the child process

return value:

On success, returns the process ID of the terminated child; On error,-1 is returned.

Characteristics:

A 1.wait system call causes the parent process to pause execution until any of its (not all) child processes have ended.

2. The PID of the child process is returned, which is usually the child process that has ended;

3. State information allows the parent process to determine the exit status of the child process, that is, the value returned from the main function of the child process or the exit code of the Exit/_exit statement in the child process.

4. If status is not a null pointer, the state information is written to the location it points to

Example int main () {    pid_t pid = fork ();    if (PID = =-1)        err_exit ("fork");    else if (PID = = 0)    {        cout << "in child, PID =" << getpid () << Endl;        Sleep (5);        Exit (Ten);    }    int status;    int returnpid = Wait (&status);    Two PID values are the same, but the status value is not at all    cout << "in Parent, Returnpid =" << returnpid         << ", status =" & lt;< status << Endl;}


Wait Get Status

Macro definition

Describe

wifexited (status)

Wexitstatus (status)

Returns a non-0 value if the child process ends normally

If wifexited nonzero, returns the child process exit code

wifsignaled (status)

Wtermsig (status)

Child process terminates due to capture signal, return non 0 value

If wifsignaled nonzero, return signal code

wifstopped (status)

Wstopsig (status)

Returns a non-0 value if the child process is paused

If wifstopped nonzero, returns a signal code

example void printstatus (int status) {if (wifexited (status)) {cout << "normal termination, exit status ="    << wexitstatus (status) << Endl; } else if (wifsignaled (status)) {cout << "abnormal termination, signal number =" << Wtermsig (St         atus); #ifdef wcoredump if (wcoredump (status)) cout << "(core file generated) << Endl; #else cout << Endl; #endif//Wcoredump} else if (wifstopped status) {cout << "child Stopp    ed, status number = "<< wstopsig (status) << Endl;    } else {cout << "Unknow stop!" << Endl;    }}//Test Code int main () {pid_t pid = fork ();    if (PID = =-1) err_exit ("fork");    else if (PID = = 0) exit (7);    int status;    if (wait (&status) = =-1) err_exit ("First wait error");    Printstatus (status);    PID = fork ();    if (PID = =-1) err_exit ("fork"); else if (PID = = 0) abort ();    if (wait (&status) = =-1) err_exit ("First wait error");    Printstatus (status);    PID = fork ();    if (PID = =-1) err_exit ("fork");    else if (PID = = 0) status/= 0;    if (wait (&status) = =-1) err_exit ("First wait error");    Printstatus (status); return 0;}

Viewing signal values


Waitpid

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

Wait for the end of a particular process

Parameters :

Pid:the value of PID can be:

<-1 meaning wait for any child process whose process group ID are equal to the A Bsolute value of PID.

-1 meaning wait for any child process (either subprocess).

0 meaning wait for any child process whose process group ID are equal to that of the CA Lling process (processes that are in the same group as the caller process).

>0 meaning wait for the child whose process ID was equal to the value of PID.

Status: If it is not empty, the state information is written to the location it points to (same as wait)

Options: One of the most useful options for allowing changes to waitpid behavior is Wnohang, which prevents waitpid from suspending the execution of the caller

return value:

If the ID of the waiting child process is returned successfully, the failure returns-1

The difference between wait and waitpid:

1. Before a child process terminates, wait causes its callers to block, and Waitpid has a selection that allows the caller to not block.

2.waitpid does not wait for the first terminated child process: It has several selections that control the specific process it waits for.

the 3.wait function is equivalent to a special case of the Waitpid function.

Waitpid ( -1, &status, 0);

Zombie Process (if not wait ...)

When a child process finishes running, the association between it and its parent process is also persisted until the parent process ends normally or the parent process calls wait to terminate.

The data item in the process table that represents the child process is not released immediately, and although it is no longer active, the child process remains in the system because its exit code needs to be saved for subsequent wait calls in the parent process. It will be called a "zombie process"

How to avoid zombie processes

Method 1: Call the wait or Waitpid function to query the child process exit status.

Method 2: If you do not want the parent process to hang, you can add a statement to the parent process: signal (sigchld,sig_ign), which indicates that the parent process ignores the SIGCHLD signal, which is sent to the parent process when the child process exits ( Indicates that the parent process ignores the SIGCHLD signal and everything is managed by the Linux kernel).


Attach -use man Handbook to reduce development difficulty and improve development skills

such as: Man 7 signal find out what signal can make the application pause


Linux process Practice (4)--wait Avoid zombie processes

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.