Upload
jacek-klos
View
213
Download
0
Embed Size (px)
Citation preview
8/13/2019 Presentation OSS 07-05-2013
1/20
Operating System Server
Dining Philosophers Problem using
semaphores
Emily Branagan, B00055105
Ciara Walsh, B00050867
Jacek Klos, B00053402Jose ernan!e" B00065734
#an$el %anche" B00065732
8/13/2019 Presentation OSS 07-05-2013
2/20
New and previous solution
Semaphore Solution is an evolution from
the Mutual Exclusion Locks Solution.
The main problem with the previous
solution was that If all five philosophers
pick up their left chopsticks at the same
time, they will not be able to take the
other one and a deadlockwill occur.
&o 'i( )ha) *ro+lem )his sema*hore
sol$)ion only allos )ha) 'o$r
*hiloso*hers si) !on a) )he )a+le-&o achie.e )ha) a sema*hore
ini)iali"e! )o 'o$r is $se!, each
*hiloso*her ai) on )his
sema*hore +e'ore he can *ick $*
cho*s)icks, an! e ha.e 'inishe!
ea)ing, he has )o signal )he
sema*hore )o release )he chair-
8/13/2019 Presentation OSS 07-05-2013
3/20
Semaphore is a synchronization tool.
Extension of the mutual locks solution but there aredifferences.
It involves the execution of two atomic solutions,
Wait! and Si"nal!.
Semaphore Solution
8/13/2019 Presentation OSS 07-05-2013
4/20
Execution
Wait!The counter is initialized to #$
Wait be"ins and the counter decreases, execution continues.
The counter is at %$
The thread is suspended and placed in a &ueue Si"nal!
'o waitin" threads$
The counter that was on % is now increased to #, the thread
starts the wait process a"ainThreads waitin"$
The counter must be on %, one thread is chosen to leave the
&ueue.
8/13/2019 Presentation OSS 07-05-2013
5/20
Advantages of Semaphores
There is no wastin" of resources, due to no busy waitin"
When a process is de(&ueued, it is done in the semaphoreimplementation They allow flexible resource mana"ement
They are machine independent
8/13/2019 Presentation OSS 07-05-2013
6/20
Disadvantages of Semaphores
Impractical when it comes to lar"e scale use
Semaphores use &ueues in its implementation, in thecase of )I)* if a hi"h priority process comes into the
&ueue it must wait while other low priority process
finish.
8/13/2019 Presentation OSS 07-05-2013
7/20
Philosopher code
/i'n!e' %E
/!e'ine %E
/incl$!e &hrea!Class-h
macro !e'ines
/!e'ine 9:#%E% 5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hiloso*her class !e'ini)ion;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
class hiloso*her< *$+lic &hrea!
=
public:
Philosopher(int Number, int iter); // builder
private:
int No; // position of the philosopher
int Iteration;
void ThreadFunc();//where the code of each philosopher will be
>?
/en!i'
8/13/2019 Presentation OSS 07-05-2013
8/20
Initialiing mutex loc!s and
semaphore
locks an! sema*hores
s)a)ic %ema*hore
o$rChairs@o$rChairs,9:#%E% ; 1A?
s)a)ic #$)e( Cho*s)ick1@Cho*s)ick1A?s)a)ic #$)e( Cho*s)ick2@Cho*s)ick2A?
s)a)ic #$)e( Cho*s)ick3@Cho*s)ick3A?
s)a)ic #$)e( Cho*s)ick4@Cho*s)ick4A?
s)a)ic #$)e( Cho*s)ick5@Cho*s)ick5A?
s)a)ic #$)e( Cho*s)ick9:#%E%D
!"hopstic#$, !"hopstic#%, !"hopstic#&, !"hopstic#',
!"hopstic# ;
8/13/2019 Presentation OSS 07-05-2013
9/20
"hread#unc$% &ode
Four"hairs*+ait(); // allows ' to sit down onl if s-. (semaphore00)
"hopstic#1No2034oc#();
"hopstic#1(No 5 $) 6 N789F9PI4
8/13/2019 Presentation OSS 07-05-2013
10/20
'ain Program
hiloso*her hiloso*hers9:#%E%D?
int i, Iteration;
.
-
// create all the philosopher threads and fire them up
for (i . ; i ? N789F9PI4
8/13/2019 Presentation OSS 07-05-2013
11/20
JN- join thread.SS- semaphore signal.SW- semaphore wait.
SE- semaphore enter.MW- Mutex wait.ML- Mutex lock.MU- Mutex unlock.
(egend about "hread'entor
8/13/2019 Presentation OSS 07-05-2013
12/20
'utex Solution &ode
Bela();
"hopstic#1No2034oc#();
"hopstic#1(No 5 $) 6 N789F9PI4
8/13/2019 Presentation OSS 07-05-2013
13/20
&ircular waiting example
8/13/2019 Presentation OSS 07-05-2013
14/20
&ircular waiting example
8/13/2019 Presentation OSS 07-05-2013
15/20
Solution to circular waiting
Four"hairs*+ait(); // allows ' to sit down onl if s-. (semaphore00)
"hopstic#1No2034oc#();
"hopstic#1(No 5 $) 6 N789F9PI4
8/13/2019 Presentation OSS 07-05-2013
16/20
Solution to &ircular )aiting
8/13/2019 Presentation OSS 07-05-2013
17/20
Solution to &ircular )aiting
8/13/2019 Presentation OSS 07-05-2013
18/20
Deadloc! and Starvation solution
*ow does it come to a deadloc! sytuation+
If two philosophers are really fast eatin" and thinkin", they
always be in control of the chopsticks, avoidin" the
others philosophers.
When all philosopers take one chopstick, and a waitin" for
the other one.
8/13/2019 Presentation OSS 07-05-2013
19/20
Deadloc! and Starvation solution
+ere is a typical eadlock -roblem
q ll thread are redq ll philosopher blockedq ll philospher waitin"
8/13/2019 Presentation OSS 07-05-2013
20/20
Deadloc! and Starvation solution
Solution to avoid the deadloc! problem
F /reatin" a semaphore waitin"! that allows four
philosophers at a table while blockin" the fifth. Thismake sure that one philosopher will have both
chopsticks.
F /reatin" the )I)* )irst In )irst *ut! &ueue with limitedeatin" time. This prevents from someone eatin" all the
time.