Summary of Lecture 22 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Summary of Lecture 22

Description:

Summary of Lecture 22. Bakery Algorithm for ME. AND and NOT Synchronization ... Bakery Algorithm. v=ticket(S); await(E,v); CS; advance(E); or. await(E, ticket(S)); CS; ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 23
Provided by: kevin566
Category:

less

Transcript and Presenter's Notes

Title: Summary of Lecture 22


1
Summary of Lecture 22
  • Bakery Algorithm for ME
  • AND and NOT Synchronization
  • General Priority Problem

2
Bakery Algorithm
  • vticket(S)
  • await(E,v)
  • CS
  • advance(E)
  • or
  • await(E, ticket(S))
  • CS
  • advance(E)
  • How this algorithm can fail?

3
AND Synchronization
  • All events have to happen. Possible deadlock if
    acquired (reserved) in wrong order. Solution
  • Simultaneous P SP(S1, S2, ..., Sn)
  • if
    then for i 1 to n do SiSi-1 enddo
  • else put process into the first queue i with Si lt
    1 endif
  • SV(S1, S2, ..., Sn)
  • for i1 to n do if (queue i nonempty) then break
  • if igtn then for i1 to n do SiSi1 enddo
  • else awake a process at the head of queue i
    endif
  • Always awake at the beginning of for statement.
  • No reservations here, so starvation possible!

4
AND Synchronizationwith Event counters
Sequencers
  • An AND Condition with Eventcounters and
    Sequencers
  • var G, R, S record
  • T sequencer
  • E eventcount
  • end
  • procedure Pboth(R, S)
  • var r, s integer

Reservation of both resources, so no
starvation but deadlock is possible!
5
AND Implementation, continued
  • begin
  • - First lock the coordinated ticket generator
  • await (G.E, ticket(G.T))
  • - Get a coordinated set of tickets
  • r ticket(R.T)
  • s ticket(S.T)
  • advance (G.E)
  • - Now wait for both R and S to happen
  • await(R.E, r)
  • await(S.E, s)
  • end Pboth

6
Dining Philosophers Problem
  • FIVE PHILOSOPHERS (PROCESSES) alternate between
    thinking and eating
  • EATING requires two chopsticks (resources)
  • Each philosopher has assigned seat. There are 5
    chopsticks between seats on the round table.
  • With reservations, avoiding deadlock
  • all five have one chopstick
  • If no reservation, avoiding starvation
  • Phils 1 and 3 cooperate to starve 2
  • Other solutions breaking symmetry,
  • or a butler not letting the 5th eater in.

seat
1
chopstick
5
5
1
4
2
2
3
4
3
7
Dining Philosophers Problem SP Solution
  • Shared variables
  • var chopstick array0..4 of semaphore (1, 1,
    1, 1, 1)
  • Process i
  • loopforever
  • Think
  • SP(chopstick (i 1) mod 5, chopsticki)
  • Eat
  • SV(chopstick (i 1) mod 5, chopsticki)
  • endloop

Starvation possible!
8
NOT Synchronization
  • if
    then
  • for i 1 to n do Si Si - di enddo
  • else
  • place in the first queue i for which Si lt ti
  • endif
  • Re-examine all conditions on release.
  • Has event E happened ? initial value
  • Si 1 indicates false (has not happened yet)
  • SP (Si , 0, 1) V changes it to true
  • SP (Si , 1, 0) each process can check for false
  • V(Si) resets to false

9
Reader-Writer ProblemA Strong Reader Preference
Solution
  • Shared Variables
  • var L semaphore R / number of readers /
  • mx semaphore 1
  • Reader Writer
  • loopforever loopforever
  • ... ...
  • SP(L,1,1)
  • SP(mx,1,0) SP(mx,1,1 L,R,0)
  • Perform read Perform write
  • SV(L, 1) SV(mx, 1)
  • ... ...
  • endloop endloop

10
Reader-Writer ProblemA Writer Preference Solution
  • Shared Variables
  • var NW semaphore W / number of writers /
  • NR semaphore R / number of readers /
  • mx semaphore 1

