> GOAL: To create a program "timeread" with syntax:
>
> timeread <secs> <prompt>
>
> which returns the input value on stdout, or an exit value of 1 if
> timedout, or 2 if aborted by the user.
>
> METHOD:
>
> The current method I'm using is to spawn a process (background)
> which will sleep the appropriate amount of time, then kill the
> prompting process, which catches the signal and acts
> appropriately.
>
> The flip side is that if input is taken, then the spawned sleeper
> process is killed and the program exits with data on stdout.
>
> PROBLEM(S):
>
> 1. The original process can't kill the sleeper; sleep appears to
> ignore all signals until AFTER it is complete; GNU sleep does this
> too.
>
> 2. The original process can't exit until its children (i.e., the sleeper
> process) completes - it becomes (almost) a zombie.
>
> SOLUTION:
>
> Anyone know?

Take a look at my shell-script e-mail client, where I do pretty much what
you want, with a few extra features (look for the mail procedure):
http://lrp.steinkuehler.net/Packages/files/Utilities/grep

The mainline code fires off a couple sub-processes that acutally does the
work.  The main code then goes into a timeout loop (sleeps for 1 second,
increments a counter, checks to see if the children have finished).  If the
timeout counter ever expires, the mainline code kills all the (presumably
hung) children.  The child process can send a signals to the mainline code
to indicate it's still alive, but things are taking a while, or that a
problem has occured and we should promptly exit.

The only problem is the way I've got stuff setup, you have to wait for the
timeout loop to recognize the fact the child jobs fininshed (takes a second
or two) before processing continues.  Since this would probably be annoying
in an interactive environment, you might want to use the abort signal from
the clients to cause an immediate 'normal' exit instead of using 'exit 1',
like I did.

Multi-threaded programming with inter-process communication in shell
script...you gotta love it :)

Charles Steinkuehler
http://lrp.steinkuehler.net
http://c0wz.steinkuehler.net (lrp.c0wz.com mirror)


_______________________________________________
Leaf-devel mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/leaf-devel

Reply via email to