OS FirstAssignment
OS FirstAssignment
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);
wait(S)
while (S<=0);
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.
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.
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);
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);
. .
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