Re: question on file locking



Roman Mashak wrote:
Hello, "Nils!
You wrote on 25 Feb 2006 21:37:30 +0100:

??>> Here is 'strace' dump (removed not-important lines):
??>>
??>> open("dump", O_RDWR) = 3
??>> fcntl64(3, F_SETLK, {type=F_WRLCK, whence=SEEK_SET, start=0, len=0}) =
??>> 0 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0 <here comes
??>> sleep()> ...
NOS> Good, now what about an strace of the second instance ? Or is this it
NOS> ?
Yes, that was second instance trace.
??>> Now compile and link code and run in first console and few seconds
??>> later in second. Second instance should've failed (as I assumed),
??>> because lock is already set on to file, and 'fcntl(fd, F_SETLK,
??>> &lock)' (where lock.l_type = F_WRLCK) should return -1.
NOS> I don't see why you think this should fail.
NOS> The first fcntl call in the code reads:
NOS> if (fcntl(fd, F_SETLKW, &lock) < 0) /* wait if locked */
NOS> return -1;

NOS> As the doc says F_SETLKW does not return with -1 if it can't get
NOS> the lock, it blocks/waits until it can aquire the lock.

NOS> The first instance gets the lock, and is then stuck in an infinte
NOS> loop, doing sleep calls. The lock is released when that process ends
NOS> (e.g. you kill it).

NOS> The second instance gets to that fcntl call. The fcntl call does
NOS> not return until it gets the lock on the file. This happens
NOS> when the first instance exits (is killed.)
The point is this never happens. As long as first instance runs in endless loop, I run second instance and it also comes into loop, no matter if I killed first process or not. Such behavior confuses me.

Ok, that is confusing. File a bugreport.
Though it'd be interresting if there is any difference if you set len to somthing larger than 0.
.



Relevant Pages

  • Re: question on file locking
    ... NOS> obtain the lock. ... NOS> You said both your instances obtained the lock. ... now what about an strace of the second instance? ... The first fcntl call in the code reads: ...
    (comp.unix.programmer)
  • Re: question on file locking
    ... NOS> Good, now what about an strace of the second instance? ... NOS> the lock, it blocks/waits until it can aquire the lock. ... NOS> The first instance gets the lock, and is then stuck in an infinte ...
    (comp.unix.programmer)
  • Re: question on file locking
    ... ??>> Actually, I expected that second instance fnctl() should return -1, ... NOS> obtain the lock. ... NOS> You said both your instances obtained the lock. ... Second instance should've failed, ...
    (comp.unix.programmer)
  • Re: question on file locking
    ... NOS> No - it will block on the fcntl call until it aquires the lock. ...
    (comp.unix.programmer)