2019329621077唐晓莲homework-6 Version 0 |
|
👤 Author: by 1097682897qqcom 2021-12-21 16:49:31 |
There is a doctor, a department and n a chair for patients waiting to see a doctor in the hospital. If there are no patients, the doctor will rest. When a patient arrives, he must first wake up the doctor. If a patient comes when the doctor is seeing a doctor, if there is an empty chair to sit, they will sit down and wait. If there is no empty chair, he will leave. The problem here is to write a program for doctors and patients to describe their behavior without competitive conditions.
semaphore patient, doctor = 0;
semaphore mutex = 1;
int empty = N;
void Doctor() {
while(1) {
P(patient);
P(doctor);
empty++;
V(doctor);
V(mutex);
}
}
void Patient() {
P(mutex);
if(empty > 0) {
empty--;
V(patient);
V(mutex);
P(doctor);
} else {
V(mutex);
}
}