Linux operating process (exit process and destruction process)

Quit Process

In Linux, there are eight kinds of methods exit process, including five kinds of normal and a method of three kinds of anomalies.

In general, Linux application code calls the exit function to exit a series of process.

#include<stdlib.h>
#include<unist.h>
void exit(int status);
void _exit(int status);
void _Exit(int status);

Series exit function does not return a value, using a final state integer variables as parameters, Linux kernel will check the final state: When an abnormal termination, Linux kernel will directly generate a stop status word, describes the reason for the abnormal termination can be obtained by termination wait status word or waitpid function; parent process may be obtained by examining the state of the sub process termination status.

If the following three states:

  • When calling exit function without a series termination status.
  • The main function performed without a return value return.
  • The main function return value is not an integer.

Linux will believe that ending the state of the process is undefined.
If the main function returns an integer value is defined as the main function is to perform and to return to the last statement, the termination status of the process is 0.

Call return statement in the main function returns, the vast majority of the series is equivalent to calling exit function.

These two functions call the procedure in Figure
Here Insert Picture Description

  • _exit function: direct the process stops running, clear the memory space it uses, remove its various data structures in the kernel.
  • exit function: Based on the _exit function to do some packing to quit before executing a number of processes.

The biggest difference between these two functions is: the former open before calling to check the case file, the contents of the file buffer write back to the file; the latter direct the process stops running, clear the memory space it uses, its destruction in the kernel the various data structures, i.e., the figure "clean-up I / O buffer" a.

The destruction process

When a series of processes using exit function exits, will remain in the memory part of the data for the parent query, but also generates a status word is terminated, then the Linux kernel will send a SIGCHLD signal notifies the parent process, because the child process to end the parent process is asynchronous, so the SIGCHLD signal to the parent process is asynchronous, the parent can not respond.

For the default state of the parent process child process after the withdrawal is not treated, but this will lead to zombie processes in the system a waste of system resources at this time should call wait or waitpid function is a function of these zombie process for processing.

There may be the following three cases after a call to wait or waitpid function:

  • If all child processes the parent process are still running, blocking their parent process to wait for child processes run end. .
  • If there is a child process has ended, the parent process has made the final state of the child process and returns immediately.
  • If the parent process is not any child process, then immediately return an error.

Call format standard wait and waitpid function is as follows:

#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
1. wait function
  • If the wait function is successful identifier of the child process is returned, or -1 if it fails.
  • Wherein the status parameter is a pointer to an integer, the state can be stored for terminating the child process may be defined as a null pointer.
  • Different functions wait and waitpid function in a child process to terminate before, wait function allows the parent process to block waiting for the child process exits, and waitpid there is a parameter that allows the parent process is not blocked, and multiple children in a parent's case next, if there was a child process exits the process of the child process is returned identifier.

terminating wait state function returns a macro

Macros Explanation
WIFEXITED(status) When the child process ends normally return true
WIFSIGNALED(status) When failed the child returns true
WEXITSTATUS(status) When WIFEXITED (status) is true when called, it returns a status word are 8
WTERMSIG(status) When WIFSIGNALED (status) is true when the call is terminated due to the return signal codes
2. waitpid function

When using the wait function, if any parent of a child process returns to the wait function returns, and waitpid function may be specified by the child process to wait parameter.

Argument pid waitpid function for a child process accordingly screening.

  • pid> 0: only wait for the process ID of the child process pid, no matter how many other child process has run out of the end, as long as the specified child process is not over, waitpid to wait forever.
  • pid = -1: waiting for any one sub process exits without any limitation, this time is equivalent to waitpid wait.
  • pid = 0: wait for any child process with a process group, if a child process has joined other process group, waitpid it will not do anything to ignore.
  • pid <-1: waiting for any child processes in a process group specified, ID, the process is set equal to the absolute pid.

Options waitpid function parameter for further controlling the operation waitpid function, which may be 0, and may be one WNOHANG WUNTRACED two options, or the use of "or" operator "I" symbolic link.

  • WNOHANG: If the child process specified by pid is not immediately available, the letter waitpid function is not blocked, then return to "0."
  • WUNTRACED: if its implementation supports job control, designated by the pid any child process is already in a suspended state, and unreported, its status is returned.

For waitpid function, if the specified process or process group does not exist, or the child process pid argument specifies the process is not called by the parent process, will go wrong.

Overall, waitid function provides a wait function does not have the following three functions:

  • We can wait for a specified process ends.
  • The state can not block parent get the child process.
  • Support job control.

Use waitpid function exit process

#include <sys/types.h>                                                                                                                                                   #include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
    pid_t pid;
    if((pid=fork())<0)  //创建子进程失败
    {
        perror("创建子进程失败!\n");
        exit(0);
    }
    else if(pid==0)  //进入子进程
    {
        if((pid=fork())<0)
        {
            perror("创建子进程失败!\n");
            exit(0);
        }
        else if(pid>0)
        {
            exit(0);
        }
        else
        {
            sleep(2);
            printf("这是第二个子进程,parent pid=%d\n",getppid());
            exit(0);
        }
    }  
    if(waitpid(pid,NULL,0)!=pid)
    {
        perror("waitpid 销毁进程失败!\n");
        exit(0);
    }
    exit(0);
}

Compile and run, you can see after exiting the second sub-process will stop running have been waiting for, then you can use ctrl + c to exit.

[cassie@localhost 练习]$ gcc waitpid.c -o waitpid
[cassie@localhost 练习]$ ./waitpid
[cassie@localhost 练习]$ 这是第二个子进程,parent pid=1
^C
Published 71 original articles · won praise 131 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43239560/article/details/102728631