评估操作系统中进程的逻辑
考虑一个同时执行两个进程 P 和 Q 的单处理器系统。每个进程执行下面列出的代码,进程 P – 过程 P,进程 Q – 过程 Q。两个进程在彼此相隔很短的时间内到达,但不能假设关于他们开始执行的时间和相对速度。下面的代码中使用的从 A 到 K 的所有语句都是原子的,即。它们要么完全执行,要么根本不执行。进程的执行由两个二进制信号量S1和S2同步。信号量S1初始化为1,信号量S2初始化为0。进程执行的代码如下:
procedure P
begin
A;
wait(S1);
B;
signal(S1);
C;
D;
signal(S2);
E;
end
procedure Q
begin
F;
wait(S1);
G;
H;
J;
signal(S1);
wait(S2);
K;
end
a.给出语句A到K的至少四种可能的执行顺序:
A、C、D、E ,K
A,D,C,E,K
F,C,D,E,K
F,D,C,E,K
b.给定示例中信号量 S1 和 S2 的功能是什么?
S1 – used for waiting
c.语句E是否可以在语句F之前执行?证明你的答案合理。
是的,(但我不太确定,有人可以确认吗?)
d.语句K可以在语句A之前执行吗?证明你的答案合理。
Consider a uniprocessor system executing concurrently two processes P and Q. Each process executes the code listed below, process P – procedure P, and process Q – procedure Q. Both processes arrive within a very short time of each other, but no assumptions can be made about the time they start execution and their relative speed. All statements used in the code below from A to K are atomic ie. they either execute completely or not at all. The execution of the processes is synchronised by two binary semaphores S1 and S2. The semaphore S1 is initialised to 1, and the semaphore S2 is initialised to 0. The code executed by the processes is as follows:
procedure P
begin
A;
wait(S1);
B;
signal(S1);
C;
D;
signal(S2);
E;
end
procedure Q
begin
F;
wait(S1);
G;
H;
J;
signal(S1);
wait(S2);
K;
end
a.Give at least four possible orders of execution for statements A to K.
A,C,D,E,K
A,D,C,E,K
F,C,D,E,K
F,D,C,E,K
b. What is the function of each of the semaphores S1 and S2 in the given example?
S1 – used for waiting
c. Is it possible for statement E to execute before statement F? Justify your answer.
Yes, (but i am not so sure, can someone confirm?)
d. Is it possible for statement K to execute before statement A? Justify your answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个。我想知道你为什么省略了语句 B、F、G、H 和 J。你认为它们不会被执行吗?我认为一旦你回答了b,你就会很容易地回答a。
b.所有信号量都可以等待,但这就是它们的工作方式,而不是它们的用途。你能说得更具体一些吗?信号量可用于同步(foo 必须发生在 bar 之后)和排除(在这两段代码之间,一次只能执行一段)。您认为这两个信号量在这里有什么用?
c. E 可能需要什么才能在 F 之前执行?存在这种情况吗?或者,换个角度看:需要什么才能阻止 E 在 F 之前执行?存在这种情况吗?
d.参见c。
这是一个信号量,用于确保 bar 在 foo 之后发生:
这是一个信号量,用于确保一次只执行两段代码之一:
您看到区别了吗?重新检查作业中的代码,看看它现在是否更有意义。
a. I wonder why you've left out statements B, F, G, H and J. Do you suppose that they don't get executed? I think that once you've answered b you'll easily answer a.
b. All semaphores can wait, but that's how they work, not what they're for. Can you be more specific? Semaphores can be used for synchronization (foo must happen after bar) and for exclusion (between these two sections of code, only one may execute at a time). What do you think the two semaphores are being used for here?
c. What would be required for E to possibly execute before F? Is that condition present? Or, look at it the other way: What would be required to keep E from executing before F? Is that condition present?
d. See c.
Here is a semaphore being used to ensure that bar happens after foo:
And here is a semaphore being used to ensure that only one of two sections of code is executed at a time:
Do you see the difference? Reexamine the code in your homework and see if it makes more sense now.