Semaphores

Let's say a semaphore(bm) has been put to wait or sleep, after P(bm). This means that in my case its sem_op=-1 and absolute value of sem_op > semval.
The current semval=0 and sem_op=-1. Process goes to sleep and semval remains 0.

The man page for semop shows this, when this condition happens:
semncnt (the counter of processes waiting for
this semaphore's value to increase) is incremented by one and the
process sleeps until one of the following occurs:

· semval becomes greater than or equal to the absolute value of
sem_op, at which time the value of semncnt is decremented, the abso‐
lute value of sem_op is subtracted from semval and, if SEM_UNDO is
specified for this operation, the system updates the process undo
count (semadj) for this semaphore.

· The semaphore set is removed from the system: semop() fails, with
errno set to EIDRM.

· The calling process catches a signal: the value of semncnt is decre‐
mented and semop() fails, with errno set to EINTR.

· The time limit specified by timeout in a semtimedop() call expires:
the system call fails, with errno set to EAGAIN.

Let's say that process is asleep and another process runs V(bm). This would set the sem_op=1 and modify bm.semval=0 to bm.semval=1; therefore, fulfilling the first condition from the man-page that would wake the other process up.

My questions are: If there is no context switch(process currently executing V(bm) doesn't change to process with bm waiting), the process with waiting bm won't change anything and would keep waiting?
Also,is it possible that if process that executed V(bm) keeps going and executes P(bm) and sets bm.semval=0 again, and then switches to process waiting it will find that process waiting doesn't fulfill the condition to stop waiting anymore and process will keep waiting?

I believe that if this conditions are true one process can starve another.
Thank you.
Last edited on
If there is no context switch(process currently executing V(bm) doesn't change to process with bm waiting), the process with waiting bm won't change anything and would keep waiting?

The state of the waiting process was changed from sleeping to scheduled to run. Nothing needs to change in the process itself, this is an OS scheduler status.

,is it possible that if process that executed V(bm) keeps going and executes P(bm) and sets bm.semval=0 again, and then switches to process waiting it will find that process waiting doesn't fulfill the condition to stop waiting anymore and process will keep waiting?

Yes, if "the condition to stop waiting" is some user-level code that the waiting process executes after returning from semop(). semop itself is atomic.
Last edited on
Thank you! :)
Topic archived. No new replies allowed.