Reader loopforever ... SP(NR,1,1
NW,W,0) Perform read SV (NR,1) ... endloop
Writer loopforever ... SP(NW,1,1)
SP(mx,1,1NR,R,0) Perform write SV (mx,1
NW,1) ... endloop
11
General Priority ProblemArray of Semaphores
Solution
There are n priorities, numbered from 1 to n,
with 1 being the highest one and a single
resource.
  • Shared variables var r semaphore 1
  • s array1..n of semaphore (1,1, ..., 1)
  • Process with priority i
  • loopforever
  • SP(si, 1, 1)
  • SP(r, 1, 1 s1, 1, 0 ... si - 1, 1, 0)
  • Use resource
  • SV(r,1 si, 1)
  • ...
  • endloop

12
General Priority ProblemSP Solution
  • We assume single process per priority
  • Why gt1 does not work? Hint consider the first SP
  • Shared variables var r semaphore 1
  • s simultaneous semaphore 2n
  • Process of priority i
  • Loopforever
  • SP(s,1, 2i-1)
    Example n3
  • SP(s, 2n1-2i,0 r,1,1) (i) processes
    1,2 arrive-gts5,
  • Use resource process 3
    can pass and blocks 1,2
  • SV(r,1 s, 2i-1) (ii)
    processes 1,3 arrive-gts3
  • endloop
    process 2 is blocked as is 1

13
General Priority ProblemSP Solution (gt1 process
per priority)
  • Shared variables var r semaphore 1 me
    semaphore1
  • s simultaneous semaphore 2n
  • Present1..n0,0,0 integer
  • Process of priority i
  • Loopforever
  • P(me) Presenti if Presenti1 SP(s,1,
    2i-1) V(me)
  • SP(s, 2n1-2i,0 r,1,1)
  • Use resource
  • P(me) Presenti-- if Presenti0 SV(r,1 s,
    2i-1) V(me)
  • endloop

14
Sample Synchronization Problem
Consider a system with the memory control that
limits the number of processes in memory to MPL,
and with dltMPL disks, pltMPL processors and
ngtMPLd terminals. There are n processes, each
executing the following program loopforever
Think at the terminal for some time Request
disk for reading in First-In-First-Out (FIFO)
order Request memory in FIFO order Read initial
data from disk and release disk Request
processor in Last-In-First-Out (LIFO)
order Process initial data and produce the
results in memory Release processor Request
disk for writing in FIFO order Write results and
release disk Release memory endloop
15
Questions for Synchronization Problem
Question 1 Show how deadlock can arise in the
system and modify the program for a process to
avoid deadlock. Question 2 Starvation for
which resource (terminal, memory, disk or
processor) is possible? Question 3 Write
synchronization for this program using semaphores

16
Answers to Questions 1, 2
Answer to Question 1 When MPL processes are in
the memory, and d new processes gets all disks,
none of the processes in the memory can release
it and none of the processes holding disks can
proceed. To avoid this situation, the last disk
cannot be allocated to a new processes if there
is no memory available. Another solution, simpler
is to reverse order of lines 2 and 3 in the
program. Answer to Question 2 Because of LIFO
order, processes waiting for processors may
starve if there is a constant inflow of new
processes.
17
Solution to Question 3 Semaphores
Semaphores MemMPL, diskd, lifon1, procd,
me1 integer nonactiven1 loopforever Think
at the terminal for some time P(Mem) / request
memory (FIFO order) / P(disk) / request
disk for reading in FIFO order / V(Disk) /
read initial data from disk and release disk
/ / request processor in LIFO order
/ SP(lifo,0,1 me,1,1) nonactive--
localranknonactive V(me) SP(lifo,localrank,0
proc,1,1 me,1,1) nonactive V(me) Process
initial data and produce the results in memory
V(lifo) V(proc) / release processor
/ P(disk) / request disk for writing in
FIFO order / Write the results V(disk) /
release disk / V(Mem) / release memory /
endloop
18
Problem 1 Synchronization
  • Consider a system which consists of a server and
    several producer and consumer
  • processes. They cooperate via a buffer of N
    objects numbered from 1 to N, Buffer1..N.
  • Each type of process does the following
  • A producer
  • loopforever
  • get access to the buffer
  • select the first free object from the
    bottom of the buffer
  • assign the value to this object
  • release the buffer for other
    producers/server
  • process locally
  • endloop
  • 2. A consumer
  • loopforever
  • get access to the buffer
  • select the last full pair of objects from
    the top of the buffer
  • copy the values of these objects
  • release the buffer for other
    consumers/server
  • process locally
  • endloop

