Minimizing chance of race conditions

Discussion in 'Perl Misc' started by Duke of Hazard, Nov 16, 2004.

  1. It is imperative that a section of my code is only executed by only
    one user at a time. So I have my program write a lock file when it
    reaches that section , then remove the lock file after it completes
    that section.

    This seemed to work ok, until I knew about race conditions. So I
    changed the code to write the lock file twice. First at the beginning
    of the script and second when it reaches the critical section of code
    that must be executed by only 1 user at a time.

    I figure this will significantly lower my probability of a race
    condition since two race conditions will need to occur simultaneously
    for it to break (which I am fine with, as long as it happens less than
    once per 1000 times).

    Is that right, or am I missing something?

    Thanks!
    Duke of Hazard, Nov 16, 2004
    #1
    1. Advertising

  2. Duke of Hazard wrote:
    [...]
    > I figure this will significantly lower my probability of a race
    > condition since two race conditions will need to occur simultaneously
    > for it to break (which I am fine with, as long as it happens less than
    > once per 1000 times).
    >
    > Is that right, or am I missing something?


    Why don't you use code that does not have a race condition?
    "perldoc -q lock"

    jue
    Jürgen Exner, Nov 16, 2004
    #2
    1. Advertising

  3. Also sprach Duke of Hazard:

    > It is imperative that a section of my code is only executed by only
    > one user at a time. So I have my program write a lock file when it
    > reaches that section , then remove the lock file after it completes
    > that section.
    >
    > This seemed to work ok, until I knew about race conditions. So I
    > changed the code to write the lock file twice. First at the beginning
    > of the script and second when it reaches the critical section of code
    > that must be executed by only 1 user at a time.
    >
    > I figure this will significantly lower my probability of a race
    > condition since two race conditions will need to occur simultaneously
    > for it to break (which I am fine with, as long as it happens less than
    > once per 1000 times).
    >
    > Is that right, or am I missing something?


    It's still wrong. Avoiding race conditions means that the chance of one
    occuring is zero. You merely lowered its probability. But if there is
    still a chance of a race condition, your code remains broken.

    You should only create one lockfile, but do it properly. It has to be an
    atomic operation:

    select undef, undef, undef, 0.5
    while ! sysopen LOCK, "file.lock", O_WRONLY|O_EXCL|O_CREAT;

    # once the program reaches this line, it succesfully acquired a
    # lock-file

    ...

    close LOCK;
    unlink "file.lock" or die "Something happened to the lock file: $!";

    The above, by the way, is reasonably portable.

    There's a number of FAQ entries dealing with locking. See

    perldoc -q lock

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Nov 16, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Weiss Family

    race conditions/pulse width

    The Weiss Family, Oct 16, 2004, in forum: VHDL
    Replies:
    6
    Views:
    701
    Jim Lewis
    Oct 19, 2004
  2. Taras_96
    Replies:
    7
    Views:
    6,613
    Taras_96
    Apr 5, 2005
  3. Replies:
    2
    Views:
    1,174
  4. mars
    Replies:
    6
    Views:
    344
    Laurent Pointal
    Feb 7, 2007
  5. Larry Bates

    Drop folder and race conditions

    Larry Bates, Oct 9, 2007, in forum: Python
    Replies:
    1
    Views:
    269
    Steven D'Aprano
    Oct 9, 2007
Loading...

Share This Page