[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is there any mutex/semaphore mechanism in shell scripts?
From: |
Greg Wooledge |
Subject: |
Re: Is there any mutex/semaphore mechanism in shell scripts? |
Date: |
Thu, 21 May 2009 10:51:52 -0400 |
User-agent: |
Mutt/1.4.2.2i |
On Wed, May 20, 2009 at 12:34:42PM -0700, jjjaime wrote:
> FUNCTION_1 &
> FUNCTION_2
> FUNCTION_3
>
> So, to speed up the execution of the script, I want FUNCTION_1 and
> FUNCTION_2 in parallel. But the script fails when FUNCTION_2 ends before
> FUNCTION_1.
Why?
> Is there any mechanism for synchronization (i.e. semaphores/mutes) or any
> other suggestion for simulating it?
You haven't really shown a need for it, but I'll assume your example is
simply misleading, and disregard it.
If what you're really asking for is a way to do mutual exclusion (mutexes)
then there are a few choices:
* mkdir is atomic, and you can use it for locking:
while ! mkdir $HOME/mylockdir; do
sleep 3
done
# Or something fancier.
Just be sure to remove it when you're done. (Set a 'trap', etc.)
* ln -s is also atomic and can be used similarly.
* There's "setlock" in daemontools (http://cr.yp.to/daemontools.html)
if you want third-party stuff.
If what you're really asking for is "How do I keep two jobs running in
the background at all times, starting another job when one finishes?"
then there are a couple recipes at
http://mywiki.wooledge.org/ProcessManagement#advanced
I mention this only because we get that question pretty often in #bash,
and since your example was not helpful, I'm not sure exactly what it is
you wanted.