Question

In: Computer Science

general semaphores

It should be possible to implement general semaphores using binary semaphores. We can use the operations semWaitB and semSignalB and two binary semaphores, delay and mutex. Consider the following:
void semWait(semaphore s)
{
semWaitB(mutex);
s--;
if (s < 0) {
semSignalB(mutex);
semWaitB(delay);
}
else SemsignalB(mutex);
}
void semSignal(semaphore s);
{
semWaitB(mutex);
s++;
if (s <= 0)
semSignalB(delay);
semSignalB(mutex);
}
Initially, s is set to the desired semaphore value. Each semWait operation decrements s, and each semSignal operation increments s. The binary semaphore mutex, which is initialized to 1, assures that there is mutual exclusion for the updating of s. The binary semaphore delay, which is initialized to 0, is used to block processes.
There is a flaw in the preceding program. Demonstrate the flaw and propose a change that will fix it.

Solutions

Expert Solution

Suppose two processes each call semWait(s) when s is initially 0, and after the first has just done semSignalB(mutex) but not done semWaitB(delay), the second call to semWait(s) proceeds to the same point. Because s = -2 and mutex is unlocked, if two other processes then successively execute their calls to semSignal(s) at that moment, they will each do semSignalB(delay), but the effect of the second semSignalB is not defined.

 

 

 

The solution is to move the else line, which appears just before the end line in semWait to just before the end line in semSignal. Thus, the last semSignalB(mutex) in semWait becomes unconditional and the semSignalB(mutex) in semSignal becomes conditional.


Thus, the last semSignalB(mutex) in semWait becomes unconditional and the semSignalB(mutex) in semSignal becomes conditional.

Related Solutions

semaphores
onsider the following definition of semaphores:void semWait(s){if (s.count > 0) {s.count--;}else {place this process in s.queue;block;}}void semSignal (s){if (there is at least one process blocked onsemaphore s) {remove a process P from s.queue;place process P on ready list;}elses.count++;}Compare this set of definitions with that of Figure 5.3. Note one difference: With the preceding definition, a semaphore can never take on a negative value. Is there any difference in the effect of the two sets of definitions when used in programs?...
What are Semaphores and Mutex? What are they used for?
What are Semaphores and Mutex? What are they used for?
The project will study the coordination of multiple threads using semaphores. The design should consist of...
The project will study the coordination of multiple threads using semaphores. The design should consist of two things: (1) a list of every semaphore, its purpose, and its initial value, and (2) pseudocode for each function. Code Your code should be nicely formatted with plenty of comments. The code should be easy to read, properly indented, employ good naming standards, good structure, and should correctly implement the design. Your code should match your pseudocode. Project Language/Platform This project must target...
Q3) Explain; how the synchronization mechanism (semaphores) can be used, to ensure a correct access to...
Q3) Explain; how the synchronization mechanism (semaphores) can be used, to ensure a correct access to exclusive resources.
Answer the following: a) What is a critical region? b) How can counting semaphores be used...
Answer the following: a) What is a critical region? b) How can counting semaphores be used to provide exclusive access to a critical region? c) Describe the operation (clearly, in English) of the semWait and semSignal actions of a counting semaphore.
can someome investigate my program that utilizes semaphores that can result in deadlock due to programming...
can someome investigate my program that utilizes semaphores that can result in deadlock due to programming errors and help in finding out if the solution meet all criteria for the critical section If yes, then comment code identifying the parts of code that do If no, could you help in fixing the code wherever given solution fails criteria in the code below #include <pthread.h> #include <semaphore.h> #include <stdio.h> #include <unistd.h> #define N 5 #define THINKING 2 #define HUNGRY 1 #define...
using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add...
using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add comments please) The barber shop has one barber (a thread), one barber chair, and n chairs for waiting customers (semaphore), if any, to sit on. If there are no customers (each customer is a thread) present, the barber sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while...
In C programming: Using mutexes, semaphores, and spinlocks, you can sequence memory operations to prevent race...
In C programming: Using mutexes, semaphores, and spinlocks, you can sequence memory operations to prevent race conditions. It might make some sense to just chuck spinlocks out the window and use mutexes. Explain why mutexes would tend to relieve you of some of the problems inherent in spinlocks. Explain a situation in which those benefits might not be worth your time and you’d go with the spinlock anyway.
Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In...
Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In this task, a character buffer should be created (e.g., char buf[500]) in the server which is shared among threads that are created to handle communication with clients. Specifically, when the server receives a message (a string) from a client, the server should (1) store the message in the buffer, (2) broadcast the message to all connected clients, and (3) clear the buffer. Make sure...
Summarize the general characteristics of a general partnership Summarize the general characteristics of a limited partnership...
Summarize the general characteristics of a general partnership Summarize the general characteristics of a limited partnership Bowen and Campbell are partners in operating a store. Without consulting Bowen, Campbell enters into a contract for the purchase of merchandise for the store. Bowen contends that he did not authorize the order and refuses to take delivery. The vendor sues the partners for the contract price of the merchandise. Will the partnership have to pay? Why? Does your answer differ if Bowen...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT