#include <unistd.h> #include <sys/types.h> #include <stdio.h> #include <sys/wait.h>
int main() { pid_t child = 0; int status = 0; printf("forking!\n"); child = fork(); if(child == 0) { printf("Inside child\n"); sleep(2); printf("Finishing if\n"); } else { printf("Inside Parent\n"); while(1) { printf("Calling waitpid\n"); waitpid(child, &status, 0); if(WIFEXITED(status)) { printf("Child exited\n"); break; } } printf("Finishing else\n"); } } The above code works perfectly, the parent waits for the child to die & then exits itself. [EMAIL PROTECTED]:~/junk# ./child forking! Inside child Inside Parent Calling waitpid Finishing if Child exited Finishing else [EMAIL PROTECTED]:~/junk# However, how do i make this wait by the parent a non-blocking wait? This is because while checking for "has the child died", parent is blocked by waitpid & hence cannot do other processing. I've tried using WNOHANG in waitpid as waitpid(child, &status, WNOHANG) & this is the output i get [EMAIL PROTECTED]:~/junk# ./child forking! Inside child Inside Parent Calling waitpid Child exited Finishing else [EMAIL PROTECTED]:~/junk# [EMAIL PROTECTED]:~/junk# Finishing if [EMAIL PROTECTED]:~/junk# Notice that parent exited before child died. Am i using WNOHANG in a wrong way? or in a wrong place? What can be other strategies i can use? I dont want to use threads as they'll be an overkill (all i want to do is keep parent in non-blocking state, while looping for child's alive status). -- regards, makuchaku --- http://makuchaku.info When you speak out with the courage of your convictions, people listen! -- Valmik Thapar, Wildlife Conservationist. _______________________________________________ ilugd mailinglist -- ilugd@lists.linux-delhi.org http://frodo.hserus.net/mailman/listinfo/ilugd Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi http://www.mail-archive.com/ilugd@lists.linux-delhi.org/