操作系统-信号量
首先要了解什么是P(wait)、V(signal)操作:
1 wait(semaphore *S){ 2 S->value--; 3 if(S->value<0)block(S->list); 4 } 5 6 signal(semaphore *S){ 7 S->value++; 8 if(S->value<=0)wakeuup(S->list); 9 }
设有两个进程E,N;N的优先级高于E,同时进入就绪队列,各自运行的程序段如下:
进程E: 进程N:
E1 S = 48 N1 A = 46
E2 T = S - T N2 S = A - S
E3 P(Q) N3 V(Q)
E4 S = T + A N4 A = S - T
E5 V(R) N5 P(R)
E6 A = T - S N6 T = A - S
Q=0,R=0,A=17,T=27,S=31,非抢占式优先级调度算法:
并发执行过程:
N1 A = 46 同时进入就绪队列,N的优先级高,先运行N。A = 46
N2 S = A - S S = 46 - 31 。S = 15
N3 V(Q) signal(Q),Q = 1,没有处于阻塞状态的进程,因此没有那个进程被唤醒。Q = 1
N4 A = S - T A = 15 -27。A = -12
N5 P(R) wait(R),R=-1,因此进程N进入阻塞状态。R = -1
E1 S = 48 S = 48
E2 T = S - T T = 48 - 27。T = 21
E3 P(Q) Q = 0,因此进程并没有进入阻塞状态。Q = 0
E4 S = T + A S = 21 + -12。S = 9
E5 V(R) R = 0。唤醒阻塞队列当中的下一个(N)。R = 0
E6 A = T - S A = 21 - 9。A = 12
N6 T = A - S T = 12 - 9。 T = 3