y = 2; parbegin x = y * 2; y = 6; parend;
up(s): wait until s < smax; then increment s by 1 down(s): wait until s > 0; then decrement s by 1Write a monitor to implement bounded semaphores. (Hint: assume the semaphore is to be initialized to the constant SINIT and the maximum value is SMAX.)
type boundedsem = monitor (* scount is the integer value of the semaphore; if this is too big and the process tries to up it, the process will block on toobig; similarly, if the value is too small and the process tries to down it, the process will block on toosmall. ` *) var scount : integer; toobig, toosmall : condition; (* straighforward implementation of up *) procedure entry up; begin (* if too big, wait until ok *) while scount >= SMAX do toobig.wait; (* increment and notify someone waiting for the semaphore to be nonzero that it is *) scount := scount + 1; toosmall.signal; end; (* straighforward implementation of down *) procedure entry down; begin (* if too small, wait until ok *) while scount <= 0 do toosmall.wait; (* decrement and notify someone waiting for the semaphore to be less than SMAX that it is *) scount := scount - 1; toobig.signal; end; begin (* initialization *) scount := SINIT; end.
job name | arrival time | service time |
---|---|---|
A | 0 | 3 |
B | 2 | 5 |
C | 4 | 2 |
D | 6 | 1 |
E | 8 | 4 |
time | running | ready queue | explanation |
---|---|---|---|
0 | |||
1 | A | ||
2 | A | B arrives | |
3 | B | A | |
4 | B | A | C arrives |
5 | A | C B | C comes before B as arrivals precede quantum expirations, but A runs as it is first in the run queue; A terminates |
6 | C | B | D arrives |
7 | C | B D | B comes before D as arrivals precede quantum expirations; C terminates |
8 | B | D | E arrives; B runs as it is already in the queue |
9 | B | D E | D comes before E as D is already in the queue |
10 | D | E B | D terminates>|
11 | E | B | |
12 | E | B | |
13 | B | E | B terminates |
14 | E | ||
15 | E | E terminates |