1

に起こるデッドロックになるとき:私は次のコードが与えられた<a href="http://williamstallings.com/OperatingSystems/" rel="nofollow noreferrer">Operating Systems by William Stallings</a>から生産消費者問題について読んでいたこの生産者 - 消費者コード

+----------------------------------------------------+-------------------------------------------------------+ 
|    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)に設定した場合、プロデューサはコンシューマがnをチェックして14行目を待つ前に、それを1(表の11行目)に増やします。その間にプロデューサーが増えてしまったので、疲れてしまった。最悪、消費者はすぐに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および遅延のものにリセットされます始める前にそれで、どうやってデッドロックにつながるのでしょうか? (実際、私はこれが正確な解決策であると感じています。)

+0

気になる質問はありません。 –

答えて

0

間違いなくデッドロックが発生します。

プロデューサが正常に1つのアイテムを生成します。これはsempaphoresの次の値をもたらす:

mx = 1 and delay = 1 

今消費者がそのコードを実行し、delayがあるため、消費者

semWaitB(delay); 
のライン2の 0に設定されているため、ライン10

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

に到達します

10行目は消費者をブロックし、この時点ではコンシューマの6行目のためにmx = 0となっていますsemWaitB(mx);

コンシューマーはすでにブロックされており、プロデューサーは8行目のためにブロックされます。semWaitB(mx);mx = 0です。 これはデッドロックです

関連する問題