This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Semaphores in libc


On Thu, Dec 31, 2009 at 04:31:33PM +0000, Luciano Rocha wrote:
>
> (...)
> 
> open("lockfile", O_CREAT | O_EXCL ...) also have problems, but the
> manual page has a workaround:
> 
>        O_EXCL  is  only  supported  on NFS when using NFSv3 or later on
>        kernel 2.6 or later.  In environments where NFS  O_EXCL  support
>        is not provided, programs that rely on it for performing locking
>        tasks will contain a race  condition.   Portable  programs  that
>        want  to  perform atomic file locking using a lockfile, and need
>        to avoid reliance on NFS support for O_EXCL, can create a unique
>        file  on  the same file system (e.g., incorporating hostname and
>        PID), and use link(2) to  make  a  link  to  the  lockfile.   If
>        link(2)  returns  0,  the  lock  is  successful.  Otherwise, use
>        stat(2) on the unique file  to  check  if  its  link  count  has
>        increased to 2, in which case the lock is also successful.

I have a hard time understanding the last sentence.  What exactly is the
purpose of stat(2) resp. checking the link count in that example?  Why
would one want to issue stat(2) after a failed link(2) and why is the
return of the former able to overturn the return of the latter?

		Petar Bogdanovic


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]