Professional Documents
Culture Documents
Experiment 6: Implement Semaphore
Experiment 6: Implement Semaphore
Experiment 6: Implement Semaphore
Theory:
A deadlock happens in operating system when two or more processes need some resource to
complete their execution that is held by the other process.
In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly
process 2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in deadlock as
each of them needs the other’s resource to complete their execution but neither of them is willing
to relinquish their resources.
Coffman Conditions:
A deadlock occurs if the four Coffman conditions hold true. But these conditions are not mutually
exclusive.
• Mutual Exclusion: There should be a resource that can only be held by one process at a time.
In the diagram below, there is a single instance of Resource 1 and it is held by Process 1 only.
• Hold and Wait: A process can hold multiple resources and still request more resources from
other processes which are holding them. In the diagram given below, Process 2 holds Resource 2 and
Resource 3 and is requesting the Resource 1 which is held by Process 1.
• Circular Wait: A process is waiting for the resource held by the second process, which is
waiting for the resource held by the third process and so on, till the last process is waiting for a
resource held by the first process. This forms a circular chain. For example: Process 1 is allocated
Resource2 and it is requesting Resource 1. Similarly, Process 2 is allocated Resource 1 and it is
requesting Resource 2. This forms a circular wait loop.
Semaphore was proposed by Dijkstra in 1965 which is a very significant technique to manage
concurrent processes by using a simple integer value, which is known as a semaphore. This variable
is used to solve the critical section problem and to achieve process synchronization in the
multiprocessing environment.
1. Binary Semaphore – This is also known as mutex lock. It can have only two values – 0 and 1.
Its value is initialized to 1. It is used to implement the solution of critical section problem with
multiple processes.
2. Counting Semaphore – Its value can range over an unrestricted domain. It is used to control
access to a resource that has multiple instances.
Reader-writer problem:
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.
Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
/*
This program provides a possible solution for first readers writers problem using mutex and
semaphore.
I have used 10 readers and 5 producers to demonstrate the solution. You can always play with these
values.
*/
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
sem_wait(&wrt);
cnt = cnt*2;
sem_post(&wrt);
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt); // If this id the first reader, then it will block the writer
pthread_mutex_unlock(&mutex);
// Reading Section
pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0) {
pthread_mutex_unlock(&mutex);
int main()
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the producer and consumer
pthread_join(read[i], NULL);
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
Output:
Conclusion:
In this experiment, we learnt the concept of deadlock and how to avoid it using semaphores. We
also implemented the semaphores in C language to solve the reader writer problem.