■ Q: Reader and writers don't deadlock or livelock (safety)? ■ Q: Every reader or writers will eventually succeed (liveness)? ■ Q: Fairness: • Multiple readers can eventually read? • One writer can eventually write? ■ Yes, all readers can eventually read. ◉ No, if there are always at least one readers, the writer cannot write forever. ■ How to solve it? • Propose your solution in Assignment 2

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter17: Markov Chains
Section: Chapter Questions
Problem 12RP
icon
Related questions
Question

Given a shared data set, we allow multiple readers to read at the same time, and only one single writer can access the shared data at the same time. In the lecture slides, a solution is given. However, the problem is that the write cannot write forever, if there are always at least one reader.

How to ensure that the writer can eventually write? Propose your solution by using semaphores and implemented in Python

from threading import Thread, Semaphore
from time import sleep
from sys import stdout

class Reader(Thread):
def__init__(self, name):
self.n=name; Thread.__init__(self)
defrun(self):
globalnr, nw, dr, dw
whileTrue:
# ⟨await nw == 0 then nr += 1⟩
e.acquire()
ifnw>0:
#if nw > 0 or dw > 0 :
dr+=1; e.release(); r.acquire()
nr+=1
ifdr>0: dr-=1; r.release()
else: e.release()
# read data
stdout.write(self.n+' reading\n')
sleep(1)
# ⟨nr -= 1⟩
e.acquire()
nr-=1
ifnr==0anddw>0:
dw-=1 ; w.release()
else: e.release()

class Writer(Thread):
def__init__(self, name):
self.n=name; Thread.__init__(self)
defrun(self):
globalnr, nw, dr, dw
whileTrue:
# ⟨await nr == 0 and nw = 0 then nw += 1⟩
e.acquire()
ifnr>0ornw>0:
dw+=1; e.release(); w.acquire()
nw+=1
e.release()
# write data
stdout.write(self.n+' writing\n')
sleep(2)
# ⟨nw -= 1⟩
e.acquire()
nw-=1
ifdr>0: dr-=1; r.release()
elifdw>0: dw-=1; w.release()
else: e.release()
#if dw > 0: dw -= 1; w.release()
#elif dr > 0: dr -= 1; r.release()
#else: e.release()

e = Semaphore(1)
r, w = Semaphore(0), Semaphore(0)
nr, nw = 0, 0
dr, dw = 0, 0

r1 = Reader('R1'); r2 = Reader('R2')
w1 = Writer('W1'); w2 = Writer('W2')
r1.start(); r2.start(); w1.start(); w2.start() 

 

■ Q: Reader and writers don't deadlock or livelock (safety)?
■ Q: Every reader or writers will eventually succeed (liveness)?
■ Q: Fairness:
•
Multiple readers can eventually read?
•
One writer can eventually write?
■ Yes, all readers can eventually read.
◉
No, if there are always at least one readers, the writer cannot write
forever.
■ How to solve it?
•
Propose your solution in Assignment 2
Transcribed Image Text:■ Q: Reader and writers don't deadlock or livelock (safety)? ■ Q: Every reader or writers will eventually succeed (liveness)? ■ Q: Fairness: • Multiple readers can eventually read? • One writer can eventually write? ■ Yes, all readers can eventually read. ◉ No, if there are always at least one readers, the writer cannot write forever. ■ How to solve it? • Propose your solution in Assignment 2
Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:
9781337569798
Author:
ECKERT
Publisher:
CENGAGE L
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
A+ Guide To It Technical Support
A+ Guide To It Technical Support
Computer Science
ISBN:
9780357108291
Author:
ANDREWS, Jean.
Publisher:
Cengage,
Principles of Information Systems (MindTap Course…
Principles of Information Systems (MindTap Course…
Computer Science
ISBN:
9781285867168
Author:
Ralph Stair, George Reynolds
Publisher:
Cengage Learning