1

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...\n");
          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)

Rocket
  • 123
  • 8
  • The waitpid(,,WNOHANG) is guaranteed to not block. But you still have to wait for the child process. – wildplasser Jan 27 '21 at 12:22
  • But the parent cant wait for the child process to end. – Rocket Jan 27 '21 at 12:24
  • 1
    You could catch SIGCHLD and wait after the signal has been raised. Or just wait periodically, e.g. before issuing a new pronpt (for an interactive shell) – wildplasser Jan 27 '21 at 12:26
  • But why waitpid(,,WNOHANG) is not working? – Rocket Jan 27 '21 at 12:32
  • You want to test the value returned by `waitpid()` and act accordingly. – alk Jan 27 '21 at 12:56
  • That was shorthand, I omitted the first two arguments -->> `waitpid(-1,&status,WNOHANG)`. You do know what *blocking* means in this context? – wildplasser Jan 27 '21 at 13:28
  • 2
    You claim `waitpid` is returning 0, but your code discards the value returned by `waitpid`, so how do you know? Is this the actual code? It looks like a simple race condition; the parent is calling `waitpid` before the `ls` has finished. After the `waitpid` returns, `ls` finishes and the process is a zombie until the next call to `waitpid`. – William Pursell Jan 27 '21 at 14:58
  • thank you this is what was happening – Rocket Jan 27 '21 at 16:35

0 Answers0