Linux杀死fork产生的子进程的僵尸进程defunct

Linux杀死fork产生的子进程的僵尸进程defunct,第1张

概述僵尸进程 就是 已经结束,但是还没有清理出去的.用kill -9 $PID 也无法杀死.所以程序中应该避免出现僵尸进程.用fork之后,父进程如果没有wait /waitpid 等待子进程的话,子进程

僵尸进程 就是 已经结束,但是还没有清理出去的.用kill -9 $PID 也无法杀死.

所以程序中应该避免出现僵尸进程.

用fork之后,父进程如果没有wait /waitpID 等待子进程的话,子进程完毕后,就成了僵尸进程.

但是父进程如果等待wait/waitpID的话,就没法干别的事情了...尤其在多个子进程的情况下.所以 中断 信号量 是一个好办法:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

voID sig_child(){
pID_t pID;
int status;
while((pID = waitpID(-1,&status,WNOHANG)) > 0) {}
//Key !!!!!!!! wait or waitpID
return;
}
voID nodefunct_sig(){
signal(SIGCHLD,sig_child);//prevent defunct

int child=0;
pID_t status=0;
int i=0;
while(1){
if(child=fork()==0){
childf();
printf("child(%d):I will be exit...pgID=%d\n",getpID(),getpgID(getpID()));//getpgrp()
abort();
char cmd[1024]="";
sprintf(cmd,"kill -9 %d",getpID());
system(cmd);
exit(5);
}else{
printf("Parent(%d):Main process...\n",getpID());
//kill(child,SIGABRT);
system("ps -A|grep a.out");

sleep(3);
}
printf("Parent: waitting child...pgID=%d\n\n",getpgID(getpID()));
//waitpID(child,
}
}
voID main(){
nodefunct_sig();
}

 


参考自:http://topic.csdn.net/t/20020424/21/673887.html

总结

以上是内存溢出为你收集整理的Linux杀死fork产生的子进程的僵尸进程defunct全部内容,希望文章能够帮你解决Linux杀死fork产生的子进程的僵尸进程defunct所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1015277.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存