Linux process 3: The parent process waits (wait, waitpid) for the child process to exit and collects the child process exit status, zombie process

The parent process waits (wait, waitpid) for the child process to exit and collects the child process exit status

1. Why does the parent process wait for the child process to exit?

The purpose of creating the child process: ------ Work
We need to know whether the work is over.
The exit status of the child process is not collected and becomesZombie process

Wait: wait, waitpid function:

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

Status parameter:
is an integer pointer
non empty: The exit status of the child process is placed in the address it points to
air: Do not care about the exit status.
If all of its child processes are still running, wait is blocked.
If a child process has terminated and is waiting for the parent process to obtain its termination status, wait obtains the termination status of the child process and returns immediately
if it has no children. Process, an error immediately returns

The difference between wait and waitpid:

Difference: wait makes the caller block, waitpid has an option to make the caller not block: options

The parameter pid
can be seen from the parameter name pid and type pid_t. What is needed here is a process ID, but when pid takes a different value, it has a different meaning here.
1. When pid>0: Only wait for the child process whose process ID is equal to pid, no matter how many other child processes are finished and exit, as long as the specified child process has not ended, waitpid will wait forever.
2. When pid=-1: Wait for any child process to exit without any restrictions. At this time, waitpid and wait have exactly the same functions.
3. When pid=0: Wait for any child process in the same process group. If the child process has entered another process group, waitpid will not give any attention to it.
4. When pid<-1: wait for any child process in a specified process, the ID of this process group is equal to the absolute value of pid

Parameters: options:
Insert picture description here

wait demo code:

#include <sys/types.h>
#include <sys/wait.h> 
#include <unistd.h> 
#include <stdlib.h>
 #include <stdlib.h> 
#include <stdio.h> 
int main() 
{
    
     
pid_t pid,pc; 
int status; 
printf("wait实例:\n"); 
pid=fork(); 
if(pid<0)//创建出错, 
printf("error ocurred!\n"); 
else if(pid == 0) //如果是子进程 
{
    
     
printf("我是子进程的ID=%d\n",getpid());
 sleep(10); //睡眠10秒 
exit(7); 
} 
else //父进程 
{
    
     
pc=wait(&status); //等待子进程结束; 得到子进程的ID
if(WIFEXITED(status)) //子程序正常结束返回非0值,异常返回0
{
    
     
printf("我是父进程,我等待的子进程的id号=%d\n",pc); 
printf("退出码是%d\n",WEXITSTATUS(status)); 
} 
else 
{
    
     
printf("子进程退出异常!\n"); 
} 
} 
exit(0);
}

Code implementation: The parent process waits for the child process to exit.
There is a waiting time of 10 seconds before the result of line 3 is printed. This is the time we set for the child process to sleep. Only when the child process wakes up from sleep can it exit normally and be captured by the parent process. To.

Running results:
Insert picture description here
waitpid code demonstration:

#include <sys/types.h>
#include <sys/wait.h> 
#include <unistd.h> 
#include <stdlib.h>
 #include <stdlib.h> 
#include <stdio.h> 
int main() 
{
    
     
pid_t pid,pc;
 pid=fork(); 
int status; 
if(pid<0) 
{
    
     
printf("创建进程失败!\n"); 
} 
else if(pid==0) 
{
    
     
printf("我是子进程,我的ID=%d\n",getpid());
 sleep(10); 
exit(0); 
} 
else 
{
    
     
do
{
    
     
pc=waitpid(pid,&status,WNOHANG);//使用了WNOHANG参数,waitpid就不会等待,直接返回0. 
// pc=waitpid(pid,&status,0); 
if(pc==0) 
{
    
     
printf("没有收集到子进程!\n"); 
sleep(1); 
} 
}while(pc==0);//等不到,继续等, 
if(pid==pc) 
printf("等到了子进程\n");
 else printf("出错了\n");
 printf("我是父进程,我要等的进程id是%d\n",pc); 
} 
exit(0); 
}

Operation result:
Insert picture description here
It can be seen from the result:

First execute the parent process, output "No child process is collected", then sleep(1), at this time execute the child process, output "I am...31842", sleep(10); Then execute the parent process, because the parent process will not Waiting for the child process, so the child process is continuously collected, until the child process sleep(10), after the end, it will not be collected.

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108975712