HW6 Version 0 |
|
👤 Author: by 627864699qqcom 2020-11-10 15:46:07 |
Definition:A semaphore is a variable or abstract data type used to control access to a common resource by multiple processes in a concurrent system such as a multitasking operating system.
Operations:
Wait(): a process performs a wait operation to tell the semaphore that it wants exclusive access to the shared resource;
Signal(): a process performs a signal operation to inform the semaphore that it is finished using the shared resource.
Problem: we set that we only have one cashier. The clients are waiting in the queue for paying for their commodities. To solve this problem, we use semaphore to simulate the process:
// client
do {
wait(empty);
wait(mutex);
pay()
signal(mutex);
signal(full);
} while(1);
// cashier
do {
wait(full);
wait(mutex);
collect();
signal(mutex);
signal(empty);
}while(1);