Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SYNCHRONIZATION
IN DINING
PHILOSOPHERS
PROBLEM
Team Members:
• Krishna Kumar -18BEC0468
• Swapnadeep Sarkar-18BEC0773
• Agnitra Paul -18BEC0915
• Ion Jestin Paul -18BME0039
• Rajesh S V -18BME0082
Slot: L19+L20
Submitted to:
Professor: Dr. Jagalingam Pushparaj
Abstract
Process synchronization plays an important role in maintaining the consistency of shared
data. Both the software and hardware solutions are present for handling critical section
problem. But hardware solutions for critical section problem are quite difficult to implement.
In this review, we will discuss two software-based solution to handle critical section problem
i.e. Semaphore and Mutex in Dining Philosophers Problem.
The basic difference between semaphore and mutex is that semaphore is a signalling
mechanism i.e. processes perform wait() and signal() operation to indicate whether they are
acquiring or releasing the resource, while Mutex is locking mechanism, the process has to
acquire the lock on mutex object if it wants to acquire the resource.
Introduction
Process Synchronization in Operating System
Process Synchronization is an approach to facilitate process that utilization the common
information. It means sharing framework assets by processes so that simultaneous access to
shared information is dealt with, consequently, limiting the opportunity of data clashing.
Basedonsynchronization, processes areclassified as oneof theaccompanying two sorts:
• Independent Process: Execution of one process does not influences the execution
of different processes.
• Cooperative Process: Execution of one process influences the execution of
different processes.
Process synchronization issue emerges on account of Cooperative process additionally in light of
the fact that assets are partaken in Cooperative processes.
Race Condition
A few processes access and process the controls over similar information simultaneously, at that
point the result depends upon the specific order in which the access takes place.
Critical Section Problem
Critical section is acode section that can be accessed by just one process at once. Critical section
contains shared variables which should be synchronized to keep up consistency of information
variables.
Intheentrysegment,theprocessdemandsforpassageintheCriticalSection. Any
answer for the critical section issue must fulfil three necessities:
▪ Mutual Exclusion: If a process is executing in its critical section, at that point no
different process is permitted to execute in the critical section.
▪ Progress:If no process is executing inthecritical sectionand different processes
are waiting outside the critical section, at that point just those processes that are
not executing in their remainder segment can take part in choosing which will
enter in the critical section next, and the determination can't be delayed
indefinitely.
▪ Bounded Waiting: A bound must exist on the occasions that different processes
are permitted to enter their critical section after a process has made a request to
enter its critical section and before that request is allowed.
Peterson's Solution
Peterson's Solution is a traditional programming-based answer for the critical
section issue.
In Peterson's answer, we have two common factors:
• Booleanflag[i]:Initialized to FALSE, atfirst nobodyiskeenonentering
critical section
• int turn: The process whose turn is to enter the critical section.
The wait() and signal() operation modify the value of the semaphore indivisibly. It means
that when a process is modifying the value of the semaphore, no other process can
simultaneously modify the value of the semaphore. Semaphore are distinguished by the
operating system in two categories Counting semaphores and Binary semaphore.
When a program starts it request the system to creates a mutex object for a given
resource. The system creates the mutex object with a unique name or ID. Whenever the
program thread wants to use the resource it occupies lock on mutex object, utilizes the
resource and after use, it releases the lock on mutex object. Then the next process is
allowed to acquire the lock on mutex object.
Meanwhile, a process has acquired the lock on mutex object no other thread/process can
access that resource. If the mutex object is already locked, the process desiring to acquire
the lock on mutex object has to wait and is queued up by the system till the mutex object
is unlocked.
Description Semaphore Mutex
int main()
{
int i,k;
void *msg;
double time_spent = 0.0;
clock_t begin = clock();
for(i=1;i<=5;i++)
{
k=pthread_mutex_init(&chopstick[i],NULL);
if(k==-1)
{
printf("\n Mutex initialization failed");
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_create(&philosopher[i],NULL,(void
*)func,(int *)i);
if(k!=0)
{
printf("\n Thread creation error \n");
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_join(philosopher[i],&msg);
if(k!=0)
{
printf("\n Thread join failed \n");
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_mutex_destroy(&chopstick[i]);
if(k!=0)
{
printf("\n Mutex Destroyed \n");
exit(1);
}
}
void *func(int n)
{
printf("\nPhilosopher %d is Thinking.",n);
pthread_mutex_lock(&chopstick[n]);
pthread_mutex_lock(&chopstick[(n+1)%5]);
printf("\nPhilosopher %d is Eating.",n);
sleep(3);
pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n+1)%5]);
printf("\nPhilosopher %d Finished Eating.",n);
}
Output
C Code Semaphore Only
#include<stdio.h>
#include<fcntl.h>
#include<semaphore.h>
#include<sys/wait.h>
#include<pthread.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
sem_t *sem[20];
int n;
sem[i]=sem_open(semname,O_CREAT|O_EXCL,0666,1);
if(sem[i]==SEM_FAILED)
perror("Unable to create semaphore");
}
for(i=0;i<n;i++)
{
cpid[i]=fork();
if(cpid[i]==0)
break;
}
if(i==n)
{
int status;
for(i=0;i<n;i++)
waitpid(cpid[i],&status,WUNTRACED);
for(i=0;i<n;i++)
{
sem_close(sem[i]);
sprintf(semname,"%d",getpid()+i);
sem_unlink(semname);
}
}
else
reader(i);
sleep(3);
clock_t end = clock();
time_spent += (double)(end - begin) /
CLOCKS_PER_SEC;
printf("\nTime elpased is %f seconds", time_spent);
return 0;
}
Output
Observation: Comparing Time Taken by
Semaphore vs Mutex
0.0014
0.0012
0.001
0.0008
0.0006
0.0004
0.0002
0
Trial 1 Trial 2 Trial 3 Trail 4 Trail 5 Trail 6 Trail 7 Trail 8 Trail 9 Trail 10
Semaphore Mutex
Conclusion
Hence from the above study it can be observed that the Dining Philosophers Problem
executes faster using Semaphores than compared to using Mutex.
Thus, it can be concluded that Semaphore is a better option in case there are multiple
instances of resources available and in the case of single shared resource Mutex is a better
choice.
References
1. https://techliebe.com/synchronization-in-various-operating-systems/
2. https://tutorialwing.com/process-synchronization-in-operating-system/
3. https://www.geeksforgeeks.org/process-synchronization-set-1/
4. www.csee.wvu.edu/~jdmooney/classes/CS550/notes/tech/mutex/Peterson.html
5. http://iit.qau.edu.pk/books/OS_8th_Edition.pdf
6. http://web.stanford.edu/class/cs140/cgi-bin/lecture.php?topic=locks
7. https://cseweb.ucsd.edu/classes/fa05/CSE120/lectures/120-l5.Pdf
8. https://en.wikipedia.org/wiki/Semaphore_(programming)
PLAGIARISM SCAN REPORT
Exclude URL:
95% 5%
Unique Plagiarized