[OpenIndiana-discuss] shell script mutex locking or process signaling

Jim Klimov jimklimov at cos.ru
Fri May 31 02:36:45 UTC 2013


On 2013-05-30 16:15, Edward Ned Harvey (openindiana) wrote:
> This problem has two parts.  Atomicity of signaling operations (acquiring / releasing mutex, etc), and inter-process signaling.  (Let the later instance signal the earlier instance that it should die.)  It seems easy enough, as long as you have a good atomic operation for locking, the process that acquired lock can write its PID into a file, and later instances can "kill" that PID.

Well, you can use signalling ;)

You can use "trap" to set up a signal handler; this code would be
executed instantly if a signal is caught. You could invoke some
clean_exit procedure (made by you) to clean up working data and
exit with a non-error exit code (0), or you could set a variable
value instantly (i.e. to break out of a loop when its cycle is
completed).

Then your other invokation has to find the first one somehow
(i.e. "ps -ef|grep ..." or "svcs -p" parsing) and send the signal.
Usually you'd use SIGUSR1 and such for custom signals, and handle
the typical exit signals as exit ones (0 1 2 3 15). Note that the
signal 0 is a placeholder for shell exit when the script ends.
Also note that if you trap the exit signals, then you program does
not end instantly, but tries to end gracefully by its own definition.
You can end a script quickly with an uncatchable SIGKILL (9).

For example:

END_LOOP=0
trap "END_LOOP=1" 1 2 3 15
while [ "$END_LOOP" = 0 ]; do
...
done
trap "" 1 2 3 15

In your loop you might also decide to set END_LOOP=nonzero to end it.
The last line clears the handler and reverts to default ones.

HTH,
//Jim Klimov




More information about the OpenIndiana-discuss mailing list