in reply to Re: File Read
in thread File Read

I am not attempting to lock the file... I am locking a parallel process from executing one section from running in Parallel and forcing it to run in serial. In order to do that I needed to store a flag in a file that can be accessed by multiple running processesshowing whether a process is executing that section or not. Looping the other Processes until that section is free to run. Then the next process will lock the section and process and release the lock when finished. Thanks for everyone's help though. I found that I was able to use file access +>> and the truncate function to achieve what I needed.

Replies are listed 'Best First'.
Re^3: File Read
by Fletch (Bishop) on Mar 12, 2007 at 16:51 UTC

    The problem is you do need some sort of atomic locking. There's nothing preventing both your processes from simultaneously obtaining "not locked" results from the file.

    • Process A opens and reads the unlocked status
    • Process A gets put to sleep
    • Process B starts running
    • Process B opens and reads the unlocked status
    • B writes the locked status into the file and gets put to sleep
    • A writes the locked status into the file and starts on its way
    • B wakes up and hilarity ensues!
Re^3: File Read
by Anno (Deacon) on Mar 12, 2007 at 17:24 UTC
    I am not attempting to lock the file... I am locking a parallel process ...

    Right. What people are tying to tell you is, you should use file locking to do that. Holding state in a file without locking is inherently subject to race conditions. Instead of using the file content (0/1) to indicate a critical region in your process(es), lock a file (exclusively) and use the lock status directly. Locking is meant to do process serialization free of race conditions, if used correctly.

    The file content is irrelevant, though your situation it may be useful to write the PID to the file when a process gets a lock.

    Anno