临界区问题

发布于 2024-09-05 13:26:09 字数 810 浏览 1 评论 0原文

proces P0:                             proces P1:
while (true)                           while (true)
{                                      {
  flag[0] = true;                          flag[1] = true;
  while (flag[1])                          while (flag[0])
  {                                        {
     flag[0] = false;                        flag[1] = false;
     flag[0] = true;                         flag[1] = true;
  }                                        }
 crit0();                                  crit1();
 flag[0] = false;                          flag[1] = false;
 rem0();                                   rem1();
}                                       }

有人可以给我一个上下文切换的场景来证明上述代码是否满足进度和有界等待的要求。

任何人都可以给我一些关于如何检测代码是否满足进度或有界等待的要求的提示(可能包括饥饿、死锁和在你之后)

proces P0:                             proces P1:
while (true)                           while (true)
{                                      {
  flag[0] = true;                          flag[1] = true;
  while (flag[1])                          while (flag[0])
  {                                        {
     flag[0] = false;                        flag[1] = false;
     flag[0] = true;                         flag[1] = true;
  }                                        }
 crit0();                                  crit1();
 flag[0] = false;                          flag[1] = false;
 rem0();                                   rem1();
}                                       }

Could someone give me a scenario with context switches to prove if the above stated code meets the requirements of progress and bounded waiting.

And can anyone give me some tips about how to detect if a code meets the requirements of progress or bounded waiting(and maybe including starvation,deadlock and after-you after you)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-09-12 13:26:09

这两个过程同时发生。

这里的技巧是,由于没有真正同步两个程序,因此行之间可能会发生一些事情。同样,事情也可能同时发生。

要了解这如何成为一个问题,请考虑这种情况...

如果第一个 flag[0] = true 和第一个 flag[1] = true 恰好同时发生在 P0/P1 上,会发生什么?

进程 1 和进程 2 都会陷入 while 循环。他们如何退出 while 循环?一个进程必须在另一进程将其 flag[me] 设置为 true 的同一时刻检查 while(flag[other]) 。这是一个非常狭窄的时间跨度。这相当于一遍又一遍地掷骰子,直到达到某个数字才继续。

这就是为什么我们需要更高级别的东西来为我们处理同步——真正的锁等。

编辑:哦,另一件事。您可能需要检查读/写操作是否是线程安全的。如果系统在尝试读取该位的同时尝试写入该位,会发生什么情况?

编辑2:仅供参考 - http://msdn.microsoft .com/en-us/library/aa645755(v=VS.71).aspx

The two processes are happening at the same time.

The trick here is that since there is nothing truly synchronizing the two programs, something could happen between lines. On the same note, it's possible things happen at the same time.

To see how this can be an issue, think about this situation...

What would happen if the first flag[0] = true and the first flag[1] = true happened on P0/P1 at exactly the same time?

Both process 1 and process 2 would be stuck in a while loop. How would they exit the while loop? One process would have to check while(flag[other]) at exactly the same moment the other process set their flag[me] to true. This is a very narrow time span. It's the equivalent of rolling dice over and over and not continuing until you hit a certain number.

This is why we need something of a higher level to handle the synchronization for us - real locks and the like.

edit: Oh, one other thing. You may want to check to see if the read/write operations are thread safe. What happens if the system tries to write to the bit the same time it tries to read it?

edit2: FYI - http://msdn.microsoft.com/en-us/library/aa645755(v=VS.71).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文