CSC205 Group 16 Assignment.-1
CSC205 Group 16 Assignment.-1
GROUP NUMBER: 16
READERS-WRITERS PROBLEM
control when and how different processes can access the shared
This involves:
Ensuring that only one writer can access at any given time.
different priorities:
present.
schedules.
deadlines.
consistency.
SOLVING USING MUTEX
resource) but there is only one toy and you both want to play
with it at the same time. If you both try to grab it, it could get
`pthread_mutex_lock()`).
`pthread_mutex_unlock()`).
Types of Mutexes
1. Recursive mutex: Allows a thread or process to acquire the
class ReadersWriters:
def __init__(self):
self.lock = threading.Lock() # Protects shared variables
self.cond = threading.Condition(self.lock) # Condition
variable for signaling
self.writers_waiting = 0
self.readers = 0
def reader(self):
with self.cond: # Acquire the lock associated with the
condition
while self.writers_waiting > 0: # Wait if writers are
waiting
self.cond.wait() # Release lock and wait for signal
self.readers += 1
print("Reader reading data") # Read data
self.cond.notify_all() # Signal that a reader has finished.
Important to allow waiting writers to proceed.
def writer(self):
with self.cond: # Acquire the lock associated with the
condition
self.writers_waiting += 1 # Indicate a writer is waiting
while self.readers > 0: # Wait if readers are present
self.cond.wait() # Release lock and wait for signal
self.writers_waiting -= 1 # Decrement the waiting writer
count.
print("Writer writing data") # Write data
self.cond.notify_all() # Signal that the writer has
finished
writer_threads = []
for _ in range(2):
writer_threads.append(threading.Thread(target=rw.writer))
print("Program finished.")
manage threads.
GROUP MEMBERS
Joseph Bassey
Simileoluwa Abiodun-Ilori Nora
Okhai Merit Ohihon
Taiwo Daniel Onikoyi
Daniel Ogundare Ayomide
Balogun Modinat Mogbadunola
Shittu Adewunmi Mojeed
Salami Mubarak Joseph
Daniel Moyosore Hamzat
Alayode Tanitoluwa Annabelle
Adeoti Micheal Oluwapamilerin
Akinwamide Ayobami
Ogunkanmi Taiwo Emmanuel
Zubair Qudus
Ashade Elisabeth
Adetunji Daniel
Princess Adebukola