[cs631apue] Question about the semdemo.c

Jan Schaumann jschauma at stevens.edu
Sat Oct 29 12:24:13 EDT 2016

bzhang41 <bzhang41 at stevens.edu> wrote:
> In the semdemo.c
> #46 arg.val=1 is to set the each semaphore value of each sem_num to 1,
> however, the operation at #51 semop(semid,&sb,1)
> is to "free" the resource, so it will add sb.sem_op to semaphore value
> of each sem_num,
> So does it mean the total semaphore value of each sem_num is 2 ?

This program itself originates from
where a big chunk of the sample code was itself taken from W. Richard
Stevens's "UNIX Network Programming, Volume 2", but adjusted to run
stand-alone without Stevens's library (you can get all the code from
that book from here:

(I added this code to this class in 2012, it looks like, but it has been
around for quite some time before then.)

As so often, code found on the internet is not guaranteed to be correct,
and warrants some scrutiny.  (I really should make some time to rewrite
this example to be less confusing.)

As you may have noticed, even though 'arg.val' is set, it is never
actually applied, since the if-branch in which it is set does not call
semctl(2) with that value.

So the initial value will be 1 for each semaphor initialized via this
function, which allows it to be used (via the call to semop(2) in line
111, which leads to it being decremented, which then has the effect that
the next process trying to access the semaphore will block).

> Or at #46, the arg.val = 1 means set the "total" semaphore value of each
> sem_num to 1, but initially it is 0, so #51 semop(semid,&sb,1) is add
> 1 to 0, so it becomes 1 ???

The call to semop(2) in line 52 increments the value of the semaphor by
1.  As the initial value is 0, this effectively "frees" the semaphore,
as the semaphore semantics demand that it can only be used if > 0.

> #99 I don't get why use 'J' here, can you tell me answer ?

Any integer value will do.  I don't know why the original author of the
program chose 'J'; I might speculate that as the author of the tutorial
(from http://www.beej.us) goes by the online name 'beej', his middle
initial might be 'J'.

Since the tutorial is widely recognized and used, more and more websites
have copied that code, and even adjusted code has retained the 'J' value
to ftok(3):
https://www.google.com/search?q="ftok("semdemo.c"%2C 'J')"

This is another example of how code is copied frequently without any
understanding or consideration.

> Another question is, after run this program,
> I use ipcs -s, it shows semaphore arrays, so does this mean the array 
> still exist in the system ? And do we need to explicitly delete it ?


Semaphores and other SysV IPC structures allow asynchronous
communications on the same system, so need to remain available after a
program exits.  As a result, there is no way of knowing when these
resources might no longer be needed, and so the owner has to explicitly
delete them via ipcrm(1).


More information about the cs631apue mailing list