Linux 进程等待
1、进程等待
僵尸进程 是一个比较麻烦的问题,如果不对其做出处理,僵尸进程 就会越来越多,导致 内存泄漏 和 标识符 占用问题
进程一旦变成僵尸状态,那就刀枪不入,“杀人不眨眼”的kill -9 也无能为力,因为谁也没有办法杀死一个已经死去的进程
2、等待原因
子进程运行结束后,父进程没有等待并接收其退出码和退出状态,OS 无法释放对应的 内核数据结构+代码和数据,出现 僵尸进程
为了避免这种情况的出现,父进程可以通过函数等待子进程运行结束,此时父进程属于阻塞状态
注意:
- 进程的退出状态是必要的
- 进程的执行结果是非必要的
- 也就是说,父进程必须对子进程负责,确保子进程不会连累 OS,而子进程执行的结果是否正确,需要我们自行判断
3.等待函数
3.1 wait()
#include<sys/types.h> #include<sys/wait.h> pid_t wait(int*status); 返回值:成功返回被等待进程pid,失败返回-1。 参数:输出型参数,获取子进程退出状态,不关心则可以设置成为NULL
代码验证:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}
int main()
{printf("I am a father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){RunChild();printf("child quit...\n");exit(123);}sleep(6);pid_t rid = wait(NULL);sleep(2);printf("father quit...\n");return 0;
}
注意:wait等待的是任意一个进程。
3.2 waitpid方法
pid_ t waitpid(pid_t pid, int *status, int options);
返回值:当正常返回的时候waitpid返回收集到的子进程的进程ID;如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;
参数:pid:Pid=-1,等待任一个子进程。与wait等效。Pid>0.等待其进程ID与pid相等的子进程。pid<0,等待的进程错误,比如进程不存在。status:WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)WEXITSTATUS(status): 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)options:WNOHANG: 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进程的ID。
代码展示:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}
int main()
{printf("I am a father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){RunChild();printf("child quit...\n");exit(123);}sleep(6);pid_t rid = waitpid(-1,NULL,0);//可以匹配任何子进程sleep(2);printf("father quit...\n");return 0;
}
当waitpid函数以 waitpid(-1,NULL,0) 进行传参,效果跟wait是一样的,都是对任意一个进程等待。
4. 获取子进程status
- wait和waitpid,都有一个status参数,该参数是一个输出型参数,由操作系统填充。
- 如果传递NULL,表示不关心子进程的退出状态信息。
- 否则,操作系统会根据该参数,将子进程的退出信息反馈给父进程。
- status不能简单的当作整形来看待,可以当作位图来看待,具体细节如下图(只研究status低16比特位)
代码演示:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}
int main()
{printf("I am a father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){RunChild();printf("child quit...\n");exit(123);}sleep(6);// fatherint status = 0;pid_t rid = waitpid(id,&status,0);if(rid > 0){printf("wait sucess,pid:%d\n",rid);}else{printf("wait failed!\n");}sleep(2);printf("father quit...,child quit code :%d,child signal code:%d\n",(status>>8)&0xFF,status & 0x7F);return 0;
}
5.阻塞等待
前面我们写的都是阻塞等待的代码,但是都是我们自己手动判断结果,此处还有两个宏,下面使用宏来演示阻塞等待的代码。
(status >> 8) & 0xFF 和 (status & 0x7F) 这两个位运算难记,系统还提供了两个宏来简化代码
WIFEXITED(status) 判断进程退出情况,当宏为真时,表示进程正常退出
WEXITSTATUS(status) 相当于 (status >> 8) & 0xFF,直接获取退出码
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}int main()
{printf("I am father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){// child RunChild(); printf("child quit...\n");exit(123);}sleep(6);// fatherint status = 0;pid_t rid = waitpid(id,&status,0);if(rid > 0){if(WIFEXITED(status))printf("child quit success,child exit code:%d\n",WEXITSTATUS(status));else printf("child quit unnormal!\n");printf("wait success\n");}else {printf("wait failed!\n");}sleep(3);printf("father quit, status: %d, child quit code : %d, child quit signal: %d\n", status, (status>>8)&0xFF, status & 0x7F);return 0;
}
6.非阻塞等待
非阻塞等待就是父进程到达waitpid等待函数处,如果子进程还在运行,那么父进程先不等子进程退出,直接往下面运行,通过while循环实现轮询检查,这样一来使得等待子进程退出的时间,父进程可以做自己的事情,实现了非阻塞等待。
wait函数只能是一个阻塞等待的函数,如果子进程没有退出,父进程就会在一直在wait调用处等待子进程。
只有 waitpid函数,通过options 参数,可以实现非阻塞等待
//options 参数
WNOHANG//比如
waitpid(id, &status, WNOHANG);
若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进程的ID。
waitpid() 函数返回值
- pid_t > 0 : 等待成功,子进程退出,并且父进程回收成功。
- pid_t = 0 : 检测成功,但是子进程没有退出,需要下一次进行重复等待。
- pid_t < 0 : 等待失败。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> //进程等待相关函数头文件int main()
{//演示 waitpid()pid_t id = fork(); //创建子进程if(id == 0){int time = 9;int n = 0;while(n < time){printf("我是子进程,我已经运行了:%d秒 PID:%d PPID:%d\n", n + 1, getpid(), getppid());sleep(1);n++;}exit(244); //子进程退出}int status = 0; //状态pid_t ret = 0;while(1){ret = waitpid(id, &status, WNOHANG); //参数3 设置为非阻塞状态if(ret == -1){printf("进程等待失败!进程不存在!\n");break;}else if(ret == 0){ printf("子进程还在运行中!\n");printf("我可以干一些其他任务\n");sleep(3);}else{printf("进程等待成功,子进程已被回收\n");//通过 status 判断子进程运行情况if(WIFEXITED(status)){printf("子进程正常退出,退出码:%d\n", WEXITSTATUS(status));break;}else{printf("子进程异常退出,code dump:%d 退出信号:%d\n", (status >> 7) & 1, (status & 0x7F));break;}}}return 0;
}