何时将在此生产者 - 消费者代码中发生死锁

时间:2017-01-16 13:18:33

标签: concurrency operating-system synchronization deadlock producer-consumer

我正在阅读来自Operating Systems by William Stallings的生产者消费者问题,其中提供了以下代码:

+----------------------------------------------------+-------------------------------------------------------+
|               Producer                             |             Consumer                                  |
+------------------------------------------------------------------------------------------------------------+
| 1  int n;                                          | 1  void consumer()                                    |
| 2  binary_semaphore mx = 1, delay = 0;             | 2  {  semWaitB(delay);  //wait till first data item   |
| 3  void producer()                                 | 3                       //is produced                 |
| 4  {                                               | 4     while (true)                                    |
| 5     while (true)                                 | 5     {                                               |
| 6     {                                            | 6        semWaitB(mx); //continue if producer is not  |
| 7        produce();                                | 7                      //producing                    |
| 8        semWaitB(mx);  //continue if consumer     | 8        take();                                      |
| 9                       //is not consuming         | 9        n--;                                         |
| 10        append();                                | 10        semSignalB(mx);//signal done with consuming |
| 11        n++;                                     | 11        consume();                                  |
| 12        if (n==1) semSignalB(delay);  //unblocks | 12        if (n==0) semWaitB(delay);  //block self if |
| 13                                      //consumer | 13                                    //no data item  |
| 14        semSignalB(mx);  //signal done with      | 14     }                                              |
| 15                         //producing             | 15  }                                                 |
| 16     }                                           | 16  void main()                                       |
| 17  }                                              | 17  {  n = 0;                                         |
|                                                    | 18     parbegin (producer, consumer);                 |
|                                                    | 19  }                                                 |
+----------------------------------------------------+-------------------------------------------------------+

然后说(参考下表中的行号):

  

如果消费者排气缓冲区将n设置为0(第8行),则生产者将其增加到1(表的第11行),然后消费者检查n并在第14行等待。由于缓冲区耗尽,第14行应该已经阻塞了消费者,但它没有,因为生产者同时增加了n。最糟糕的是,消费者可以立即再次运行以消耗不存在的项目以将n减少到-1(第20行)

enter image description here

然后它说:

  

我们无法在关键部分内移动条件语句,因为这可能导致死锁(例如,在上表的第8行之后)。

它继续提供不同的解决方案。

但我无法理解它将如何导致僵局。考虑以下修改后的消费者代码:

1  void consumer()
2  {  semWaitB(delay);  //wait till first data item
3                       //is produced
4     while (true) 
5     {
6        semWaitB(mx); //continue if producer is not 
7                      //producing
8        take();
9        n--;
10        if (n==0) semWaitB(delay);  //block self if
11                                    //no data item
12        semSignalB(mx);//signal done with consuming
13        consume();
14     }
15  }
16  void main()
17  {  n = 0;
18     parbegin (producer, consumer);
19  }

我想出了以下内容:

enter image description here

如您所见,最后,mx,n和delay的值将重置为start之前的值。那怎么会导致僵局呢? (事实上​​,我觉得这可能是精确的解决方案。)

1 个答案:

答案 0 :(得分:0)

肯定会导致死锁。考虑一系列操作:

生产者成功生产1件商品。这导致了sempaphores的以下值:

mx = 1 and delay = 1

现在,消费者执行其代码并到达第10行

if (n==0) semWaitB(delay);

因为delay设置为0,因为消费者的第2行

semWaitB(delay);

第10行将阻止消费者,此时我们有mx = 0,因为消费者semWaitB(mx);的第6行

由于第8行semWaitB(mx);mx = 0,消费者已被屏蔽,生产者将被屏蔽。 这是一个僵局