Chon-Ji has asked for the wisdom of the Perl Monks concerning the following question:

I have a question regarding perl sysseek. I have a C program that writes/closes a file at random intervals. Then I have this perl script that gets the size (using sysseek) of the file also at random intervals. What would happen if I use sysseek to get the size while my C program is writing to it.

Replies are listed 'Best First'.
Re: Perl sysseek
by Fletch (Bishop) on Nov 17, 2006 at 14:35 UTC

    Depends. You may get the size before the write completes. You could get the size after the write completes. You shouldn't get an "in-between" number, as the individual write(2) system calls should be atomic; it's just the ordering of when they occur that's up in the air.

    Aside from the fact that you probably should be using stat, rather than seeking to EOF and tell presumably, you may get more germane answers if you state why you're doing this and what you think you're finding out.

Re: Perl sysseek
by kwaping (Priest) on Nov 17, 2006 at 15:59 UTC
    A possible solution to a problem you have implied but not stated is this: Have your C program create an empty "signal file" somewhere while it's writing to the output file. Then have your Perl code check for the existance of this file (-e $file) on startup. The Perl code can then take action based on whether this signal file exists or not - exit, wait for a bit and try again, proceed anyway, etc. Once the C program is done with its write, it should remove the signal file.

    ---
    It's all fine and dandy until someone has to look at the code.

      Race conditions. Using a file lock would be better.

      - tye