Question
The Doctor on Night Duty A doctor in a clinic has a small examination room where she examines her patients. After the initial examination, the
The Doctor on Night Duty
A doctor in a clinic has a small examination room where she examines her patients. After the initial examination, the doctor sends the patient to the lab for tests. Once the test is done, the patient visits the doctor again to receive prescription. (This means that each patient MUST visit the doctor twice). Attached to her examination room is a waiting room with four chairs 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 and finds the doctor sleeping, the patient awakens the doctor. If a patient arrives and finds the doctor currently examining another patient, the patient sits on one of the chairs in the waiting room and waits. If no chairs are available, the patient goes away to have coffee from the clinics 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.
The Doctor and the Patients
You must create n patients (where n is input by the user) using PThreads, with each patient running as a separate thread. The doctor runs as a separate thread as well. Patient threads will alternate between being seen by the doctor to visiting the lab or the cafeteria. If the doctor is available, patient will be treated immediately. Otherwise, the patient will either sit in a chair in the waiting room or, if no chairs are available, will go to the cafeteria for a random amount of time and will return to see the doctor. 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 examining a patient, the patient must visit the lab for a random amount of time and then return to see the doctor again. The patient might have to wait in the waiting room again if the doctor is busy; or drink coffee from the cafeteria if there is no empty chair in the waiting room. Meanwhile, the doctor 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. If no patients are present, the doctor may return to napping.
Perhaps the best option for simulating the time that patients spend in the lab or drinking coffee in the cafeteria, as well as the doctor treating a patient, is to have the appropriate threads sleep for a random period.
Each patient thread should print its state (drinking coffee, waiting for doctor, getting first examination, getting second examination, visiting the lab, waking up doctor, etc.) along with its patient number. Similarly, the doctor should print its state (examining, checking for next patient, sleeping etc.). A sample output is shown on the following page.
SAMPLE OUTPUT: Note: The output is meant for guidance pertaining to messages only. Actual sequence of events will vary based on the number of threads, how random numbers are generated, and how scheduler allocates CPU to each thread Patient 2 drinking coffee for 1 seconds Patient 3 drinking coffee for 3 seconds Patient 1 drinking coffee for 1 seconds Patient 0 drinking coffee for 1 seconds Patient 2 waiting. Seats occupied1 Patient 1 waiting. Seats occupied2 Patient 0 waiting. Seats occupied3 Patient 3 will try later Patient 3 drinking coffee for 2 seconds Patient 4 drinking coffee for 1 seconds Doctor treating patient for 2 seconds. Seats occupied 2 Patient 2 getting treatment Patient 2 drinking coffee for 2 seconds Patient 4 waiting. seats occupied = 3 Doctor treating patient for 1 seconds. Seats occupied2 Patient 1 getting treatment Patient 1 drinking coffee for 3 seconds Patient 3 waiting. Seats occupied Patient 2 will try later 3 Patient 1 drinking coffee for 1 seconds
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