Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Reader - Writers Problem of Threads with Locks / Semaphores The reader - writer problem is used to illustrate synchronization for threads that's are

The Reader-Writers Problem of Threads with Locks/Semaphores The reader-writer problem is used to illustrate synchronization for threads that's are reading
and writing to a shared file. Readers read files but do not update files, Writers read and update
files. In the reader-writers problem, multiple readers can read at the same time but only ONE
writer can be allowed to write to the file.
Whe
The reader-writer problem is used to illustrate synchronization for threads thats are reading
and writing to a shared file. Readers read files but do not update files, Writers read and update
files. In the reader-writers problem, multiple readers can read at the same time but only ONE
writer can be allowed to write to the file.
1. When 1 thread writes, no other threads can write.
2. When 1 thread writes, no other threads can read.
3. When 1 thread reads, no other threads can write.
4. When 1 thread reads, other threads can read ONLY.
Mutex Locks and Semaphores can be used to carry out the synchronization of readers and
writers in this problem.
Create your algorithm to execute the reader-writers problem with locks to control reading and
writing to ONE shared file.
There are 3 universal control variables
Mutex semaphore : ensure mutual exclusion when read_count is updated
Write_mutex: mutual exclusion lock for writers
Read_count : keep track of how many processes are currently reading object
The implementation be written in C or C++.
For your program, there should be at least 4 functions, the Check function is up to you to
implement whether separately or in the Read()/Write().
1. Wait()
- When a reader or writer needs to acquire the Mutex Semaphore or Write_mutex
- If a reader allowed to read, Read_Count as to be incremented
2. Signal()
- When a reader or writer finishes reading and writing, the Mutex Semaphore or
Write_mutex should be released
- If a reader finishes reading, Read_Count should be decremented
3. Read()
- If a reader wants to read, it has to check whether any writer is writing to the file.
If there are writers writing, readers are not allowed to read.
- If there are no writing done then readers can read. Before reading, readers have
to Wait() to increment Read_Count
4. Write()
- If a writer wants to write, it has to check whether any writer is writing to the file
or whether any readers are reading the file. If there are writers writing or readers
reading, the writer is not allowed to write.
- If there are no writing or reading done then writers can write. Before writing,
writers have to Wait() to use the Write_mutex.
5. Check()
- Check whether readers finish reading, Signal() to decrement Read_Count
- Check whether writers finish writing, Signal() to release Write_mutex.
Inputs:
- Read/ Write function
- Finish/ Not Finish writing
- Finish/ Not Finish Reading
Outputs:
- Allowed/ Not Allowed to Write
- Allowed/ Not Allowed to Read
- Any Writer writing
- Number of Readers reading
You are currently accessing A file.
Are you a Reader(r) or Writer(w)? Type e to exit. >>>w
*** You are a Writer. ***
Writing allowed.
write mutex on
----------------- running apain
Are you a Reader(r) or Writer(w)? Type e to exit. >>>r
*** You are a Reader. ***
Reading not allowed. Writer is writing.
Is the Writer done writing )?y
Writer done. Please proceed again
-------------------r running again
Are you a Reader(r) or Writer(w)? Type e to exit. >>r
**x You are a Reader. ******
Reading allowed.
mutex on
read count: 1
----------------- running again
Are you a Reader(r) or Writer(w)? Type e to exit. >>e
Process exited after 29.6 seconds with return value 0
Press any key to continue ...
Your program design do not need to be the same but should have proper notification and
warnings. Your output should also be able to show the state and response of the program.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions