算法测验| SP竞赛1 |问题10

以下是两个并发进程A、B及其各自的代码:

null

代码A:

while (true) // infinite condition
{
    M :____;
    printf("%c", b);
    printf("%c", b);
    N:____;
}

代码B:

while (true) // infinite condition
{
    O:____;
    printf("%c", a);
    printf("%c", a);
    P:____;
}

M、N、O、P上的二进制信号量操作应该是什么,信号量X、Y的初始值必须是什么才能得到输出 bbaabbaabbaa。 ? 其中,P分别表示下降,V分别表示上升操作。

(A) M=P(Y),N=V(X),O=P(X),P=V(Y);X=0,Y=1; (B) M=P(Y),N=V(X),O=P(X),P=P(Y);X=Y=1; (C) M=P(Y),N=V(Y),O=P(X),P=V(X);X=1,Y=0; (D) M=P(Y),N=V(Y),O=P(X),P=V(X);X=Y=1; 答复: (A) 说明: 在信号灯中,向上操作总是成功的,而向下操作并不总是成功的。 在以下并行过程中,操作是: A:代码

while (true) // infinite condition
{
M :P(Y); // Y become 0 successful down operation.
printf("%c", b);
printf("%c", b);
N:V(X); // X become 1 successful up operation.
}

B代码:

while (true) // infinite condition
{
O:P(X); // X  become 0 successful down operation.
printf("%c", a);
printf("%c", a);
P:V(Y); // Ybecome 1 successful up operation.
}

在这里,所有操作均成功,初始值X和Y分别为0和1。 因此,选项(A)是正确的。 这个问题的小测验

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享