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?
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] 00_Prelims-Embedded Systems.p65:: File Format: PDF/Adobe Acrobat - View as HTML1.6 Embedded System-on-chip (Soc) and Use of VLSI Circuit Design Technology 29 4.1 programmed-I/O Busy-wait Approach without Interrupt Service Mechanism http://highered.mcgraw-hill.com/sites/dl/free/0070667640/607543/Detailed_Table_of_Contents.pdfHOME |
Nortel Unveils Vision, Strategy for Israeli High-Performance Net
Busy Friday Leads to Strong Close for Net Stocks
|