Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

waitpid == -1 and errno == ECHILD

455 views
Skip to first unread message

alexandrug

unread,
Nov 27, 2009, 3:08:35 PM11/27/09
to
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.

Eric Sosman

unread,
Nov 27, 2009, 3:25:09 PM11/27/09
to

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
eso...@ieee-dot-org.invalid

alexandrug

unread,
Nov 27, 2009, 4:24:21 PM11/27/09
to
> esos...@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

Alan Curry

unread,
Nov 27, 2009, 5:59:52 PM11/27/09
to
In article <e22036c1-e514-430d...@m3g2000yqf.googlegroups.com>,

alexandrug <weba...@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

Jiří Paleček

unread,
Nov 27, 2009, 5:52:18 PM11/27/09
to alexandrug
On Fri, 27 Nov 2009 21:08:35 +0100, alexandrug <weba...@gmail.com> wrote:

> Greetings,

Hello,

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

alexandrug

unread,
Nov 28, 2009, 8:16:33 AM11/28/09
to
On Nov 28, 12:59 am, pac...@kosh.dhis.org (Alan Curry) wrote:

> In article <e22036c1-e514-430d-b372-93681ff25...@m3g2000yqf.googlegroups.com>,


>
>
>
> alexandrug  <webaig...@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

Suppose that

1) pid = fork() > 0, in parent process (ie, fork() was successful)
2) there is NOT any handler for SIGCHLD, that colls wait*()
3) there is NOT any thread which calls wait*()
4) the signal handler for SIGCHLD is set, but NOT to SIG_IGN

5) at runtime, we have waitpid(pid, &exitcode, 0) == -1

In what conditions we could have errno == ECHILD ? (*)

I noticed that if I kill -9 (manually) the child process, then
the child process (defunct) will still exist in the process table,
and a waitpid() on it would return a code of success.

Please help me with my question : (*).

Thanks,
Alexandru.

Alan Curry

unread,
Nov 28, 2009, 5:29:58 PM11/28/09
to
In article <31306251-f3bb-49e4...@9g2000yqa.googlegroups.com>,

alexandrug <weba...@gmail.com> wrote:
>On Nov 28, 12:59�am, pac...@kosh.dhis.org (Alan Curry) wrote:
>
>Suppose that
>
>1) pid = fork() > 0, in parent process (ie, fork() was successful)
>2) there is NOT any handler for SIGCHLD, that colls wait*()
>3) there is NOT any thread which calls wait*()
>4) the signal handler for SIGCHLD is set, but NOT to SIG_IGN
>
>5) at runtime, we have waitpid(pid, &exitcode, 0) == -1
>
>In what conditions we could have errno == ECHILD ? (*)

Let's just say it's a kernel bug. If it's reproducable, write a minimal test
program that demonstrates it, put a system("ps axj") after the errno==ECHILD
test, and follow your vendor's bug reporting procedure.

--
Alan Curry

0 new messages