Since there was no answer to this msg in comp.os.linux.developer.app I just
try it in here.
Hi, I have a few questions about semaphores.
First question: I want to know what layout the structs 'sem', 'sem_queue'
and 'sem_undo' have, but I can't them in any .h-file (/usr/include/*) and
what's the function of struct 'sem_queue'?
Second question: about the semop() call; the 2nd parameter is a pointer to
an array of 'sembuf'-s, while the last parameter is an int. Am I correct in
saying that the last param. selects a 'sembuf' out of the array and that
each 'sembuf' contains some sort of presetting operation?
Third question: maybe simple, but I have to know for sure; it's about the
semctl() call. If you use the command GETALL or SETALL, then the last
argument of this call is a pointer to an array which contains or will
contain the values of the semaphores. I assume this array must be as large
as there are semaphores in the set. So 2 semaphores in a set, means an array
of 2 elements. Correct?
Fourth and most important question: in my study book of IPC the following
pseudo code for p- and v-operations is mentioned:
p(sem)
{
if (sem != 0)
decrement sem by one
else
wait until sem becomes non-zero
v(sem)Quote:}
{
if(queue of waiting processes not empty)
restart first process in wait queue
else
increment sem by one
I thought that if every process handles the p- and v-operation correctlyQuote:}
(i.e. calling them), the semaphore value will eventually returns to its
initial value. So if the initial value was 2 and a few processes do some p-
and v-operations, the semaphore will at the end be 2 again. But, reading
further in the book I started to doubt if that was true. I found that when a
process makes a p-operation and ends up in the waiting queue, it decrements
the semaphore when it comes out the queue. Why it that?
And what's the goal of waiting until the semaphore becomes 0 with semop(). I
mean, if p-operations decrement the semaphore it may get smaller than 0. If
not all v-operations compensate/increment the semaphore, the semaphore could
grow more negative and testing for 0 will always fail.
Greetz, Arthur