ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU-(2020-2021)-1 > student homework > 2018329621072_陈怡 >
homework6_陈怡_2018329621072 Version 0
👤 Author: by 1210775967qqcom 2020-11-09 07:08:39
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 (from the Dutch proberen,“to test"); signal() was originally called V (from verlogen,“to increment"). The definition of wait() is as follows:

wait(S) {
while S <= 0
; // no-op
S--;
}
The definition of signal() is as follows:
signal(S){
S++ ;
}
All the modifications to the integer value of the semaphore in the wait() and signal() operations must be executed 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 of the integer value of S (S≤0), and its possible modification (S--), must also be executed without interruption.


Problem: A group of producers and consumers share n ring buffers.

empty -- indicates whether the buffer is empty, with an initial value of n.
full -- indicates whether the buffer is full, with an initial value of 0.
mutex1 -- mutex semaphore between producers, with an initial value of 1.
mutex2 -- mutex semaphore between consumers, with an initial value of 1.

Let the number of buffer be 1 ~ n-1, and define two pointers, in and out, which are used by producer process and consumer process respectively, to point to the next available buffer.

Producer Process:
while(TRUE){
//produce an item;
P(empty);
P(mutex1);
//move the item to buffer(in);
in=(in+1)mod n;
V(mutex1);
V(full);
}

Consumer Process:
while(TRUE){
P(full)
P(mutex2);
//remove the item from buffer(out);
out=(out+1)mod n;
V(mutex2);
V(empty);
//consume;
}

Please login to reply. Login

Reversion History

Loading...
No reversions found.