Cyclic executive

A cyclic executive[1][2] is an alternative to a real-time operating system. It is a form of cooperative multitasking, in which there is only one task. The sole task is typically realized as an infinite loop in main(), e.g. in C.

The basic scheme is to cycle through a repeating sequence of activities, at a set frequency. For example, consider the example of an embedded system designed to monitor a temperature sensor and update an LCD display. The LCD may need to be written twenty times a second (i.e., every 50 ms). If the temperature sensor must be read every 100 ms for other reasons, we might construct a loop of the following appearance:

int main(void)
{
   while (1)
   {
      // This loop is designed to take 100 ms, meaning
      // all steps add up to 100 ms.
      // Since this is demo code and we don't know how long
      // tempRead or lcdWrite take to execute, we assume
      // they take zero time.
      // As a result, the delays are responsible for the task scheduling / timing.

      // Read temp once per cycle (every 100 ms)
      currTemp = tempRead();

      // Write to LCD twice per cycle (every 50 ms)
      lcdWrite(currTemp);
      delay(50);
      lcdWrite(currTemp);
      delay(50);

      // Now 100 ms (delay(50) + delay(50) + tempRead + lcdWrite + lcdWrite)
      // has passed so we repeat the cycle.
   }
}

The outer 100 ms cycle is called the major cycle. In this case, there is also an inner minor cycle of 50 ms. In this first example the outer versus inner cycles aren't obvious. We can use a counting mechanism to clarify the major and minor cycles.

int main(void)
{
   int i == 0;
   while (1)
   {
      // This loop is designed to take 50 ms.
      // Since this is demo code and we don't know how long
      // tempRead or lcdWrite take to execute, we assume
      // they take zero time.
      // Since we only want tempRead to execute every 100ms, we use
      // an if statement to check whether a counter is odd or even, 
      // and decide whether to execute tempRead.

      // Read temp every other cycle (every 100 ms)
      if ( (i%2) == 0)
      {
         currTemp = tempRead();
      }

      // Write to LCD once per cycle (every 50 ms)
      lcdWrite(currTemp);
      delay(50);

      i++;
      // Now 50 ms has passed so we repeat the cycle.
   }
}

See also

References

  1. Bruce Powell Douglass (2003). Real-Time Design Patterns: Robust Scalable Architecture for Real-Time Systems. Addison-Wesley Longman Publishing Co., Inc. pp. 232–237. ISBN 0201699567.
  2. Laplante, Phillip A.; Ovaska, Seppo J. (2012). Real-Time System Design and Analysis (4th ed.). Hoboken, NJ: John Wiley & Sons, Inc. pp. 84–85, 100–102. ISBN 978-0-470-76864-8.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.