Question

In: Computer Science

What are Semaphores and Mutex? What are they used for?

What are Semaphores and Mutex? What are they used for?

Solutions

Expert Solution

SEMAPHORE

Semaphore is simply a variable that is non-negative and shared between processes. A semaphore is a signaling mechanism, and a process that is waiting on a semaphore, can be signaled by another process so that at a time only one process can enter into critical section (shared resource) so that we can have mutual exclusion.

It uses two atomic operations:

1) Wait(P): decrement the value of semaphore

2) Signal(V): Increment the value of semaphore.

It is of two types :

1) Binary Semaphore: In this, semaphore have 0 and 1 value. 0 (zero) is when wait operation is used and 1 is when signal operation is used. Initially value of S is 1.

2) Counting Semaphore: In this, semaphore have non-negative integers (for eg: 0,1,2,3..) as a value.

Use of Semaphore

  • Non-binary semaphores are used in resource allocation. A semaphore might hold the count of the number of a particular resource.
  • If you have a pool of connections, such as a web browser might use, then an individual thread might reserve a member of the pool by waiting on the semaphore to get a connection, uses the connection, then releases the connection by releasing the semaphore.

MUTEX

  • The full form of Mutex is Mutual Exclusion Object. It is a special type of binary semaphore which also used for controlling access to the shared resource (critical section).
  • It includes a priority inheritance mechanism to avoid extended priority inversion problems. It allows current higher priority tasks to be kept in the blocked state for the shortest time possible.
  • However, priority inheritance does not correct priority- inversion but only minimizes its effect.

Use of Mutex

A mutex provides mutual exclusion, which can be either producer or consumer that can have the key (mutex) and proceed with their work. As long as producer fills buffer, the user needs to wait, and vice versa. In Mutex lock, all the time, only a single thread can work with the entire buffer.


Related Solutions

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...
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?...
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.
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
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.
Explain why it is necessary to use a mutex lock in conjunction with a condition variable.
Explain why it is necessary to use a mutex lock in conjunction with a condition variable.
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...
How could we use Pthreads to find the minimum value in an array without mutex locks?...
How could we use Pthreads to find the minimum value in an array without mutex locks? Would this version be slower? If so, why?
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...
Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and...
Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and condition variables. To implement the program, please followthe hints below. (a) The detailed 'Dining-Philosophers' problem statement: Refer to Page 71 ~ Page 72 in 'Lecture-08.pptx'    (b) The introduction to Pthreads mutex locks and condition variables: Refer to Page 17 ~ Page 39 in'Lecture-08.pptx'    (c) Creating five philosophers, each of which will run as a separate thread. Philosophers alternatebetween thinking and eating: The activities...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT