in reply to Disappearing File

Your attempt at making a semaphore file is not having any effect. The race condition caused by reading and writing separately goes like this:
Process AProcess B
1readdata()
2readdata()
3writedata()
4writedata()
Process B's work is overwritten.

You should open the file once to read and write, flock it (blocking), read, process, and write. Close releases the lock.

To use a semaphore file, you should do a blocking sysopen with O_EXCL, and unlink the file when you're done. There's no need to use both methods.

I think you also have a problem with autovivifying globals in your subroutines, then expecting later lexicals to appear in their place. use warnings; use strict;.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Disappearing File
by Gorby (Monk) on Jan 13, 2004 at 03:11 UTC
    Is there a chance for a race condition to occur in my code? Reading and writing of data happens between get_lock() and release_lock(). If this is not sufficient, what is lacking? I just want to understand the problem. Thanks in advance. Gorby

      Some platforms/perl versions don't support flock. Mod_perl will give you trouble with all those global variables and unlocalized handles - each connection will think it has the lock (I withdraw the "lexical" word). Your locking code will work on the right system under ideal conditions, but there are many things that can fail.

      You haven't shown your error logs and you don't say what platform, perl version, whether threading is in use, or whether different connections have a new environment.

      My advice was to simplify the locking and isolate variables' scope.

      After Compline,
      Zaxo