Unix Programming - monitor and restart child process

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2006 > monitor and restart child process





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 monitor and restart child process
nadav

2006-06-13, 1:23 pm

i have a program that has 2 forked children. i have a couple of
quesitons.

in the main i fork 2 children:

something like:

int main ()
{

pid_t procA;
pid_t procB

if ( (procA = fork() ) == 0 )
{
run_procA();
}

if ( (procB = fork() ) == 0 )
{
run_procB();
}

while (1)
{
}

return 0;
}


Question -
1. i use the while(1) in order to keep the parent alive so he can wait
for signal from the children (in case they terminate), i have code for
that.
is their another way i can use rather then this busy wait?

2. i want to restart any child process when he terminates (if he
terminates...) , what is the best way?

i have a signal catcher function that handles signals from the child ,
correntlly it is used to avoid aombie and only wait for SIGINIT , so i
know when they crash.

i am new at this so thank alot for any response.
btw - i write in c.
thanks.

Duncan Muirhead

2006-06-13, 1:23 pm

On Tue, 13 Jun 2006 07:02:58 -0700, nadav wrote:

> i have a program that has 2 forked children. i have a couple of
> quesitons.
>
> in the main i fork 2 children:
>
> something like:
>
> int main ()
> {
>
> pid_t procA;
> pid_t procB
>
> if ( (procA = fork() ) == 0 )
> {
> run_procA();
> }
>
> if ( (procB = fork() ) == 0 )
> {
> run_procB();
> }
>
> while (1)
> {
> }
>
> return 0;
> }
>
>
> Question -
> 1. i use the while(1) in order to keep the parent alive so he can wait
> for signal from the children (in case they terminate), i have code for
> that.
> is their another way i can use rather then this busy wait?
>
> 2. i want to restart any child process when he terminates (if he
> terminates...) , what is the best way?
>
> i have a signal catcher function that handles signals from the child ,
> correntlly it is used to avoid aombie and only wait for SIGINIT , so i
> know when they crash.
>
> i am new at this so thank alot for any response.
> btw - i write in c.
> thanks.

try
man 2 wait


SM Ryan

2006-06-14, 1:28 am

# Question -
# 1. i use the while(1) in order to keep the parent alive so he can wait
# for signal from the children (in case they terminate), i have code for
# that.
# is their another way i can use rather then this busy wait?

Use a blocking wait().

# 2. i want to restart any child process when he terminates (if he
# terminates...) , what is the best way?

You can do something like
pid_t procA = -1,procB = -1;
for (;;) {
if (procA<0) {
procA = fork();
if (procA<0) perror...
else if (procA==0) ...
}
if (procB<0) {
procB = fork();
if (procB<0) perror...
else if (procB==0) ...
}
int s; pid_t w = wait(&s);
if (w==procA) {
reportStatus("A",s);
procA = -1;
}else if (w==procB) {
reportStatus("B",s);
procB = -1;
}else (w<0 && errno!=EINTR) perror...
}

# i have a signal catcher function that handles signals from the child ,
# correntlly it is used to avoid aombie and only wait for SIGINIT , so i
# know when they crash.

Catching SIGCHLD is not what releases the child process, its the wait
that captures the final status that sends the child to its final reward,
the great bit dump in the sky.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
There are subtler ways of badgering a witness.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com