[comp.unix.wizards] semaphore question

stevens@hsi.UUCP (Richard Stevens) (04/28/88)

I have a question about the System 5 semaphores and their
initialization.

I have 2 processes that are started at about the same time.
They use a semaphore to guarantee exclusive access to a shared resource.
The first process executed must create the semaphore and initialize it -
here is where my problem is - I don't see an easy way to both create
a new semaphore and initialize it (atomically).  (Yes, I know that
there are other, better and easier ways to do the same thing.)

(1) The System 5 man pages, specifically semget(2), don't say anything
	about initialization of the semaphore value itself.  Neither does
	the Chapter on IPC in the System 5.3 Programmer's Manual.

(2) From a perusal of the system 5.2 source code it appears that
	semget() *doesn't* set the semaphore value when a new semaphore
	is created.  Whatever happens to be in that (random) memory
	location is what its initial value is, until you specifically
	set it with semctl(2).

(3) If you try a combination of system calls, you're going to have
	a race condition.

(4) Looking in some texts I have handy, other operating systems
	(Xinu and Tunis, for example) handle the creation of a
	semaphore as an operation that also initializes it.
	Why doesn't the System 5 implementation do this ??

(5) If you have a client/server situation where the server is the
	only process that can create the semaphore, it can then
	also initialize it, and all is OK.

Is there some trick that I'm missing ??

	Richard Stevens
	Health Systems International, New Haven, CT
		{ uunet | ihnp4 | yale } ! hsi ! stevens