0% found this document useful (0 votes)
41 views

OS FirstAssignment

Mutex and semaphores both provide synchronization but differ - a mutex locks access to a resource while only one thread uses it, whereas a semaphore signals threads and multiple can access a resource. The critical section problem aims to ensure only one process accesses shared variables at a time through mutual exclusion, progress, and bounded waiting. Common synchronization problems like the dining philosophers, sleeping barbers, readers-writers, and producer-consumer problems demonstrate different concurrency challenges and solutions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

OS FirstAssignment

Mutex and semaphores both provide synchronization but differ - a mutex locks access to a resource while only one thread uses it, whereas a semaphore signals threads and multiple can access a resource. The critical section problem aims to ensure only one process accesses shared variables at a time through mutual exclusion, progress, and bounded waiting. Common synchronization problems like the dining philosophers, sleeping barbers, readers-writers, and producer-consumer problems demonstrate different concurrency challenges and solutions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Mutex vs Semaphore

Mutex and Semaphore both provide synchronization services but they are not the same. Details
about both Mutex and Semaphore are given below:
Mutex
Mutex is a mutual exclusion object that synchronizes access to a resource. It is created with a
unique name at the start of a program. The Mutex is a locking mechanism that makes sure only
one thread can acquire the Mutex at a time and enter the critical section. This thread only releases
the Mutex when it exits the critical section.
This is shown with the help of the following example:

wait (mutex);

…..

Critical Section

…..

signal (mutex);

A Mutex is different than a semaphore as it is a locking mechanism while a semaphore is a


signalling mechanism. A binary semaphore can be used as a Mutex but a Mutex can never be used
as a semaphore.
Semaphore
A semaphore is a signalling mechanism and a thread that is waiting on a semaphore can be signaled
by another thread. This is different than a mutex as the mutex can be signaled only by the thread
that called the wait function.
A semaphore uses two atomic operations, wait and signal for process synchronization.
The wait operation decrements the value of its argument S, if it is positive. If S is negative or zero,
then no operation is performed.

wait(S)

while (S<=0);

S--;

The signal operation increments the value of its argument S.


signal(S)

S++;

There are mainly two types of semaphores i.e. counting semaphores and binary semaphores.
Counting Semaphores are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count is the number
of available resources.
The binary semaphores are like counting semaphores but their value is restricted to 0 and 1. The
wait operation only works when the semaphore is 1 and the signal operation succeeds when
semaphore is 0.

Critical Section Problem


The critical section is a code segment where the shared variables can be accessed. An atomic action
is required in a critical section i.e. only one process can execute in its critical section at a time. All
the other processes have to wait to execute in their critical sections.
A diagram that demonstrates the critical section is as follows:
In the above diagram, the entry section handles the entry into the critical section. It acquires the
resources needed for execution by the process. The exit section handles the exit from the critical
section. It releases the resources and also informs the other processes that the critical section is
free.
Solution to the Critical Section Problem
The critical section problem needs a solution to synchronize the different processes. The solution
to the critical section problem must satisfy the following conditions:

1. Mutual Exclusion

Mutual exclusion implies that only one process can be inside the critical section at any
time. If any other processes require the critical section, they must wait until it is free.

2. Progress

Progress means that if a process is not using the critical section, then it should not stop any
other process from accessing it. In other words, any process can enter a critical section if it
is free.

3. Bounded Waiting

Bounded waiting means that each process must have a limited waiting time. Itt should not
wait endlessly to access the critical section.

Dining Philosopher’s problem


The dining philosopher’s problem states that there are 5 philosophers sharing a circular table and
they eat and think alternatively. There is a bowl of rice for each of the philosophers and 5
chopsticks. A philosopher needs both their right and left chopstick to eat. A hungry philosopher
may only eat if there are both chopsticks available.Otherwise a philosopher puts down their
chopstick and begin thinking again.
The dining philosopher is a classic synchronization problem as it demonstrates a large class of
concurrency control problems.

Sleeping Barber’s Algorithm


