here is what Iâm writing in the book. Let us image that we have the following two processes and one semaphore. [[[ | semaphore p1 p2 | semaphore := Semaphore new. p1 := [ semaphore wait. 'p1' crTrace ] fork. p2 := [ semaphore signal. 'p2' crTrace ] fork. ]]] - The first process is waiting on the semaphore. As soon as the semaphore will be signalled, ==p1== is signal also the semaphore. - The second process is just signalling the semaphore and printing. Now the question is to understand what is the generated trace which is ==p2== then ==p1==. Let us explain why. - ==p1== is scheduled and its execution starts to wait on the semaphore, so it is removed from the run queue of the scheduler and added to the waiting list of the semaphore. - ==p2== is scheduled and it signals the semaphore. The semaphore takes the first waiting process (==p1==) and reschedules it by adding it to the end of the suspended lists. ==p2== continues its execution. Let us slightly change the example to show how priorities are involved during the signalling process. [[[ | semaphore p1 p2 | semaphore := Semaphore new. p1 := [ semaphore wait. 'p1' crTrace ] forkAt: 30. p2 := [semaphore signal. 'p2' crTrace ] forkAt: 20. ]] As a conclusion when the semaphore is signalled, the first waiting process of the semaphore is removed from the semaphore list, and resumed following the preemption rules of the scheduler.
On 9 Jan 2020, at 13:01, ducasse <stepharo@netcourrier.com> wrote:
Hi
I wanted to explain
| semaphore p1 p2 | semaphore := Semaphore new. p1 := [ semaphore wait. 'p1' crTrace ] fork.
p2 := [semaphore signal. 'p2' crTrace ] fork.
displays p2 and p1. but I would like explain clearly but it depends on the semantics of signal.
- ==p1== is scheduled and its execution starts to wait on the semaphore, so it is removed from the run queue of the scheduler and added to the waiting list of the semaphore. - ==p2== is scheduled and it signals the semaphore. The semaphore takes the first waiting process (==p1==) and reschedule it by adding it to the end of the suspended lists.
Now this sentence "The semaphore takes the first waiting process (==p1==) and reschedule it by adding it to the end of the suspended lists.â is super naive. Is the semaphore signalling scheduled? or not?
signal "Primitive. Send a signal through the receiver. If one or more processes have been suspended trying to receive a signal, allow the first one to proceed. If no process is waiting, remember the excess signal. Essential. See Object documentation whatIsAPrimitive."
<primitive: 85> self primitiveFailed
"self isEmpty ifTrue: [excessSignals := excessSignals+1] ifFalse: [Processor resume: self removeFirstLink]"
I wanted to know what is really happening when a semaphore is signalled. Now resume: does not exist on Processor.
I will look in the VM code.
S
S.