[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ale] Only one process



On 6/4/2010 7:16 PM, scott wrote:
> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512
>
>
> On Jun 4, 2010, at 8:49 PM, JK wrote:
>
>> Doesn't anybody worry about race conditions any more?

>
> when I write scripts that I dont want to run in parallel.  I check for a /var/run/*.pid file
> related to the script and if the PID in it isnt mine, then I abort out. if the file isnt there, I
> create it with my PID.  And when the script exits it removes the file.


You would appear to have a race condition if two instances
are started approximately simultaneously:

script 1:
   check for /var/myname.pid - not found

CONTEXT SWITCH

script 2:
   check for /var/myname.pid - not found

CONTEXT SWITCH

script 1:
   create /var/myname.pid
   write my pid

CONTEXT SWITCH

script 2:
   create /var/myname.pid
   write my pid

Script 1 is now happily running with the wrong pid in /var/myname.pid.
You really must have a "create-file-only-if-it-doesn't-already-exist"
operation in order to make this work, and flock(1) is the first
obvious thing I found that will do it.  I thought "touch" might have
some magic to do the trick, but no. (Disappointingly.)

-- JK


-- 
Forget Jesus: stars died so that you could be here today.
  - physicist Lawrence Krauss