From owner-freebsd-hackers@FreeBSD.ORG Thu Oct 22 21:47:35 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D4CF7106566C for ; Thu, 22 Oct 2009 21:47:35 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (relay04.stack.nl [IPv6:2001:610:1108:5010::107]) by mx1.freebsd.org (Postfix) with ESMTP id 9A5DD8FC0C for ; Thu, 22 Oct 2009 21:47:35 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id E72AE1DD645; Thu, 22 Oct 2009 23:47:33 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id D2E84228BE; Thu, 22 Oct 2009 23:47:33 +0200 (CEST) Date: Thu, 22 Oct 2009 23:47:33 +0200 From: Jilles Tjoelker To: Andrew Gallatin Message-ID: <20091022214733.GA32745@stack.nl> References: <4AE0BBAB.3040807@cs.duke.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4AE0BBAB.3040807@cs.duke.edu> User-Agent: Mutt/1.5.18 (2008-05-17) Cc: freebsd-hackers@freebsd.org, Christian Bell Subject: Re: semaphores between processes X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Oct 2009 21:47:35 -0000 On Thu, Oct 22, 2009 at 04:08:11PM -0400, Andrew Gallatin wrote: > We then moved on to posix semaphores. Using sem_wait/sem_post > with the sem_t residing in a shared page seems to work on > all 3 platforms. However, the FreeBSD (7-stable) man page > for sem_init(3) has this scary text regarding the pshared > value: > The sem_init() function initializes the unnamed semaphore pointed > to by > sem to have the value value. A non-zero value for pshared specifies a > shared semaphore that can be used by multiple processes, which this > implementation is not capable of. > Is this text obsolete? Or is my test just "getting lucky"? They work, but only if the processes are related and do not exec and the parent process initializes the semaphores before forking. This is because sem_t is a pointer to a malloc'ed structure. For process-shared semaphores this really only contains an identifier of the kernel semaphore. This also means process-shared semaphores are slower than in-process semaphores (libthr implements those using atomics and umtx so that system calls are only needed if a thread needs to sleep or be awakened). This is documented in comments in the source code, but not in man pages or other documentation. > Is there recommended way to do this? Apart from sysv semaphores, perhaps posix named semaphores (sem_open() etc). These require a 'kldload sem' on older versions though. -- Jilles Tjoelker