Os Presentation

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

SYNCHRONIZATION

Classical Problems of
Synchronization
Our Team

Qurat-ul-Ain
Muzamil Khan Touseef AQ
Fa-17/BSCS/019 Fa-17/BSCS/297
Fa-17/BSCS/127
Leader
Agenda

01 Process Synchronization

02 Three Classical Problems of Synchronization

03 Solution of Classical Problems


Process-
Synchronization
Let’s start with the first set of slides
Process-Synchronization 6

Sharing system resources by processes in


a such a way that, concurrent access to
shared data is handled thereby minimizing
the chance of inconsistent data.

Process 1 Process 3

Process 2
Classical problems of
Synchronization

Classical Problems

Bounded buffers The Readers Dining


Problem Writers Problem Philosophers
Problem
Want big impact?
Use big image.

8
Bounded-Buffer Problem
Also Known as producer/consumer problem

Participants

Producer Buffer Counter Consumer


Bounded-Buffer Problem
Without Synchronization
Also Known as producer/consumer problem

Produces

Producer Buffer
Bounded-Buffer Problem
Without Synchronization
Also Known as producer/consumer problem

Buffer
Producer Consumer
Bounded-Buffer Problem
Without Synchronization 2 2 10
(Preemptive)
Also Known as producer/consumer problem

Counter

Produces Consumes

Producer Buffer Consumer


Solution
For This
Problem?
Binary-Semaphore
Semaphores are binary integer
variables that are used to solve the
critical section problem by using two
atomic operations, wait and signal

Mutex = 1

CREDITS: This presentation template was created by Slidesgo,


including icons by Flaticon, and infographics & images by Freepik
How? Mutex --

Wait(empty) Signal(full)
Wait(Mutex) Signal(Mutex)

Mutex ==1 Mutex++


Mutex --

Counter Remainder Section


Mutex<=0 Critical Section

Infinite loop

Mutex = 1
Classical Problem 2:

The Dinning Philosopher’s Problem


Problem Scenario

5 philosophers sitting around a table


Spaghetti on the table
5 forks around the table
Each philosopher must ‘alternately’ think and eat
- A philosopher can only eat spaghetti when he has
both left and right forks
Each fork can be held by only 1 philosopher
After eating, he needs to put down both forks so they
become available to others
A philosopher can grab the fork on left or right as Dining Table
they become available → He can’t start eating until
he has both of them Each philosopher needs 2 forks to eat
Problem
How to design a Concurrent Algorithm such that each philosopher won’t
starve, i.e., he can forever continue to alternate between eating and
thinking assuming that any philosopher cannot know when others may

: want to eat or think.


M2

Each philosopher can pick up left fork before anyone


picks up their right fork.

?
M3
Now everyone is waiting for right fork.
y ?
Wh
M1

?
Deadlock

M0 M3
Dining Table
Solution?
M0 M1 M2 M3 M4

How? 1 1 1 1 1
Mutex --
Signal(Mi)
Wait(fork(Mi)
Wait(fork(Mi+1%N) Signal(Mi+1%5)

Mi-- EAT Mi++ Think

Remainder Section
Mi<=0 Critical Section

Infinite loop

Int M[5];
Problem 03:
Reader Writer's Problem
A file is to be shared among several concurrent
processes. Some of these processes, called readers,
may want only to read whereas others, called writers,  No reader will be kept waiting unless a writer has
may want to update the shared file. already obtained permission to use the shared
object

Reader Shared File Writer


 if a writer is waiting to access the object, no new
readers may start reading
A solution to either problem may result in starvation. In the first case,
writers may starve; in the second case, readers may starve.
Solution ?
Binary Semaphore
INT mutex , Wrt = 1

Wait(mutex) Wait(mutex)
Readcount++ Readcount--

Reading/W
Wait(wrt) Signal (wrt)
riting
If (Readcount ==1) If (Readcount ==0)

Critical Section
Wait(wrt) Signal (wrt)
Signal(mutex) Signal (mutex)
THANK YOU

You might also like