Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 6 . 1 4 ( Readers and Writers ) This exercise asks you to develop a Java monitor to solve a famous problem in
Readers and Writers This exercise asks you to develop a Java monitor to solve a famous problem in concurrency control. This problem was first discussed and solved by P J Courtois, F Heymans and D L Parnas in their research paper, Concurrent Control with Readers and Writers, Communications of the ACM, Vol. No October pp The interested student might also want to read C A R Hoares seminal research paper on monitors, Monitors: An Operating System Structuring Concept, Communications of the ACM, Vol. No October pp Corrigendum, Communications of the ACM, Vol. No February pThe Readers and Writers problem is discussed at length in Chapter of the authors book: Deitel, H M Operating Systems, Reading, MA: AddisonWesley,
a With multithreading, many threads can access shared objects; as we have seen, access to shared objects needs to be synchronized carefully to avoid corrupting the objects.
b Consider an airlinereservation system in which many clients are attempting to book seats on particular flights between particular cities. All of the information about flights and seats is stored in a common database in memory. The database consists of many entries, each representing a seat on a particular flight for a particular day between particular cities. In a typical airlinereservation scenario, the client will probe around in the database looking for the optimal flight to meet that clients needs. So a client may probe the database many times before deciding to book a particular flight. A seat that was available during this probing phase could easily be booked by someone else before the client has a chance to book it In that case, when the client attempts to make the reservation, the client will discover that the data has changed and the flight is no longer available.
c The client probing around the database is called a reader. The client attempting to book the flight is called a writer. Clearly, any number of readers can be probing the shared object at once, but each writer needs exclusive access to the shared object to prevent the object from being corrupted.
d Write a multithreaded Java program that launches multiple reader threads and multiple writer threads, each attempting to access a single reservation record. A writer thread has two possible transactions, makeReservation and cancelReservation. A reader has one possible transaction, queryReservation.
e First implement a version of your program that allows unsynchronized access to the reservation record. Show how the integrity of the database can be corrupted. Next implement a version of your program that uses Java monitor synchronization with wait and
notify to enforce a disciplined protocol for readers and writers accessing the shared reservation data. In particular, your program should allow multiple readers to access the shared object simultaneously when no writer is active. But if a writer is active, then no readers should be allowed to access the shared object.
f Be careful. This problem has many subtleties. For example, what happens when there are several active readers and a writer wants to write? If we allow a steady stream of readers to arrive and share the object, they could indefinitely postpone the writer who may become tired of waiting and take his or her business elsewhere To solve this problem, you might decide to favor writers over readers. But here, too, there is a trap, because a steady stream of writers could then indefinitely postpone the waiting readers, and they, too, might choose to take their business elsewhere! Implement your monitor with the following methods: startReading, which is called by any reader who wants to begin accessing a reservation; stopReading to be called by any reader who has finished reading a reservation; startWriting to be called by any writer who wants to make a reservation and stopWriting to be called by any writer who has finished making a reservation.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started