|
Home > Archive > Unix Programming > March 2005 > How do I make process wait on child or kill ?
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 |
How do I make process wait on child or kill ?
|
|
| codefixer@gmail.com 2005-03-28, 6:18 pm |
| Hi,
I have a situation where I have to handle the following scenario.
The main() must wait for child to complete or the main() must kill the
child after 3 seconds and exit.
/* Assume everything is declared */
time(&start);
while ( childpid != wait(&status) )
{
time(&end);
diff = difftime(end,start);
fprintf(stderr, "\n%ld",diff);
if (diff >= 3.00)
{
/* Kill child */
exit(0);
}
}
}
Even though the child is taking more than 3 seconds, this doesn't seem
to work. Since wait() and waitpid() wait unconditionally, how can I
make this work ? Any suggestions ?
Thanks.
NOTE: Posting it here on request from folks on comp.lang.c
| |
| Fletcher Glenn 2005-03-28, 6:18 pm |
| codefixer@gmail.com wrote:
> Hi,
>
>
> I have a situation where I have to handle the following scenario.
> The main() must wait for child to complete or the main() must kill the
> child after 3 seconds and exit.
>
>
> /* Assume everything is declared */
>
>
> time(&start);
> while ( childpid != wait(&status) )
> {
> time(&end);
> diff = difftime(end,start);
> fprintf(stderr, "\n%ld",diff);
> if (diff >= 3.00)
> {
> /* Kill child */
> exit(0);
> }
>
>
>
> }
> }
>
>
> Even though the child is taking more than 3 seconds, this doesn't seem
> to work. Since wait() and waitpid() wait unconditionally, how can I
> make this work ? Any suggestions ?
>
> Thanks.
>
>
> NOTE: Posting it here on request from folks on comp.lang.c
>
waitpid() can be used with the W_NOHANG flag to sample if the child is
done. You can then use usleep or sleep to kill time before you test
waitpid() again.
--
Fletcher Glenn
| |
| Gordon Burditt 2005-03-28, 6:18 pm |
| >I have a situation where I have to handle the following scenario.
>The main() must wait for child to complete or the main() must kill the
>child after 3 seconds and exit.
>
>
>/* Assume everything is declared */
>
>
>time(&start);
>while ( childpid != wait(&status) )
>{
> time(&end);
> diff = difftime(end,start);
> fprintf(stderr, "\n%ld",diff);
> if (diff >= 3.00)
> {
> /* Kill child */
> exit(0);
> }
>
>
>
>}
>}
>
>
>Even though the child is taking more than 3 seconds, this doesn't seem
>to work. Since wait() and waitpid() wait unconditionally, how can I
>make this work ? Any suggestions ?
Find something that DOESN'T wait unconditionally. For example,
waitpid() with the WNOHANG option. That way your (parent) process
can chew up all the CPU time so the child never gets a chance
to run to completion (or maybe not at all).
Gordon L. Burditt
| |
| Alex Fraser 2005-03-28, 6:18 pm |
| <codefixer@gmail.com> wrote in message
news:1112038977.162287.250140@o13g2000cwo.googlegroups.com...
> I have a situation where I have to handle the following scenario.
> The main() must wait for child to complete or the main() must kill the
> child after 3 seconds and exit.
A quick hack is to use nanosleep()/waitpid(WNOHANG) in a loop. Better but
slightly more complex solutions include using SIGALRM to detect the timeout
(blocking on waitpid()) or SIGCHLD to detect the child's termination
(blocking on nanosleep()).
Alex
| |
| codefixer@gmail.com 2005-03-29, 2:49 am |
|
Alex Fraser wrote:
> <codefixer@gmail.com> wrote in message
> news:1112038977.162287.250140@o13g2000cwo.googlegroups.com...
the[vbcol=seagreen]
>
> A quick hack is to use nanosleep()/waitpid(WNOHANG) in a loop. Better
but
> slightly more complex solutions include using SIGALRM to detect the
timeout
> (blocking on waitpid()) or SIGCHLD to detect the child's termination
> (blocking on nanosleep()).
>
Thanks. With WNOHANG option I got it to work.
> Alex
|
|
|
|
|