信号量小书

发布于 2024-10-17 09:26:54 字数 442 浏览 5 评论 0原文

下面的代码中,每个线程必须等待其他线程完成集合部分,然后等待直到每个线程都完成关键部分。

/* rendezvous code */
mutex.wait()
count++;
mutex_signal()
if(count==n)
            sem.signal()
sem.wait()
sem.signal()

mutex.wait()
          count--;
mutex.signal()

if(count==0)
         sem.wait()

我知道两个进程可能会遇到相同的 count 值(可能是 0 或 n)的情况。因此,可以同时发送两个或多个信号。上次测试怎么会出现僵局呢。我好像没明白这个。
这是一种十字转门类型的信号量安排,作者实际上认为它是一个十字转门,但它是一个信号量,它应该可以无死锁地工作。 请告诉我这段代码怎么会出现死锁!

Below is code in which each thread must wait for each other thread to complete the rendezvous part and then wait until everyone has completed the critical section.

/* rendezvous code */
mutex.wait()
count++;
mutex_signal()
if(count==n)
            sem.signal()
sem.wait()
sem.signal()

mutex.wait()
          count--;
mutex.signal()

if(count==0)
         sem.wait()

I know that two processes can reach the case where both see the same value of count (0 or n may be). Due to this two or more signals may be sent at the same time. How can there be a deadlock in the last test. I don't seem to get this.
This is a turnsile kindof semaphore arrangement and author is actually thinking it is a turnstile, but it's a semaphore and it should work without a deadlock.
Please tell me how is there a deadlock in this code!

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

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

发布评论

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

评论(2

念﹏祤嫣 2024-10-24 09:26:54

我将尝试解释我的看法。

除最后一个线程外的所有线程都会在第一个 sem.wait() 处等待。一旦最后一个线程到达,它将 sem.signal() (因为 count==n)允许等待线程之一(比如 T1)继续。然后T1将依次执行sem.signal(),这将允许另一个线程继续。这就像连锁反应。请注意,最后一个传递的线程也会发出一个信号,这将使信号量值为 1。
现在,如果两个线程看到 count==0 然后将尝试执行 sem.wait()。但由于信号量值为1,一个线程将无法通过,造成死锁。

I'll try to explain the way I see it.

All threads but the last will come and wait at the first sem.wait(). Once the last thread arrives it will sem.signal() (because count==n) allowing one of the waiting threads(say T1) to continue. Then T1 will in turn do a sem.signal() which will allow another thread to continue. It is something like a chain reaction. Note that the last thread to pass will also do a signal which will make the Semaphore value 1.
Now if two threads come and see that the count==0 then will try to do sem.wait(). But since the semaphore value is 1, one thread will not be able to pass, causing deadlock.

风为裳 2024-10-24 09:26:54

“if”语句也应该位于“mutex”信号量指定的关键部分内,否则竞争条件可能导致死锁。

即,正确的代码是

/* rendezvous code */
mutex.wait()
count++;
if(count==n)
        sem.signal()
mutex.signal()

sem.wait()
sem.signal()

mutex.wait()
      count--;
if(count==0)
     sem.wait()
mutex.signal()

The "if" statements should also be inside the critical sections designated by the "mutex" semaphore, otherwise race conditions can lead to deadlock.

I.e., the correct code is

/* rendezvous code */
mutex.wait()
count++;
if(count==n)
        sem.signal()
mutex.signal()

sem.wait()
sem.signal()

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