3.1分析以下程序,会有僵尸进程出现吗?后台运行该程序,前台用ps命令查看进程,判断是否出现僵尸进程,如果出现,请用三种方法(wait/waitpid,两次fork,捕捉信号SIGCHLD)修改程序处理僵尸进程。
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
main()
{int pid;
if ((pid=fork())==0)
{ sleep(2);printf("child process %d exit\n",getpid());exit(0);}
else
{ printf("hello child\n");
while(1){ sleep(1); };
}
}
先让我们以一段资料来了解一下有关僵尸进程的资料.
从以上资料中我们可以看出,若不对僵尸进程采取措施处理,则会浪费一定的内存空间.
我们先运行一下下述程序,并查看运行结果.
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int pid;
pid = fork();
if(pid == 0) //son process
{
sleep(2);