parenp process controlling child

M

Monika Talwar

How can parent process control a child??
I tried to kill the child, but the code didn;t worked to my
expectation.

Actually i wrote the following code -

# fork first child
if(defined($pid= fork))
{
print "1) pare $pid \n\n";
if($pid) ## If parent process then fork another child to start
timer
{ # fork new child
my $child_pid = fork;

# if parent process then wait for one of the child to terminate
if($child_pid)
{ # wait for children
my $terminated_id = wait;

# kill the still active child
if($terminated_id == $pid)
{ kill 9, $child_pid; }
else
{ kill 9, $pid ; }
}
else ## child that start the timer
{
print "2 start child $child_pid \n";
my ($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;

LOOP_ON_TIMER :
print "\nLOOOOOOOP
($luser_time,$lsys_time,$lcu_time,$lcs_time)\n\n";
($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;

goto LOOP_ON_TIMER if ($luser_time <= 2);
print "\t\t 2) child $pid exiting\n\n";
}
}
else # if first child, then fire the test case
{ print "1 start child $pid";
&run_test_case($i);
$flag =1;

}
}

Here parent forks two childeren,- One childeren is suppose to simulate
some system task ..
another child works as watch dog.

bothchilderen and paren should terminate once any of the child
terminates....

Can anyone suggest me better way to implement this ??

Thanks in advance ...
 
A

Anno Siegel

Monika Talwar said:
How can parent process control a child??
I tried to kill the child, but the code didn;t worked to my
expectation.

Actually i wrote the following code -

# fork first child
if(defined($pid= fork))
{
print "1) pare $pid \n\n";
if($pid) ## If parent process then fork another child to start
timer
{ # fork new child
my $child_pid = fork;

# if parent process then wait for one of the child to terminate
if($child_pid)
{ # wait for children
my $terminated_id = wait;

# kill the still active child
if($terminated_id == $pid)
{ kill 9, $child_pid; }
else
{ kill 9, $pid ; }
}

Signal 9 is a little radical. It should only be used when a process
can't be killed by other means. I'd use a TERM signal.
else ## child that start the timer
{
print "2 start child $child_pid \n";
my ($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;

LOOP_ON_TIMER :
print "\nLOOOOOOOP
($luser_time,$lsys_time,$lcu_time,$lcs_time)\n\n";
($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;

goto LOOP_ON_TIMER if ($luser_time <= 2);
print "\t\t 2) child $pid exiting\n\n";
}
}

I don't think this loop times what you want to time. You are waiting
for this child process to accumulate two seconds of CPU time. Depending
on system load, this could take any amount of real time from two seconds
upwards. I expect you actually want to wait for two seconds real time.

Also, what you have there is a busy loop -- the process will continuously
be actively querying its CPU time and produce system load.

To repair both, use the sleep() command (see perldoc -f sleep).
else # if first child, then fire the test case
{ print "1 start child $pid";
&run_test_case($i);
$flag =1;

}
}

Here parent forks two childeren,- One childeren is suppose to simulate
some system task ..
another child works as watch dog.

You don't need a separate process to establish a time limit for a
process. Simply set an alarm timer (perldoc -f alarm). It will
kill your process after a set time.

If you need to do things after the alarm timer fires, there is
"perldoc -q 'slow event'" which shows how to catch the timer
signal.
bothchilderen and paren should terminate once any of the child
terminates....

Can anyone suggest me better way to implement this ??

You don't need a separate process for the timeout. Set an alarm timer
in the working child process. Your use of "wait" to let the parent die
with the kid looks fine (I didn't run your progeam). It will be
simplified when there is only one process to wait for.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top