Google Answers Logo
View Question
 
Q: embedded system--interrupts ( Answered 3 out of 5 stars,   1 Comment )
Question  
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?
Answer  
Subject: Re: embedded system--interrupts
Answered By: rbnn-ga on 26 Sep 2002 16:01 PDT
Rated:3 out of 5 stars
 
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:3 out of 5 stars

Comments  
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.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy