From: alexandrug on 27 Nov 2009 15:08 Greetings, Suppose I have this code : { pid = fork(); int exitcode = 0; if (pid == 0) { do_some_actions(); } if ( waitpid(pid, &exitcode,0) == -1) { switch(errno) { case ECHILD: do_something(); break; case EINTR: break; default : break; } } In what conditions we can have waitpid == -1 and errno == ECHILD, supposing that the signal handler for SIGCHLD is NOT SIG_IGN ? Thank you, Alexandru.
From: Eric Sosman on 27 Nov 2009 15:25 alexandrug wrote: > Greetings, > > Suppose I have this code : > > { > pid = fork(); > int exitcode = 0; > > if (pid == 0) { do_some_actions(); } > > if ( waitpid(pid, &exitcode,0) == -1) > { > switch(errno) { > case ECHILD: do_something(); > break; > case EINTR: break; > default : break; > } > } > > In what conditions we can have waitpid == -1 and errno == ECHILD, > supposing that the signal handler for SIGCHLD is NOT SIG_IGN ? In what you've shown, the waitpid() call executes in both the parent and the child processes. In the child, it tries to wait for pid zero -- that is, for any of the child's own children. If the child has no children of its own, ECHILD is correct. If that's not the problem with your actual code -- well, I'm not a mind reader, and I can't debug what I can't see. -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: alexandrug on 27 Nov 2009 16:24 On Nov 27, 10:25 pm, Eric Sosman <esos...(a)ieee-dot-org.invalid> wrote: > alexandrug wrote: > > Greetings, > > > Suppose I have this code : > > > { > > pid = fork(); > > int exitcode = 0; > > > if (pid == 0) { do_some_actions(); } > > > if ( waitpid(pid, &exitcode,0) == -1) > > { > > switch(errno) { > > case ECHILD: do_something(); > > break; > > case EINTR: break; > > default : break; > > } > > } > > > In what conditions we can have waitpid == -1 and errno == ECHILD, > > supposing that the signal handler for SIGCHLD is NOT SIG_IGN ? > > In what you've shown, the waitpid() call executes in both > the parent and the child processes. In the child, it tries to > wait for pid zero -- that is, for any of the child's own children. > If the child has no children of its own, ECHILD is correct. > > If that's not the problem with your actual code -- well, > I'm not a mind reader, and I can't debug what I can't see. > > -- > Eric Sosman > esos...(a)ieee-dot-org.invalid More precisely, the code is like this : { pid = fork(); int exitcode = 0; if (pid == 0) while(1) { do_some_actions(); } // only in parent process if ( waitpid(pid, &exitcode,0) == -1) { switch(errno) { case ECHILD: do_something(); break; case EINTR: break; default : break; } }
From: Alan Curry on 27 Nov 2009 17:59 In article <e22036c1-e514-430d-b372-93681ff2525c(a)m3g2000yqf.googlegroups.com>, alexandrug <webaigoia(a)gmail.com> wrote: >> > In what conditions we can have waitpid == -1 and errno == ECHILD, >> > supposing that the signal handler for SIGCHLD is NOT SIG_IGN ? > >More precisely, the code is like this : > >{ >pid = fork(); >int exitcode = 0; > >if (pid == 0) while(1) { do_some_actions(); } > >// only in parent process > >if ( waitpid(pid, &exitcode,0) == -1) >{ >switch(errno) { >case ECHILD: do_something(); > break; >case EINTR: break; >default : break; > >} >} It could happen if pid==(pid_t)-1 i.e. fork failed, which you didn't check for. -- Alan Curry
From: Jiří Paleček on 27 Nov 2009 17:52
On Fri, 27 Nov 2009 21:08:35 +0100, alexandrug <webaigoia(a)gmail.com> wrote: > Greetings, Hello, > Suppose I have this code : > > { > pid = fork(); > int exitcode = 0; > > if (pid == 0) { do_some_actions(); } > > if ( waitpid(pid, &exitcode,0) == -1) > { > switch(errno) { > case ECHILD: do_something(); > break; > case EINTR: break; > default : break; > } > } > > In what conditions we can have waitpid == -1 and errno == ECHILD, > supposing that the signal handler for SIGCHLD is NOT SIG_IGN ? waitpid() exeuted in the parent can fail with ECHILD eg. if there is a handler for SIGCHLD and it calls wait*() function, or if another thread calls wait() simultaneously. IMHO the best way to inspect this is run the program under strace. Regards Jiri Palecek |