Date: Fri, 31 May 1996 20:08:37 GMT From: James Raynard <fqueries@jraynard.demon.co.uk> To: temerson@cyberpub.com Cc: questions@freebsd.org Subject: Re: IPC in release 2.0.5 Message-ID: <199605312008.UAA20005@jraynard.demon.co.uk> In-Reply-To: <31AF03C1.36A6@cyberpub.com> (message from Tom Emerson on Fri, 31 May 1996 07:35:45 -0700)
next in thread | previous in thread | raw e-mail | index | archive | help
> 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 <sys/types.h> #include <sys/ipc.h> 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 <sys/shm.h>; 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199605312008.UAA20005>