This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: Semaphores in libc
- From: Petar Bogdanovic <petar at smokva dot net>
- To: Luciano Rocha <strange at nsk dot no-ip dot org>
- Cc: "Paulo J. Matos" <pocmatos at gmail dot com>,Ryan Arnold <ryan dot arnold at gmail dot com>, Petr Baudis <pasky at suse dot cz>,libc-help at sourceware dot org
- Date: Mon, 4 Jan 2010 12:42:43 +0100
- Subject: Re: Semaphores in libc
- References: <20091231135523.GT24109@machine.or.cz><11b141710912310609x7959d12p27ee249db7af4916@mail.gmail.com><20091231141804.GU24109@machine.or.cz><11b141710912310625kde9782fo10841fe4d932c276@mail.gmail.com><20091231145017.GC30379@bit.office.eurotux.com><11b141710912310657j353d3effyd9a3a1edda1124f1@mail.gmail.com><ff4da150912310715n386c91b4q90be8e39e7b311da@mail.gmail.com><11b141710912310732t2eb9a60axc8249a0fe94e94be@mail.gmail.com><11b141710912310823q7bbcba93vd3ccdc523dda377f@mail.gmail.com><20091231163133.GA10198@bit.office.eurotux.com>
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