35.8k views
5 votes
thread 0 holds the lock (i.e., it has called sem wait() but not yet called sem post()), and another thread (thread 1, say) tries to enter the critical section by calling sem wait(). in this case thread 1 must wait (putting itself to sleep and relinquishing the processor)? select one: a. thread 1will find that the value of the semaphore is -1 b. thread 1will find that the value of the semaphore is 1 c. thread 1will find that the value of the semaphore is 0 d. thread 1will find that the value of the semaphore is 2

User David Cruz
by
6.9k points

1 Answer

3 votes

c. thread 1 will find that the value of the semaphore is 0

When thread 0 holds the lock, it has called sem_wait() which decrements the semaphore value by 1. This means that the semaphore value is now 0. If thread 1 tries to enter the critical section by calling sem_wait(), it will find that the semaphore value is 0, meaning that the resource is currently being used by another thread (i.e., thread 0) and thread 1 must wait until thread 0 releases the lock by calling sem_post() which increments the semaphore value. So, thread 1 will put itself to sleep and relinquish the processor.

A semaphore is a synchronization object that controls access to a common resource by multiple processes in a parallel programming environment. Semaphores can be implemented using a counter, which is initialized to a non-negative value. When a process wants to access the resource, it decrements the counter (sem_wait()), if the counter is zero, the process is blocked until another process increments the counter. When a process is done with the resource, it increments the counter (sem_post()). The counter value tells the state of the semaphore, if the value is 0 means the resource is being used, a positive value means the resource is available and negative value means there are more threads waiting for the resource.

User Impurity
by
8.1k points