Professional Documents
Culture Documents
round-robin
Reference: Simon chapter 5
void main ()
{
Round-robin is the simplest while (TRUE)
imaginable architecture. {
if (!! I/O Device A needs service){
There are no interrupts. }
!! Take care of I/O Device A
The main loop simply checks if (!! I/O Device B needs service) {
each of the I/O devices in !! Take care of I/O Device B
turn and services any that }
(...)
need service.
if (!! I/O Device Z needs service){
No interrupts, no shared !! Take care of I/O Device Z
data, no latency concerns }
}//end while (TRUE)
} //end void main()
If all three devices need service, if (!! I/O Device B needs service) {
!! Take care of I/O Device B
and the processor starts with }
device A, then the processor will (...)
not have time to reach device C
if (!! I/O Device Z needs service){
quickly enough. !! Take care of I/O Device Z
}
... Sure you can squeeze some }//end while (TRUE)
msecs by changing device order. } //end void main()
The main loop polls the flags and does any follow-up processing
required by the interrupts.
}
fDeviceA = TRUE;
This interrupt
fDeviceB will set a
= FALSE;
!! Handle I/O Device B
void interrupt vHandleDeviceB () }
boolean variable which
{ mentions that some I/O
(...)
!! Take care of I/O Device B
fDeviceB = TRUE;
operation
if needs to be done.
(fDeviceZ){
fDeviceZ = FALSE;
} !! Handle I/O Device Z
void interrupt vHandleDeviceZ () }
{ } //end of while (TRUE)
!! Take care of I/O Device B }
fDeviceB = TRUE;
}
CPE 355 - Real Time Embedded Kernels - Spring 12
Nuno Alves (nalves@wne.edu), College of Engineering
Round robin with interrupts example
BOOL fDeviceA = FALSE; void main ()
BOOL fDeviceB = FALSE; {
(...) while (TRUE) {
BOOL fDeviceZ = FALSE; if (fDeviceA){
fDeviceA = FALSE;
void interrupt vHandleDeviceA () !! Handle I/O Device A
{
!!The
Takemain
careloop checks
of I/O Device theA }
if (fDeviceB){
boolean
fDeviceA variable
= TRUE; to see if fDeviceB = FALSE;
} there is some I/O operation !! Handle I/O Device B
void interrupt vHandleDeviceB ()
{ that needs to be done.
}
(...)
!! Take care of I/O Device B if (fDeviceZ){
fDeviceB = TRUE; fDeviceZ = FALSE;
} !! Handle I/O Device Z
void interrupt vHandleDeviceZ () }
{ } //end of while (TRUE)
!! Take care of I/O Device B }
fDeviceB = TRUE;
}
CPE 355 - Real Time Embedded Kernels - Spring 12
Nuno Alves (nalves@wne.edu), College of Engineering
Isnt this the same as normal round-
robin? Whats the point
The point is that I can add important task code into the
interrupt service routine.
One example is the communications bridge, a device with two ports that
forwards data traffic received on the first port to the second and vice versa.
Assume the data on one of the ports is encrypted and that it is the job of
the bridge to encrypt and decrypt the data as it passes through it.
enable ();
FALSE means that the I/O
while (TRUE) {
if (fLinkBReadyToSend &&
fQueueHasData (&qDataToLinkB))
hardware
vEncrypt ();is now busy. {
vDecrypt (); ch = chQueueGetData (&qDataToLinkB);
disable ();
When the character is !!Send ch to Link B
ready to be sent, an interrupt fLinkBReadyToSend = FALSE;
enable ();
will set fLinkAReadyToSend }
to TRUE. }//end of while(TRUE)
}//end of of main(void)
CPE 355 - Real Time Embedded Kernels - Spring 12
Nuno Alves (nalves@wne.edu), College of Engineering
Code (Part #2)
void main (void) if (fLinkAReadyToSend &&
{ fQueueHasData (&qDataToLinkA))
char ch; {
ch = chQueueGetData (&qDataToLinkA);
/* Initialize the queues */
Bottom line: What is
vQueuelnitialize (&qDataFromLinkA); disable ();
really important (&qDataFromLinkB);
vQueuelnitialize is that the !!Send character to Link A
arriving data (on(&qDataToLinkA);
vQueuelnitialize either fLinkAReadyToSend = FALSE;
vQueuelnitialize (&qDataToLinkB); enable ();
communication
/* node) is stored
Enable the interrupts. */ }
in a queue.
enable (); if (fLinkBReadyToSend &&
while (TRUE) { fQueueHasData (&qDataToLinkB))
vEncrypt (); {
This is ();
vDecrypt done through ch = chQueueGetData (&qDataToLinkB);
interrupts. disable ();
!!Send ch to Link B
fLinkBReadyToSend = FALSE;
Everything else is secondary. enable ();
}
}//end of while(TRUE)
}//end of of main(void)
CPE 355 - Real Time Embedded Kernels - Spring 12
Nuno Alves (nalves@wne.edu), College of Engineering
Code (Part #3)
void vEncrypt (void) void vDecrypt (void)
{ {
char chClear; char chClear;
char chCryptic; char chCryptic;
// While there are chars from port A // While there are chars from port B
while (fQueueHasData(&qDataFromLinkA)) while (fQueueHasData
{ (&qDataFromLinkB))
//Encrypt them and put them on {
//queue for port B //decrypt them and put them on
chClear = chQueueGetData //queue for port A
(&qDataFromLinkA); chCryptic = chQueueGetData
(&qDataFromLinkB);
chCryptic = !! Do encryption chClear = !! Do decryption
vQueueAdd (&qDataToLinkB, chCryptic); vQueueAdd (&qDataToLinkA, chClear);
} }
} }