[clug] Semaphores and shared memory

Martijn van Oosterhout kleptog at svana.org
Tue Dec 9 10:04:15 GMT 2003


On Tue, Dec 09, 2003 at 07:18:22PM +1100, Jepri wrote:
> I've got some chunky data files that I would like to access at the same 
> time from different programs.  I know that I can mmap the files into 
> different programs at the same time, but what's the right way (TM)  for 
> the programs to let each other know when they are updating the files?  
> Ideally I'd use mutex or something, but I'm not sure how to share these 
> between programs that do not have a common parent.  Do I have to resort 
> to some kind of formal IPC like SysV shared memory ?

Mutexes are always between programs, since locking against yourself is
pretty silly. Semaphores are (IIRC) a generalised version of mutexes in that
they can count a pool of resources, not just a single one. SysV semaphores
are identified by a global ID which you can either pick statically or have
one program generate a number and write it somewhere for the others to find.

man 5 ipc   has some useful info.

> And what are semaphores anyway?  Are they indexes into a kernel 
> semaphore table or what?

Basically, yes. Although they're not the same as the ones the kernel uses I
beleive. And you may want to look into futexes which are like mutexes but
don't require a trip to the kernel in most cases.

Hope this helps,
-- 
Martijn van Oosterhout   <kleptog at svana.org>   http://svana.org/kleptog/
> (... have gone from d-i being barely usable even by its developers
> anywhere, to being about 20% done. Sweet. And the last 80% usually takes
> 20% of the time, too, right?) -- Anthony Towns, debian-devel-announce
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://lists.samba.org/archive/linux/attachments/20031209/dfd21520/attachment.bin


More information about the linux mailing list