|
|
Subject:
embedded system--interrupts
Category: Computers > Programming Asked by: allenliu-ga List Price: $5.00 |
Posted:
26 Sep 2002 13:22 PDT
Expires: 26 Oct 2002 13:22 PDT Question ID: 69447 |
int iQueue[100]; int iHead=0; int iTail=0; void interrupt SourceInterrupt(void) { if((iHead+1==Tail)||(iHead==99 && iTail==0)) { ++iTail; if(iTail==100) iTail=0; } iQueue[iHead]= !!next value ++iHead; if(iHead==100) iHead==0; } void SinkTask(void) { int iValue; while(TRUE) if(iTail!=iHead) { iValue=iQueue[iTail]; ++iTail; if(iTail==100) iTail=0; !! Do something with iValue; } } Question: in this code, it is a queuing functions without disabling interrupts, even assuming that all of the writes to the variables are atomic, a very nasty bug is hiding in this program , what is it? |
|
Subject:
Re: embedded system--interrupts
Answered By: rbnn-ga on 26 Sep 2002 16:01 PDT Rated: |
Thank you for this question. Concurrency related issues are tricky but fun to think about. Here is one bug that can occur: Suppose that at the beginning of SinkTask iHead is 98 and iTail is 99. Now suppose the code executes "++iTail" in SinkTask when two interrupts are received. Thus, when the first interrupt is received and SourceInterrupt is executed, iHead is 98 and iTail is 100 . The "if" body in SouceInterrupt is not evaluated and subsequently iHead will be 99 and iTail will remain at 100. Now the second interrupt occurs, so that SourceInterrupt is called with iHead of 99 and iTail of 100. The body of the "if" statement in SourceInterrupt is now executed (since iHead+1==iTail), but iTail will be set to 101 by the body of the if statement in Source Interrupt. Subsequently, iTail will simply keep increasing without limit, and SinkTask will start getting garbage data or memory access violation when it tries: iValue=iQueue[iTail] |
allenliu-ga rated this answer: |
|
Subject:
Re: embedded system--interrupts
From: rbnn-ga on 26 Sep 2002 23:51 PDT |
By the way, if you would like additional clarification to the answer, you can use the request clarification button; or if there is anything you do not understand about my answer. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |