如何解决如何从我自己的 C UNIX shell 中删除僵尸进程
我正在用 C 编写我自己的 UNIX-shell,如果在命令的末尾写 &
,那么父进程不会停止子进程结束,子进程将成为后台。
要删除僵尸子进程,我使用了 waitpid(-1,&status,WNOHANG)
但它 returns 0
并且不删除任何僵尸进程,而有僵尸进程。
我应该做哪些改变?
//Everything is inside a while loop
pid_t pid,wpid;
pid=fork();
if(pid==0){
char *args1[] = {d1,NULL}; //d1 is the input from the user
execvp(d1,args1);
exit(pid);
}
if (background == 0){ //If parent has to wait for the child to end
waitpid(pid,NULL,0);
}
else{ //If parent does not wait for the child to end
fprintf(stderr,"Starting background process...\n");
waitpid(-1,WNOHANG);
}
这里的“shell”是我的 C 程序的名称。
如您所见,即使在使用 ls<defunct>
之后,waitpid(-1,WNOHANG)
进程仍然存在
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。