in reply to Re^3: File locking
in thread File locking

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^5: File locking
by Joost (Canon) on May 09, 2007 at 19:13 UTC
    I think you're confusing things. File locks in POSIX do not influence the ability to open files at all. File locks only influence the ability to create other locks.

    In other words, file locks are normally* cooperative, if some program doesn't try to lock a file, it can bypass all locks.

    In other words again, my sample code is correct; you open the file for append (or you can use some other non-destructive open) and lock after that (but before writing).

    If this doesn't help, please show some code or explain what you're trying to do, because right now it just looks like you don't know what file locks do and don't do on UNIX.

    * Update: some unix systems also provide mandatory locks. I'm ignoring those since a) they're not portable and b) I don't like mandatory locks.

Re^5: File locking
by kyle (Abbot) on May 09, 2007 at 14:22 UTC

    Normally when I've done this, merely opening the file (without modifying it) is not a problem. If you really want to avoid opening the file when someone else already has it open, you'd have to use a separate file to lock on. That lock file would be open in several processes at once but only locked in one. The one that has the lock would be allowed to open the file you're really interested in.

    That said, I don't see why this would be necessary. What concern do you have about having the file open in more than one process?

Re^5: File locking
by RL (Monk) on May 09, 2007 at 14:47 UTC

    It didn't work.

    What exactly did you do? (you may show us some code)

    What exactly happened that you consider *didn't work*?