Mayank Jain <[EMAIL PROTECTED]> wrote: On 12/25/05, Gora Mohanty  wrote:
 >>  Um, what do you mean by a "non-blocking wait." Calling
>>  wait()/waitpid() means that you *want* the parent process to
>>  wait for the child to finish processing (or, some other 
 >> condition occurs, as specified in the 3rd argument).

> What if i want just to check the status of the child? Is it alive or
> not? I dont want to *wait*...
 As Manish suggests, checking the return value of waitpid() with the
 WNOHANG option will tell you whether the child exited immediately.
 With WNOHANG, if the return value is 0, the child has not yet exited
 (really, not changed state), else if the return value is non-zero, then
 it is the PID of the child process that changed state. If you want to
 be informed as and when the child exits, a much better way than
 looping is to set up the child to signal the parent.
    Also, I did not notice this in my initial reading of your message, but
 there is no need to loop around waitpid(). A single call is sufficient.
 Try the following code (note that I have added an immediate exit in the
 child):
 
 #include <unistd.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <sys/wait.h>
 #include <stdlib.h>
 int main()
 {
    pid_t child = 0;
    int status = 0;
    printf("forking!\n");
 
    child = fork();
    if(child == 0)
      {
          exit(0);
          printf("Inside child\n");
          sleep(2);
          printf("Finishing if\n");
       }
    else
       {
          pid_t retval;
          printf("Inside Parent\nCalling waitpid\n");
          retval = waitpid(child, &status, WNOHANG);
          if(WIFEXITED(status))
            {
               printf("Child exited: waitpid returned %d\n", retval);
             }
         printf("Finishing else\n");
      }
   return 0;
 }
 
 With this, you should get some process ID for the return value of
 waitpid(). If you remove the exit(0) in the  child, you should get zero
 for the retunr value. Is that what you wanted?
 
 Regards,
 Gora


Send instant messages to your online friends http://in.messenger.yahoo.com 
_______________________________________________
ilugd mailinglist -- [email protected]
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/[email protected]/

Reply via email to