I am writing my own UNIX-shell in C, If write &
at the end of the command then the parent wont stop for the child to end and child will be backgrounded.
To remove the zombie child process I am using waitpid(-1,&status,WNOHANG)
but it returns 0
and does not remove any zombie process, while there are zombie process.
What changes should I make?
//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...
");
waitpid(-1,&status,WNOHANG);
}
Here "shell" is the name of my C program.
As you can see an ls<defunct>
process is still there even after using waitpid(-1,&status,WNOHANG)
question from:
https://stackoverflow.com/questions/65918250/how-to-remove-zombie-process-from-my-own-c-unix-shell 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…