1. Title: there is an empty plate on the table that can hold five fruits. The father kept putting apples or oranges on the plate, the son kept taking oranges out of the plate to enjoy, and the daughter kept taking apples out of the plate to enjoy. It is stipulated that three people cannot use the plate at the same time. Try semaphore to realize the synchronization between father, son and daughter.
The code is as follows:
semaphore mutex = 1, plate = 5, apple = 0, orange = 0; void father() { while (true) { buy fruits; wait(mutex); wait(plate); put in a fruit; if (fruit == apple) signal(apple); else signal(orange); signal(mutex); } } void boy() { wait(mutex); wait(orange); put out and eat an orange; signal(plate); signal(mutex); } void girl() { wait(mutex); wait(apple); put out and eat an apple; signal(plate); signal(mutex); }
2. Topic: try to use the recording semaphore to write an algorithm for the dining problem of philosophers who can't deadlock and only four philosophers go to get the chopsticks on the left.
The code is as follows:
semaphore chopstick[5] = {1, 1, 1, 1, 1}; semaphore mutex = 4; void philosopher_i() { while (true) { wait(mutex); wait(chopstick[i]); wait(chopstick[(i + 1) % 5]); eating; signal(chopstick[i]); signal(chopstick[(i + 1) % 5]); wait(mutex); thinking; } }
3. Topic: please use semaphores to solve the following problem of "crossing a single wooden bridge": pedestrians in the same direction can cross the bridge continuously. When someone crosses the bridge in one direction, pedestrians in the other direction must wait; When no one crosses the bridge in one direction, pedestrians in the other direction can cross the bridge.
The code is as follows
// The mutually exclusive access of the left and right sides across the bridge can be realized. People crossing the bridge in the same direction are not mutually exclusive. semaphore mutex = 1; int left = 0, right = 0; //Represents the number of people crossing the bridge on the left and right respectively void left() { while (true) { if (left == 0) //There's someone on the left wait(mutex); left = left + 1; cross the bridge; left = left - 1; if (left == 0) //Everyone on the left is finished signal(mutex); } } void right() { while (true) { if (right == 0) //There's someone on the right wait(mutex); right = right + 1; cross the bridge; right = right - 1; if (right == 0) //Everyone on the right is finished signal(mutex); } }
4. Title: sleepy barber question: a barber's shop consists of a waiting room with N sofas and a barber's room with a barber's chair. When no customers want a haircut, the barber goes to bed. When a customer enters the barber shop, if all the sofas have been occupied, he leaves the barber shop; Otherwise, if the barber is cutting hair for other customers, the customer will find an empty sofa to sit down and wait; If the barber is sleeping because there are no customers, the new customer wakes the barber to cut his hair. After the haircut, the customer must pay until the barber charges before leaving the barber's shop. Try semaphores to achieve this synchronization problem.
The code is as follows:
// Barber question // Mutually exclusive resources include: N sofas: sofa, and a barber chair: barber_sofa, if the barber chair is occupied, // Barbers have to work, so barber chairs are equivalent to barbers. Payment is also mutually exclusive semaphore sofa = N, barber_sofa = 1, pay = 0; int customer_num = 0; //Number of customers void customer() { while (true) { customer_num++; //Customers entering the store wait(sofa); wait(barber_sofa); signal(sofa); //The customer left the sofa for a haircut wake up barber and Start a haircut; //Wake up the barber and start cutting hair wait(pay); //The customer pays and waits for the barber to collect siganl(barber_sofa); //The customer gave up the barber chair customer_num--; //Customers leave } } void barber() { while (customer_num == 0) { sleep; } while (customer_num != 0) { harcuting; //haircut signal(pay); //Barber collection } }