19
Problem 1, Synchronization, continued
3. A server loopforever get access to the
buffer select the last full object from the
bottom of the buffer copy the value of this
object release the buffer for other
producers/server process data locally
get access to the buffer select the first
free pair of objects from the top of the buffer
assign the values to these objects release
the buffer for other consumers/server
process locally endloop Note that the buffer
is accessed from both sides, so order of
processing is LIFO (Last In First Out).
Emptied by the server Filled by the
server
Bottom
Top
Filled by producers Empty Emptied by
consumers
20
Problem 1, Synchronization-final
Write synchronization for this system, by augment
the programs for each process with proper
synchronization statements, so that (i) no
producer will proceed with accessing the buffer
unless at least three free objects exists (so the
server can process a producer filled
object), (ii) no consumer will proceed with
accessing the buffer unless there is a pair of
server filled objects, (iii) a server is not
able to proceed with getting the producer filled
object if there is none, (iv) a server is not
able to proceed with assigning a pair of objects
if no free pair of objects exists, (v) no
producer can proceed with releasing its value
filled objects unless all producers that started
accessing the buffer earlier released their
objects first, (vi) consumers cannot access the
buffer when the server is accessing it to assign
values to objects. Write your synchronization
using (a) simultaneous semaphores, (b)
eventcounters and sequencers.
21
Problem 1, Solution
We will use the following semaphores Empty to
count empty objects, MB for mutual exclusion on
a bottom part of the buffer, MT for the mutual
exclusion on the top part of the buffer, BFilled
for the filled objects at the bottom, and
TFilled for the filled objects at the top. The
initial values for them are clear EmptyN,
MBMT1, BFilledTFilled0. We will also use
indexes, Bottom0, TopN, that point to the
first empty object at the bottom/top
respectively. A producer does the
following SP(Empty,3,1 MB,1,1)
object_indexBottom Bottomload
buffer SV(BFilled,1 MB,1) A consumer does
the following SP(TFilled,1,1 MT,1,1)
object_indexTop Top2 read buffer SV(Empty,2
MT,1)
22
Problem 1, Solution continued
The server does the following SP(BFilled,1,1
MB,1,1) object_indexBottom Bottom--load
buffer SV(Empty,1 MB,1)
process locally SP(Empty,2,2 MT,1,1)
object_indexTop Top-2 fill buffer SV(TFilled
,1 MT,1) For a rewrite we are using
eventcounters for each semaphore note that in
fact SP is not necessary, sequence of P's and V's
would work in this case but care must be taken to
make sure that producer and server use Empty
eventcounter with proper value in comparison with
the ticket. Pair of Et tickets in S must be
coordinated, so ME is used P await(MEt,
ticket(MEtt)), ltticket(Et) advance(Met)
await(Empty, lt4-N) await(MB,ticket(MBt))
. advance(Bfilled) advance(MB) C await(TFille
d, ticket(Tfilledt)) await(MT,ticket(MTt))
advance(Empty) advance(Empty)
advance(MT) S await(BFilled, ticket(Bfilledt))
await(MB,ticket(MBt))
advance(Empty) advance(MB) process locally
await(MEt, ticket(MEtt)) ticket(Et)
ltticket(Et) advance(Met) await(Empty,lt1-N)
await(MT, ticket(MTt)) advance(Tfilled)
advance(MT)
Write a Comment
User Comments (0)
About PowerShow.com