Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A hospital has a doctor who treats patients during her night shift. The doctor has a small examination room with room for only one patient.

image text in transcribedimage text in transcribed

A hospital has a doctor who treats patients during her night shift. The doctor has a small examination room with room for only one patient. Attached to her examination room is a waiting room with three seats where patients can sit and wait if the doctor is currently examining another patient. When there are no patients who need help, the doctor takes a nap. If a patient arrives during the night and finds the doctor sleeping, the patient must awaken the doctor to ask for help. If a patient arrives and finds the doctor currently examining another patient, the patient sits on one of the seats in the waiting room and waits. If no seats are available, the patient goes away to havea coffee from the hospital cafeteria and will come back later. Using multithreading, write a program (using C or C++) that coordinates the activities of the doctor and the patients. Use mutex locks and semaphores to ensure that two patients are not in the examination room at the same time and that the patients waiting in the waiting room get their turn. More details are provided below Create n patients using PThreads, with each patient running as a separate thread. The doctor runs as a separate thread as well. Patient threads will alternate between having coffee in the cafeteria and being treated by the doctor. If the doctor is available, they will be treated immediately. Otherwise, they will either sit in a chair in the waiting room or, if no chairs are available, will go back to the cafeteria for a random amount of time and will return to see the doctor at a later time. If a patient arrives and notices that the doctor is taking a nap, the patient must notify the doctor using a semaphore. When the doctor finishes treating a patient, she must check to see if there are patients waiting for help in the waiting room. If so, the doctor must help each of these patients in turn. If no patients are present, the doctor may return to napping. Perhaps the best option for simulating the time that patients spend in the cafeteria as well as the doctor treating a patient, is to have the appropriate threads sleep for a random period of time. Each patient thread should print its state (drinking coffee, waiting for doctor, getting treated, doctor not available etc.) along with its threadID. Similarly, the doctor should print its state (treating, checking for next patient, sleeping etc.). A sample output is shown on the following page. Pthreads are covered in section 4.4.1, while mutex locks and semaphores are covered in section 5.9.4 of your textbook

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

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

When should the last word in a title be capitalized?

Answered: 1 week ago