#Semaphores

1 messages · Page 1 of 1 (latest)

night oyster
#

This is more inclined towards general Operating Systems, and I'm not sure if and how it exists in Java, but I was learning about Semaphores.

In a book it says that if the signal operation is done on a semaphore, it awakes a thread(im paraphrasing)

If the semaphore has a value of -5, and a thread signals, a thread can wake up. But that doesn't make sense to me cause it would still remain in the while loop as its value is still <= 0(-4)

half yachtBOT
#

<@&987246964494204979> please have a look, thanks.

night oyster
#

The while loop i'm referring to is the one used in the wait operation

wait(S):
while S <= 0:
; // busy wait OR block
S = S - 1

signal(S):
S = S + 1

tight magnet
#

Are you sure you're not conflating two different ways of implementing a semaphore? In the pseudocode you gave, S can't be less than 0 but then you are supposing that S is -5. There is an alternative way to pseudocode a semaphore:

wait(S):
  S = S - 1
  if S < 0:
    block

signal(S):
  S = S + 1
  if S <= 0:
    unblock

In this pseudocode S can be -5.

half yachtBOT
#

@night oyster

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

night oyster
rapid halo
#

Here is the implementation

#

You might need to dig deeper to see how Sync and such are implemented