Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

operating system - How to remove zombie process from my own C UNIX shell

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);

    }  

My shell output

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...