Unix Programming - Waiting for a child to die

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2005 > Waiting for a child to die





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Waiting for a child to die
makuchaku

2005-12-24, 5:56 pm

#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.

root@Warrior:~/junk# ./child
forking!
Inside child
Inside Parent
Calling waitpid
Finishing if
Child exited
Finishing else
root@Warrior:~/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

root@Warrior:~/junk# ./child
forking!
Inside child
Inside Parent
Calling waitpid
Child exited
Finishing else
root@Warrior:~/junk#
root@Warrior:~/junk# Finishing if
root@Warrior:~/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).

Thanks,
makuchaku
http://makuchaku.info

Paul Pluzhnikov

2005-12-24, 5:56 pm

"makuchaku" <mayank.gnu@gmail.com> writes:

> However, how do i make this wait by the parent a non-blocking wait?


The parent needs to (periodically) call "waitpid(... WHOHANG)" in
a loop, and do other processing while the child has not exited yet.

> I've tried using WNOHANG in waitpid as waitpid(child, &status,
> WNOHANG) & this is the output i get


That output is expected.

> Notice that parent exited before child died.


You didn't check return value of waitpid(), which was 0.

When waitpid() returns 0, status remains "untouched", and checking
WIFEXTED(status) is meaningless.

On Linux, WIFEXITED(status) expands into:

((__extension__ ({ union { __typeof(status) __in; int __i; } __u;
__u.__in = (status); __u.__i; }) & 0x7f) == 0)

Which is equivalent to ((status & 0x7F) == 0), which is true if
status is <= 0x7F. That caused your parent to exit.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Rahul

2005-12-26, 7:48 am

well after calling the signal(SIGCHLD,signal_handler),it might be
possible that waitpid changes ur errno value.if you still want to
execute parent after exiting child u can use siginterrupt(SIGCHLD,0).
now it would work.

if problem dosn't solve , write down ur code . then i see.

Regards
Rahul

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com