0% found this document useful (0 votes)
23 views14 pages

CSC205 Group 16 Assignment.-1

Contains question on operating system scheduling

Uploaded by

danieola19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views14 pages

CSC205 Group 16 Assignment.-1

Contains question on operating system scheduling

Uploaded by

danieola19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

TOPIC: SOLVE THE READERS-WRITERS

PROBLEM USING MUTEX

GROUP NUMBER: 16

PROGRAMMING LANGUAGE: PYTHON

READERS-WRITERS PROBLEM

This is a classic synchronisation and concurrency

problem which involves shared resources to be accessed by

processes or threads. It consists of:

1) Readers: Processes that only read the shared resource and do

not modify it. Multiple readers can access the resource


simultaneously.

2) Writers: Modify the resource, which could lead to

inconsistencies if multiple writers or a mix of readers and

writers access it simultaneously.

The Readers-Writers problem requires curating a mechanism to

control when and how different processes can access the shared

resource. A mechanism that utilizes Mutual Exclusion (ensures

that only one process can access critical sections at a time).

This involves:

 Allowing multiple readers to access simultaneously (if no


writer is present).

 Ensuring that only one writer can access at any given time.

 Preventing readers from accessing while a writer is

modifying the data.

A solution is required in order to ensure data consistency and

integrity. Essentially having multiple readers and exclusive

writers and avoiding race conditions (this happens when

multiple threads or processes access and manipulate shared data

concurrently, and the final outcome depends on which thread

executes first at a crucial moment).


VARIATIONS OF READERS-WRITERS
PROBLEM

There are three main variations of the problem based on

different priorities:

1. First Readers-Writers Problem (Readers

Priority) – Allows multiple readers to read simultaneously but

ensures that a writer gets access only when no readers are

present.

Problem: Writers may experience starvation (if many

readers keep coming, the writer may never get a chance).


2. Second Readers-Writers Problem (Writers Priority) – Gives

priority to writers, ensuring that once a writer is waiting, no new

readers are allowed.

Problem: Readers may experience starvation (if many writers

keep coming, readers may not get a chance).

3. Third Readers-Writers Problem (Fairness Approach) –

Ensures neither readers nor writers starve by allowing them

access in the order of their arrival.

As student’s, we encounter the Readers-Writers Problem

in various real life situations where multiple people need access


to shared resources. Here is a practical application:

 Student Portals & Result Viewing (Moodle, Blackboard)

Readers: Students check their results, assignments, and

schedules.

Writers: Lecturers upload results or update

deadlines.

Issue: If a lecturer updates a grade while a student is

viewing, they might see incorrect data.

Solution: Systems use transaction locks to ensure

consistency.
SOLVING USING MUTEX

Mutex is used to control access to a single shared resource,

ensuring that only one thread or process can access it at any

given times. A practical analogy for mutex mechanism would be

to imagine playing with a special toy with a friend (shared

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

messy (like a race condition). So you need to take turns!

How Does a Mutex Work?

1. Locking: A thread or process attempts to acquire the


mutex lock by calling a locking function (e.g.,

`pthread_mutex_lock()`).

2. Blocking: If the mutex is already locked by another

thread or process, the calling thread or process is blocked

until the mutex is unlocked.

3. Unlocking: When the thread or process that holds the

mutex lock is finished with the shared resource, it

releases the lock by calling an unlocking function (e.g.,

`pthread_mutex_unlock()`).

4. Starvation prevention: To prevent starvation, where a

thread or process is perpetually denied access to the

mutex, some implementations use a fairness mechanism,

such as a FIFO (first-in, first-out) queue.

Types of Mutexes
1. Recursive mutex: Allows a thread or process to acquire the

same mutex multiple times without deadlocking.

2. Timed mutex: Allows a thread or process to acquire the

mutex with a specified timeout.

3. Spin mutex: Instead of blocking, a thread or process busy-

waits (spins) until the mutex is available.

SOLVING READERS WRITERS PROBLEM


USING MUTEX IN PYTHON
PROGRAMMING LANGUAGE

Python's implementation of mutex (mutual exclusion) locks

is based on the concept of synchronization primitives.

Python's threading module provides the Lock class, which

implements a mutex lock. The Lock class is based on the

POSIX Threads (pthreads) API.


import threading

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

# Create a ReadersWriters object


rw = ReadersWriters()

# Create reader and writer threads


reader_threads = []
for _ in range(5):
reader_threads.append(threading.Thread(target=rw.reader))

writer_threads = []
for _ in range(2):
writer_threads.append(threading.Thread(target=rw.writer))

# Start the threads


for thread in reader_threads + writer_threads:
thread.start()

# Wait for the threads to finish (optional, for demonstration)


for thread in reader_threads + writer_threads:
thread.join()

print("Program finished.")

By using a mutex lock to protect access to the shared resource,

Python ensures that multiple threads can safely access the

resource without data corruption or inconsistencies. Python's

syntax and nature make it ideal for rapid prototyping and


development, allowing you to quickly test and refine your

solution. Python's threading module provides a high-level

interface for working with threads, making it easy to create and

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

You might also like