(Readers and Writers) Consider an airline reservation system in which many clien
ID: 3637238 • Letter: #
Question
(Readers and Writers) Consider an airline reservation system inwhich 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 airline reservation scenario, the client will probe around in the database looking for the "optimal" flight to meet that client's needs. So a client may probe the database many times before deciding to try and 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 after deciding on 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.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 shared data at once, but each writer needs exclusive access to the shared data to prevent the data from being corrupted.
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.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 thread synchronization with Locks 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 data
simultaneously when no writer is active. 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 data, 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! Explain how you solve these problems.
Make sure that you design a good interface to demonstrate what is going on during the
program run. Provide enough sample outputs.
in java code ..
Explanation / Answer
// Uses readers and writers in a synchronized airline // reservation system. Synchronization scheme allows for // multiple readers. When a writer is active, nobody can access // the data. using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace thread_proj { class Synchronized { static void Main(string[] args) { // create shared object used by threads Seat seat = new Seat(300, Seat.SeatType.WINDOW); // random object used by each thread Random randomObject = new Random(); // create reader and writer objects Reader reader1 = new Reader(seat, randomObject); Reader reader2 = new Reader(seat, randomObject); Reader reader3 = new Reader(seat, randomObject); Reader reader4 = new Reader(seat, randomObject); Reader reader5 = new Reader(seat, randomObject); Writer writer1 = new Writer(seat, randomObject); Writer writer2 = new Writer(seat, randomObject); Writer writer3 = new Writer(seat, randomObject); // create threads for each writer and reader // set delegates for each thread Thread readerThread1 = new Thread( new ThreadStart(reader1.QueryReservation)); Thread readerThread2 = new Thread( new ThreadStart(reader2.QueryReservation)); Thread readerThread3 = new Thread( new ThreadStart(reader3.QueryReservation)); Thread readerThread4 = new Thread( new ThreadStart(reader4.QueryReservation)); Thread readerThread5 = new Thread( new ThreadStart(reader5.QueryReservation)); Thread writerThread1 = new Thread( new ThreadStart(writer1.MakeReservation)); Thread writerThread2 = new Thread( new ThreadStart(writer2.CancelReservation)); Thread writerThread3 = new Thread( new ThreadStart(writer3.MakeReservation)); // name each thread readerThread1.Name = "Reader1"; readerThread2.Name = "Reader2"; readerThread3.Name = "Reader3"; readerThread4.Name = "Reader4"; readerThread5.Name = "Reader5"; writerThread1.Name = "Writer1"; writerThread2.Name = "Writer2"; writerThread3.Name = "Writer3"; // start each thread readerThread1.Start(); readerThread2.Start(); writerThread1.Start(); readerThread3.Start(); readerThread4.Start(); writerThread2.Start(); readerThread5.Start(); writerThread3.Start(); } // end Main } // end Synchronized } // end thread_proj Seat.cs // Filename: Seat.cs // Seats which clients readers and writers access. using System; using System.Threading; /// /// Summary description for Seat /// namespace thread_proj { class Seat { // seating enumerations public enum SeatType{ WINDOW, AISLE, CENTER } // boolean which determines if seat is taken private bool taken = false; // seat information variables double seatCost = 0; SeatType type = SeatType.WINDOW; // variables used to determine current state of access private bool writing = false; private static int numberReading = 0; private static int numberWaitingToWrite = 0; // constructor that initializes values public Seat( double cost, SeatType seatType) { // Your code here } // property to allow writer threads to modify seat cost public double SeatCost { get { // Your code here } set { // Your code here } } // property to allow writer threads to modify seat type public SeatType Type { get { // Your code here } set { // Your code here } } // property to allow writer threads to modify seat taken // status public bool SeatTaken { get { // Your code here } set { // Your code here } } // retrieve seat information public String SeatInformation() { // Your code here } // methods readers and writers call when accessing // and releasing reservations public void StartReading() { // Your code here } // end StartReading public void StopReading() { // Your code here } // end StopReading public void StartWriting() { // Your code here } // end StartWriting public void StopWriting() { // Your code here } // end StopWriting } // end Seat } // end thread_projRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.