• massive confusion on file locks

    From candycane@VERT/CONCHAOS to All on Thu Aug 10 12:01:46 2023
    Hi!
    Does anyone have any good recourses to learning how to make file locks in c? I've been trying to research it but there's not many recourses out there weirdly. I'm also very confused on the difference between flock(2) and fcntl locks.

    candycane

    ===
    user is generated from /dev/urandom

    ...By annihilating desires you annihilate the mind.
    ---
    ■ Synchronet ■ -=[ ConstructiveChaos BBS | conchaos.synchro.net ]=-
  • From Digital Man@VERT to candycane on Thu Aug 10 09:30:16 2023
    Re: massive confusion on file locks
    By: candycane to All on Thu Aug 10 2023 12:01 pm

    Hi!
    Does anyone have any good recourses to learning how to make file locks in c?

    File locks aren't really a "c" thing, but there is a POSIX standard and then there's the detailed differences in implementation on *BSD versus Linux versus Windows. For an example cross-platform abstraction/wrapper of different file and recording locking methods in C, see https://gitlab.synchro.net/main/sbbs/-/blob/master/src/xpdev/filewrap.c

    I've been trying to research it but there's not many recourses out there weirdly. I'm also very confused on the difference between flock(2) and fcntl locks.

    If you want to lock an *entire* file, use flock(). If you want to lock just a *region* of a file, use fcntl().
    --
    digital man (rob)

    Synchronet "Real Fact" #43:
    Synchronet added Baja/PCMS support with v2.00a (1994)
    Norco, CA WX: 66.8°F, 94.0% humidity, 5 mph SE wind, 0.00 inches rain/24hrs
    ---
    ■ Synchronet ■ Vertrauen ■ Home of Synchronet ■ [vert/cvs/bbs].synchro.net
  • From candycane@VERT/CONCHAOS to Digital Man on Thu Aug 10 14:36:35 2023
    Re: massive confusion on file locks
    By: Digital Man to candycane on Thu Aug 10 2023 09:30 am

    If you want to lock an *entire* file, use flock(). If you want to lock just a *region* of a file, use fcntl().

    So, whats the difference between LOCK_EX and SH? and does trying to obtain a lock already held suspend the program?

    candycane

    ===
    user is generated from /dev/urandom

    ...Fifty-eight per cent of all cars coming into Britain are imported.
    ---
    ■ Synchronet ■ -=[ ConstructiveChaos BBS | conchaos.synchro.net ]=-
  • From Digital Man@VERT to candycane on Thu Aug 10 13:23:37 2023
    Re: massive confusion on file locks
    By: candycane to Digital Man on Thu Aug 10 2023 02:36 pm

    Re: massive confusion on file locks
    By: Digital Man to candycane on Thu Aug 10 2023 09:30 am

    If you want to lock an *entire* file, use flock(). If you want to lock just a *region* of a file, use fcntl().

    So, whats the difference between LOCK_EX and SH? and does trying to obtain a lock already held suspend the program?

    Yes, that is what is meant by this line from the man page for flock():
    A call to flock() may block if an incompatible lock is held by another process. To make a nonblocking request, include
    LOCK_NB (by ORing) with any of the above operations.

    So by default, yes, it'll suspend the program if the file is already locked with an incompatible lock from what is being attempted.

    For more details, see "man 2 flock" or https://linux.die.net/man/2/flock
    --
    digital man (rob)

    Sling Blade quote #5:
    Karl Childers (to father): You ought not killed my little brother...
    Norco, CA WX: 76.2°F, 66.0% humidity, 0 mph SSW wind, 0.00 inches rain/24hrs ---
    ■ Synchronet ■ Vertrauen ■ Home of Synchronet ■ [vert/cvs/bbs].synchro.net
  • From candycane@VERT/CONCHAOS to Digital Man on Fri Aug 11 00:03:56 2023
    Re: massive confusion on file locks
    By: Digital Man to candycane on Thu Aug 10 2023 01:23 pm

    Yes, that is what is meant by this line from the man page for flock():
    A call to flock() may block if an incompatible lock is held by another process. To make a nonblocking request, include
    LOCK_NB (by ORing) with any of the above operations.

    Alright, sorry ^^". I've never worked with file locks before.

    candycane

    ===
    user is generated from /dev/urandom

    ...Success is a public affair. Failure is a private funeral.
    ---
    ■ Synchronet ■ -=[ ConstructiveChaos BBS | conchaos.synchro.net ]=-