[plug] Killing process after time

Bernard Blackham bernard at blackham.com.au
Mon May 31 10:34:21 WST 2004


On Mon, May 31, 2004 at 01:17:49AM +0800, Leon Brooks wrote:
> On Sat, 29 May 2004 15:42, Tim White wrote:
> > needs to run a process that is killed after a
> > certain amount of time
> 
> This works. Note that the usleep is _needed_ else the child runs first 
> and usually completes before the parent gets to set up the signal 
> vector. I would do usleep(0) but it's not portable because some systems 
> return immediately on a sleep value of 0.

If you're going to eliminate race conditions, you really should do
it the correct way (the usleep doesn't _guarantee_ that the child
will start after the parent is ready). To do this you need to
synchronise both parent & child - easiest done using signals. eg:
  
>From the fork():
	- parent sleeps until child sends it a signal to say it's ready
	  to exec.
	- child does it's stuff to get ready, sends a signal to it's
	  parent and then sleeps until the parent sends it a signal to
	  go
	- parent receives the signal from the child, starts the timer
	  off and sends a signal back to the child to go.

If you want more concrete code, I can do it up in C or Perl.

The usleep will probably work in 99.9% of cases, but race conditions
like to occur in that last 0.1% of cases ... just being picky :)

Bernard.



More information about the plug mailing list