互斥问题

发布于 2024-11-09 17:13:22 字数 618 浏览 3 评论 0原文

请看一下下面的伪代码:

boolean blocked[2];
int turn;
void P(int id) {
      while(true) {
             blocked[id] = true;
             while(turn != id) {
                    while(blocked[1-id])
                    /* do nothing */;
                    turn = id;
             }
             /* critical section */
             blocked[id] = false;
             /* remainder */
      }
}
void main() {
      blocked[0] = false;
      blocked[1] = false;
      turn = 0;
      parbegin(P(0), P(1)); //RUN P0 and P1 parallel
}

我认为可以使用上面的代码实现一个简单的互斥解决方案。但这不起作用。有谁知道为什么吗?

任何帮助将不胜感激!

Please take a look on the following pseudo-code:

boolean blocked[2];
int turn;
void P(int id) {
      while(true) {
             blocked[id] = true;
             while(turn != id) {
                    while(blocked[1-id])
                    /* do nothing */;
                    turn = id;
             }
             /* critical section */
             blocked[id] = false;
             /* remainder */
      }
}
void main() {
      blocked[0] = false;
      blocked[1] = false;
      turn = 0;
      parbegin(P(0), P(1)); //RUN P0 and P1 parallel
}

I thought that a could implement a simple Mutual - Exclution solution using the code above. But it's not working. Has anyone got an idea why?

Any help would really be appreciated!

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

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

发布评论

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

评论(6

旧时模样 2024-11-16 17:13:22

在这个例子中,由于以下原因,不能保证互斥:

我们从以下情况开始:

blocked = {false, false};
turn = 0;

P1 现在正在执行,并跳过

  blocked[id] = false; // Not yet executed.

现在的情况:

blocked {false, true}
turn = 0;

现在 P0 正在执行。它通过了第二个 while 循环,准备执行关键部分。而当P1执行时,将turn设置为1,也准备好执行临界区。

顺便说一句,这种方法最初是由海曼发明的。 1966 年,他将其发送给 ACM Communications

Mutual Exclusion is in this exemple not guaranteed because of the following:

We begin with the following situation:

blocked = {false, false};
turn = 0;

P1 is now executes, and skips

  blocked[id] = false; // Not yet executed.

The situation is now:

blocked {false, true}
turn = 0;

Now P0 executes. It passes the second while loop, ready to execute the critical section. And when P1 executes, it sets turn to 1, and is also ready to execute the critical section.

Btw, this method was originally invented by Hyman. He sent it to Communications of the Acm in 1966

以往的大感动 2024-11-16 17:13:22

在本例中,由于以下原因,无法保证互斥:

我们从以下情况开始:

turn= 1;
blocked = {false, false};

执行运行如下:

P0: while (true) {
P0:   blocked[0] = true;
P0:   while (turn != 0) {
P0:     while (blocked[1]) {
P0:     }
P1: while (true) {
P1:   blocked[1] = true;
P1:   while (turn != 1) {
P1:   }
P1:   criticalSection(P1);
P0:     turn = 0;
P0:   while (turn != 0)
P0:   }
P0:   critcalSection(P0);

Mutual Exclusion is in this exemple not guaranteed because of the following:

We begin with the following situation:

turn= 1;
blocked = {false, false};

The execution runs as follows:

P0: while (true) {
P0:   blocked[0] = true;
P0:   while (turn != 0) {
P0:     while (blocked[1]) {
P0:     }
P1: while (true) {
P1:   blocked[1] = true;
P1:   while (turn != 1) {
P1:   }
P1:   criticalSection(P1);
P0:     turn = 0;
P0:   while (turn != 0)
P0:   }
P0:   critcalSection(P0);
冷血 2024-11-16 17:13:22

这是作业,还是一些嵌入式平台?是否有任何原因不能使用 pthreads 或 Win32(相关)同步原语?

Is this homework, or some embedded platform? Is there any reason why you can't use pthreads or Win32 (as relevant) synchronisation primitives?

会发光的星星闪亮亮i 2024-11-16 17:13:22

也许您需要声明为阻塞并转为易失性,但如果不指定编程语言,则无法知道。

Maybe you need to declare blocked and turn as volatile, but without specifying the programming language there is no way to know.

萌梦深 2024-11-16 17:13:22

并发不能这样实现,特别是在多处理器(或多核)环境中:不同的核/处理器有不同的缓存。这些缓存可能不一致。下面的伪代码可以按所示顺序执行,并显示结果:

get blocked[0] -> false   // cpu 0
set blocked[0] = true     // cpu 1 (stored in CPU 1's L1 cache)
get blocked[0] -> false   // cpu 0 (retrieved from CPU 0's L1 cache)
get glocked[0] -> false   // cpu 2 (retrieved from main memory)

需要硬件知识来实现​​并发。

Concurrency can not be implemented like this, especially in a multi-processor (or multi-core) environment: different cores/processors have different caches. Those caches may not be coherent. The pseudo-code below could execute in the order shown, with the results shown:

get blocked[0] -> false   // cpu 0
set blocked[0] = true     // cpu 1 (stored in CPU 1's L1 cache)
get blocked[0] -> false   // cpu 0 (retrieved from CPU 0's L1 cache)
get glocked[0] -> false   // cpu 2 (retrieved from main memory)

You need hardware knowledge to implement concurrency.

赴月观长安 2024-11-16 17:13:22

编译器可能已经优化了“空”while 循环。将变量声明为 volatile 可能会有所帮助,但不能保证在多处理器系统上足够。

Compiler might have optimized out the "empty" while loop. Declaring variables as volatile might help, but is not guaranteed to be sufficient on multiprocessor systems.

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