1.僵尸进程:父进程还在执行,子进程已经结束,子进程依然占用部分资源,这时的子进程就是僵尸进程。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
fprintf(stderr, "fork error %s\n", strerror(errno));
exit(1);
}
else if(pid > 0)//parent process
{
while(1)
{
sleep(3);
}
printf("pid: %d, ppid: %d\n", getpid(), getppid());
}
else
{
printf("pid: %d, ppid: %d\n", getpid(), getppid());
exit(1);
}
return 0;
}
执行结果:
[root@localhost process]# ./03process
pid: 3384, ppid: 3383
这时打开另一个终端查看进程状态:
[root@localhost Desktop]# ps -ef | grep 03process
root 3383 2718 0 08:34 pts/0 00:00:00 ./03process
root 3384 3383 0 08:34 pts/0 00:00:00 [03process] <defunct>
root 5445 3265 1 08:35 pts/1 00:00:00 grep 03process
此时,3383是父进程,3384是子进程(僵尸进程),只能通过杀死父进程来杀死僵尸进程:
[root@localhost Desktop]# kill -9 3383
[root@localhost Desktop]# ps -ef | grep 03process
root 5455 3265 2 08:37 pts/1 00:00:00 grep 03process
杀死父进程后查看执行终端:
[root@localhost process]# ./03process
pid: 3384, ppid: 3383
Killed
通过wait()函数来处理僵尸进程(最合理的方式):
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
fprintf(stderr, "fork error %s\n", strerror(errno));
exit(1);
}
else if(pid > 0)//parent process
{
wait(0);
while(1)
{
sleep(3);
}
printf("pid: %d, ppid: %d\n", getpid(), getppid());
}
else
{
printf("pid: %d, ppid: %d\n", getpid(), getppid());
exit(1);
}
return 0;
}
上面程序中的wait(0)会感知子进程一旦执行完毕,就替它回收资源,从而不会产生僵尸进程。
2.孤儿进程:父进程退出,它的子进程还在运行,那么这些子进程就是孤儿进程,孤儿进程被init进程(1号进程)所收养,并由init进程负责他们的退出时清理工作。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
fprintf(stderr, "fork error %s\n", strerror(errno));
exit(1);
}
else if(pid > 0)//parent process
{
sleep(1);
printf("pid: %d, ppid: %d\n", getpid(), getppid());
exit(1);//parent process exit
}
else
{
sleep(3);
printf("pid: %d, ppid: %d\n", getpid(), getppid());
}
printf("pid: %d, ppid: %d\n", getpid(), getppid());
return 0;
}
执行结果:
[root@localhost process]# ./03process
pid: 5573, ppid: 5572
pid: 5572, ppid: 2718
[root@localhost process]# pid: 5573, ppid: 1
可以看到父进程先结束,子进程成为孤儿进程继续执行,主进程退出,孤儿进程交由1号进程,他负责回收孤儿进程。