2019329621196麻宣政homework6 Version 0 |
|
👤 Author: by 1436830195qqcom 2021-12-29 08:57:53 |
Our Own case related with process synchronizatioin using semaphore:
When designing our custom question, we assumed that an art gallery has a capacity of 100 people, with only one entrance and exit. The entrances and exits are irrelevant, and only one person is allowed to pass through at a time. The activities for tourists are described as follows:
A semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operations: wait() and signal(). The wait() operation was originally termed P; signal() was originally called V. The definition of wait() is as follows:
wait(S){
while S<=0;
S--;
}
The definition of signal() is as follows:
signal(S){
S++
}
All the modificaitons to the integer value of the semaphore in the wait() and signal() operations must be excuted indivisibly. That is, when one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value. In addition, in the case of wait(S), the testing integer value of S(S<=0), and its possible modification(S--), must be executed without interruption.
semaphore empty = 100;
semaphore mutex = 1;
do{
wait(empty);
wait(mutex);
Enter;
signal(mutex);
Visit;
waiit(mutex);
Out;
signal(mutex);
signal(empty);
}