Network Programming (32) - linux four methods in the process of destruction of zombies

A, wait function


Function prototype:
pid_t the wait (int * Status);
Description:
the wait can recover any of the zombie process, as long as there is a zombie process system, called once wait, the recovery will be a zombie process.
Parameter Description:
  Status - When the end of the child process, it enters zombie state. Its operating system state change information is placed at a particular location in memory, and the status that the pointer is at a position, this part of the location data may be obtained by calling function wait.
After obtaining the data, you can obtain the current operating status of the system through a series of macros. Such as:
WIFEXITED (* Status) can determine whether the process ended normally (main function through return or the end of the exit), returns true if the end of normal, otherwise false.
WEXITSTATUS (* status) used to obtain the return value of the main function of when the process is completed normally, provided that returns true WIFEXITED (status).
Returns:
Returns the ending zombie process process ID.
For example:
int main ()
{
    pid_t PID;
    int Status;
    PID = the fork ();
    IF (PID == 0)
    {
        Exit (10);
    }
    the else
    {
        PID = the wait (& Status);
        if(WIFEXITED(status))
        {
            printf("process %d exited,return value = %d\n",pid,WEXITSTATUS(status));
        }
    }
    return 0;
}


Drawback:
wait () function is a blocking function, that is to say if the operating system is no zombie process, the program waits in wait () at until the zombie process.


Two, waitpid function


The function prototype:
pid_t waitpid (pid pid_t, Status int *, int Options);
Description:
waitpid () is used to recover a zombie, and wait () are different, it is non-blocking function can be specified by setting the pid parameters recovered zombie process process ID, you can also specify any zombie recovery process.
Parameters:
Status, and wait () functions are the same.
pid, specify the process ID of the process to be recycled, if set to -1, the recovery of any default a zombie process.
options, setup options, under normal circumstances is set to WNOHANG, said they did not clog.
Return value
if there zombies, returns the process is ended zombie process ID; return 0 if there zombie; If an error occurs, -1.
For example:
int main ()
{
    pid_t PID;
    int Status;
    PID = the fork ( );
    IF (PID == 0)
    {
        SLEEP (10);
        Exit (10);
    }
    the else
    {
        the while ((PID = waitpid (-1, & Status, the WNOHANG)) == 0)
        {
            puts("wating ...");
            sleep(2);
        }
        if(WIFEXITED(status))
        {
            printf("process %d exited,return value = %d\n",pid,WEXITSTATUS(status));
        }
    }
    return 0;
}


Cons:
Although waitpid () function instead of blocking, but need to use in a loop when using the code after the loop is only performed after completion of recovery zombies.


Three signal function


The function prototype:
sighandler_t Signal (int Signum, sighandler_t Handler);
Description:
System level api, the operating system and the signal processing function registration signal, when the operating system of the captured signal will call the registered signal handler.
Parameters:
Signum, the signal is registered to end the child process signal is SIGCHLD.
handler Handler, signals, function prototype generally in the form void handler (int sig), the parameters of the function into a signal operating system acquisition, the return value of the function is void
Return Value:
signal processing function processes the signal registered before returning function pointer.
For example:
void sig_handling (int SIG)
{
    int Status;
    pid_t PID;
    IF (SIG the SIGCHLD ==)
    {
        PID = waitpid (-1, & Status, the WNOHANG);
        IF (WIFEXITED (Status))
        {
            the printf ( "% D Process the Exited , return value D =% \ n-", PID, WEXITSTATUS (Status));
        }
    }
}
 
 
int main()
{
    pid_t pid;
    signal(SIGCHLD,sig_handling);
    pid=fork();
    if(pid==0)
    {
        exit(11);
    }
    else
    {
        sleep(10000);
    }
    return 0;
}


  Note:
  1, the signal processing function sig_handling, but also need to use waitpid () to recover zombie process, that is just a signal role in signal registration, which itself does not recover function zombie.
  2, once the signal with a signal registration and signal processing functions, once the signal occurs, the end of the process of sleep () state, that is in sleep (10000) of the parent process; will be interrupted, not really sleep 10,000 seconds.
 
Four sigaction function


Function Prototype:
int the sigaction (int Signum, const struct the sigaction ACT *,
                     struct oldact the sigaction *);
Description:
Signum, and the same signal, a signal indicating the need to register.
act, sigaction type of pointer, sigaction type is defined as follows:

struct the sigaction {
               void (* sa_handler) (int);
               (* to sa_sigaction) (int, the siginfo_t *, void *) void;
               sigset_t is sa_mask;
               int sa_flags;
               void (* sa_restorer) (void);
           };
  when we use only need to fill its sa_handler, sa_mask, sa_flags three members, sa_handler signal processing functions, sa_mask use sigemptyset () function blank, sa_flags assigned directly to zero.
  oldact, previously registered sigaction type of pointer.
Return Value:
  returns 0 on success, -1 failure
Example:
void sig_handling (int SIG)
{
    int Status;
    pid_t PID;
    IF (SIG the SIGCHLD ==)
    {
        PID = waitpid (-1, & Status, the WNOHANG);
        IF (WIFEXITED ( status))
        {
            printf("process %d exited,return value=%d\n",pid,WEXITSTATUS(status));
        }
    }
}
 
 
int main()
{
    pid_t pid;
    struct sigaction act;
    act.sa_handler=sig_handling;
    sigemptyset(&act.sa_mask);
    act.sa_flags=0;
    sigaction(SIGCHLD,&act,0);
    pid=fork();
    if(pid==0)
    {
        exit(11);
    }
    else
    {
        sleep(10000);
    }
    return 0;
}


  Compared with the signal, sigaction function, albeit cumbersome, but his strength lies in multiple versions of linux system compatibility, and some versions of linux is not supported by the signal function, and therefore a wider range of uses sigaction in actual use.

Github positions:
https://github.com/HymanLiuTS/NetDevelopment
cloning of the project:
the Git clone [email protected]: HymanLiuTS / NetDevelopment.git
obtaining the source code herein:
Git Checkout NL32
----------- ---------- 
author: HymanLiuTS 
source: CSDN 
original: https: //blog.csdn.net/hyman_c/article/details/53512999 
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/ai2000ai/article/details/94384638