Process subprocess recycling of linux operating system

There are two main functions for recycling child processes: wait(), waitpid().
When a process terminates, it will close all file descriptors and release the memory allocated in user space, but its PCB is still retained, and the kernel saves some information in it: if it terminates normally, it saves the exit status, and if it terminates abnormally Then save which signal caused the process to terminate . The parent process of the process can call wait or waitpid to get this information, and then completely clear the process. We know that the exit status of a process can be viewed in the shell with the special variable $?, because the shell is its parent process. When it terminates, the shell calls wait or waitpid to get its exit status and completely clear the process.

wait function The
parent process calls the wait function to recover the termination information of the child process. This function has three functions:

① (Parent process) Block waiting for the child process to exit
② Recycle the residual resources
of the child process ③ Obtain the end status of the child process (exit reason).

pid_t wait(int *status); success: child process ID cleaned up; failure: -1 (no child process)

When the process terminates, the operating system's implicit reclamation mechanism will: 1. Close all file descriptors 2. Free the memory allocated by user space. The PCB for the core still exists. which holds the exit status of the process. (normal termination → exit value; abnormal termination → termination signal)
You can use the wait function to pass the parameter status to save the exit status of the process. Use the macro function to further determine the specific reason for the termination of the process. The macro functions can be divided into the following three groups:
1. WIFEXITED(status) is non-0 → the process ends normally
WEXITSTATUS(status) If the above macro is true, use this macro → get the process exit status (parameter of exit)
2. WIFSIGNALED(status) is non-0 → process terminated abnormally
WTERMSIG(status) If the above macro is true, use this macro → get the number of the signal that caused the process to terminate.
*3. WIFSTOPPED(status) is non-0 → the process is in a suspended state
WSTOPSIG(status) If the above macro is true, use this macro → get the number of the signal that causes the process to be suspended.
WIFCONTINUED(status) is true → the process has resumed running after being suspended

Case number one

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main()
{
    pid_t pid;
    int wpid;
    pid =  fork();
    if(pid == -1)
    {
            perror("fork:");
            exit(1);
    }
    if(pid == 0)
    {
            printf("i am child .pid = %ld,ppid = %ld\n",getpid(),getppid());
            sleep(10);
            printf("child did.\n");
    }
    else if(pid > 0)
    {
            sleep(1);
            wait(&wpid);//加了wait函数后,父进程阻塞在这里,不再继续执行,等待子进程执行完,然后回收子进程,释放内核资源,获取子进程退出状态。**当子进程接收,父进程继续执行。**
            if(wpid==-1)
            {
                perror("wait error:");
                exit(1);
            }
            while(1)
            {
                   printf("i am parent. pid = %d,myson= %ld\n",getpid(),pid);
                    sleep(1);
            }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643917&siteId=291194637