From owner-freebsd-questions Fri May 31 16:16:23 1996 Return-Path: owner-questions Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id QAA20755 for questions-outgoing; Fri, 31 May 1996 16:16:23 -0700 (PDT) Received: from relay-4.mail.demon.net (relay-4.mail.demon.net [158.152.1.108]) by freefall.freebsd.org (8.7.5/8.7.3) with SMTP id QAA20696 for ; Fri, 31 May 1996 16:16:10 -0700 (PDT) Received: from post.demon.co.uk ([158.152.1.72]) by relay-4.mail.demon.net id aa10882; 31 May 96 23:15 GMT Received: from jraynard.demon.co.uk ([158.152.42.77]) by relay-3.mail.demon.net id aa13223; 1 Jun 96 0:14 +0100 Received: (from fqueries@localhost) by jraynard.demon.co.uk (8.7.5/8.6.12) id UAA20005; Fri, 31 May 1996 20:08:37 GMT Date: Fri, 31 May 1996 20:08:37 GMT Message-Id: <199605312008.UAA20005@jraynard.demon.co.uk> From: James Raynard To: temerson@cyberpub.com CC: questions@freebsd.org In-reply-to: <31AF03C1.36A6@cyberpub.com> (message from Tom Emerson on Fri, 31 May 1996 07:35:45 -0700) Subject: Re: IPC in release 2.0.5 Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk > First I build an array of semaphore operations to perform, then use: > /* operations can block; if successful return, then all were done > * all array operations tried in order in kernel "atomically" > */ > if( semop( id, sops, curops ) == FAILURE ) return( FAILURE ); > return SUCCESS; > to make the kernel call. I'm not too well up on SysV IPC, but don't you have to call semget() first? > Often this works correctly...whichever (client or server) will be put to > sleep by the kernel until the semaphore operation is freed and available > to the caller. When it seems to fail, the caller is given an error > code. Does the value of errno give any clues as to why it failed? > Also, I tried the ftok() call, to create a unique key. But the function > does not appear to be in any of the /usr/include/sys header files. You're right; it should be in /usr/include/sys/ipc.h, according to the man page, but isn't. To prototype it yourself, you need to do #include #include key_t ftok(const char *path, char id); and then link with -lcompat when you compile it. (Taken from the ftok() source in -current) Also, if you're using shared memory, you'll probably need to put this in somewhere as well:- #define SHM_R (IPC_R) #define SHM_W (IPC_W) (from -current's ; this is missing from the -release header). > Granted, I'm a version behind the current release, should I update our > systems? Or, perhaps I have an error in my coding? Please let me know > if you are confident that these semaphore calls do work correctly. As I understand it, the SysV IPC support in FreeBSD is mainly there for supporting existing programs which use it; there are IMHO better alternatives such as file locking and mapped memory. If you still want to use them, I'd recommend reading Chapter 3 of the following book. It has about 50 pages on SysV IPC, including a set of wrapper functions for semaphores which makes using them simpler and less prone to race conditions. Unix Network Processing W. Richard Stevens Prentice-Hall 1990 ISBN -013-949876-1 -- James Raynard, Edinburgh, Scotland jraynard@dial.pipex.com james@jraynard.demon.co.uk