classic inter-process communication and synchronization problem between multiple operating
system processes. The problem is analogous to that of keeping a barber working when there are
customers, resting when there are none, and doing so in an orderly manner.
The analogy is based upon a hypothetical barber shop with one barber. The barber has one barber's
chair in a cutting room and a waiting room containing a number of chairs in it. When the barber
finishes cutting a customer's hair, he dismisses the customer and goes to the waiting room to see
if there are others waiting. If there are, he brings one of them back to the chair and cuts their hair.
If there are none, he returns to the chair and sleeps in it.
Each customer, when they arrive, looks to see what the barber is doing. If the barber is sleeping,
the customer wakes him up and sits in the cutting room chair. If the barber is cutting hair, the
customer stays in the waiting room. If there is a free chair in the waiting room, the customer sits
in it and waits their turn. If there is no free chair, the customer leaves.
Based on a naïve analysis, the above decisions should ensure that the shop functions correctly,
with the barber cutting the hair of anyone who arrives until there are no more customers, and then
sleeping until the next customer arrives. In practice, there are a number of problems that can occur
that are illustrative of general scheduling problems.
The problems are all related to the fact that the actions by both the barber and the customer
(checking the waiting room, entering the shop, taking a waiting room chair, etc.) all take an
unknown amount of time. For example, a customer may arrive and observe that the barber is
cutting hair, so he goes to the waiting room. While they're on their way, the barber finishes their
current haircut and goes to check the waiting room. Since there is no one there (the customer not
having arrived yet), he goes back to their chair and sleeps. The barber is now waiting for a
customer, but the customer is waiting for the barber. In another example, two customers may arrive
at the same time when there happens to be a single seat in the waiting room. They observe that the
barber is cutting hair, go to the waiting room, and both attempt to occupy the single chair.

Readers-Writers Algorithm
The readers-writers problem relates to an object such as a file that is shared between multiple
processes. Some of these processes are readers i.e. they only want to read the data from the object
and some of the processes are writers i.e. they want to write into the object.
The readers-writers problem is used to manage synchronization so that there are no problems with
the object data. For example - If two readers access the object at the same time there is no problem.
However if two writers or a reader and writer access the object at the same time, there may be
problems.
To solve this situation, a writer should get exclusive access to an object i.e. when a writer is
accessing the object, no reader or writer may access it. However, multiple readers can access the
object at the same time.

Consumer-Producer Algorithm
The consumer producer problem is a synchronization problem. There is a fixed size buffer and the
producer produces items and enters them into the buffer. The consumer removes the items from
the buffer and consumes them.
A producer should not produce items into the buffer when the consumer is consuming an item
from the buffer and vice versa. So the buffer should only be accessed by the producer or consumer
at a time.
The producer consumer problem can be resolved using semaphores. The codes for the producer
and consumer process are given as follows:
Producer Process
The code that defines the producer process is given below:

do {

. PRODUCE ITEM

wait(empty);

wait(mutex);

. PUT ITEM IN BUFFER

signal(mutex);

signal(full);

} while(1);

In the above code, mutex, empty and full are semaphores. Here mutex is initialized to 1, empty is
initialized to n (maximum size of the buffer) and full is initialized to 0.
The mutex semaphore ensures mutual exclusion. The empty and full semaphores count the number
of empty and full spaces in the buffer.
After the item is produced, wait operation is carried out on empty. This indicates that the empty
space in the buffer has decreased by 1. Then wait operation is carried out on mutex so that
consumer process cannot interfere.
After the item is put in the buffer, signal operation is carried out on mutex and full. The former
indicates that consumer process can now act and the latter shows that the buffer is full by 1.
Consumer Process
The code that defines the consumer process is given below:

do {

wait(full);

wait(mutex);

. .

. REMOVE ITEM FROM BUFFER

signal(mutex);

signal(empty);

. CONSUME ITEM

} while(1);

The wait operation is carried out on full. This indicates that items in the buffer have decreased by
1. Then wait operation is carried out on mutex so that producer process cannot interfere.
Then the item is removed from buffer. After that, signal operation is carried out on mutex and
empty. The former indicates that consumer process can now act and the latter shows that the empty
space in the buffer has increased by 1.
References:
https://www.tutorialspoint.com/mutex-vs-semaphore
https://www.tutorialspoint.com/critical-section-problem
https://www.tutorialspoint.com/dining-philosophers-problem-dpp
https://en.wikipedia.org/wiki/Sleeping_barber_problem
https://www.tutorialspoint.com/readers-writers-problem
https://www.tutorialspoint.com/producer-consumer-problem-using-semaphores

You might also like