Article Directory
Process waiting
The parent process waits for the child process to launch, gets its exit return value, and releases the child process's resources
Purpose: avoid zombie processes
Header file: #include <sys/wait.h>
1、 pid_t wait(int *status);
Block and wait for any child process to exit, obtain the exit return value through status, and release the resource
**Return value: **If successful, return the pid of the exited child process; if an error occurs, return -1
2、pid_t waitpid(pid_t pid, int *status, int options);
You can wait for any child process to exit, or you can wait for the specified child process to exit.
pid_t pid: equal to -1, means waiting for any child process to exit; greater than 0, means waiting for the specified child process to exit. It
can be blocking waiting for the child process to exit, or it can be non- Block waiting for the child process to exit
int options: set to 0, perform blocking waiting; set to WNOHANG , perform non-blocking waiting
Return value:If successful, return the child process pid;
if no child process exits, return 0 (in non-blocking state, it will wait forever in blocking state);
if an error occurs, return -1
Note: If there is a child process that has exited, wait/waitpid will process it directly without waiting
Blocking and non-blocking
Blocking: In order to complete a function, initiate a call, if the function completion conditions are not met, wait forever.
Non-blocking: In order to complete a function, initiate a call, if the function completion conditions are not met, immediately report an error and return
Code example:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int pid=fork();
if(pid<0)
{
//出错
perror("fork error");
return -1;
}
else if(pid==0)
{
//子进程
sleep(5);//休眠5秒
return 99;//main中的return--退出子进程,退出返回值为99
}
else
{
//父进程
while(1)sleep(1);//保持休眠
}
return 0;
}
The child process sleeps for 5 seconds and then exits and enters a zombie state. Memory leaks will occur. Now
modify the code and add a memory waiting operation
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
int pid=fork();
if(pid<0)
{
//出错
perror("fork error");
return -1;
}
else if(pid==0)
{
//子进程
sleep(5);
return 99;//main中的return--退出子进程,退出返回值为99
}
//int ret=waitpid(-1,NULL,0); //与21行等价,用哪个都行
int ret=wait(NULL);//等待任意子进程退出
printf("progress:%d exit\n",ret);
//父进程
while(1)sleep(1);//死循环
return 0;
}
Print information: The child process with pid=6035 exited
The result shows that the child process exits directly and no longer exists
Let's look at the code of waitpid's non-blocking state:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
int pid=fork();
if(pid<0)
{
//出错
perror("fork error");
return -1;
}
else if(pid==0)
{
//子进程
sleep(5);
return 99;//main中的return--退出子进程,退出返回值为99
}
// int ret=wait(NULL);
// printf("progress:%d exit\n",ret);
int ret;
while((ret=waitpid(-1,NULL,WNOHANG))==0)
{
//非阻塞状态下,没有子进程退出,返回0
printf("当前子进程还没有退出\n");
sleep(1);
}
if(ret<0)
{
perror("waitpid error:");
}
printf("progress:%d exit\n",ret);
//父进程
while(1)
{
sleep(1);//死循环
}
return 0;
}