Keil Logo

Round-Robin Task Switching

RTX51 Tiny may be configured to use Round-Robin Multitasking (or task switching). Round-Robin allows quasi-parallel execution of several tasks. Tasks are not really executed concurrently but are time-sliced (the available CPU time is divided into time slices and RTX51 Tiny assigns a time slice to each task). Since the time slice is short (only a few milliseconds) it appears as though tasks execute simultaneously.

Tasks execute for the duration of their time-slice (unless the task's time slice is given up). Then, RTX51 Tiny switches to the next task that is ready to run. The duration of a time slice may be defined by the RTX51 Tiny Configuration.

The following example shows a simple RTX51 Tiny program that uses Round-Robin Multitasking. The two tasks in this program are counter loops. RTX51 Tiny starts executing task 0 which is the function named job0. This function creates another task called job1. After job0 executes for its time slice, RTX51 Tiny switches to job1. After job1 executes for its time slice, RTX51 Tiny switches back to job0. This process is repeated indefinitely.

#include <rtx51tny.h>

int counter0;
int counter1;

void job0 (void) _task_ 0 {
  os_create (1);        /* mark task 1 as ready */
  while (1) {   /* loop forever */
    counter0++; /* update the counter */
  }
}

void job1 (void) _task_ 1 {
  while (1) {   /* loop forever */
    counter1++; /* update the counter */
  }
}

Note

  • Rather than wait for a task's time slice to expire, you may use the os_wait function or the os_switch_task function to allow RTX51 Tiny to switch to another task. The os_wait function suspends the current task (changes it to the WAITING State) until the specified event occurs (and the task is changed to the READY State). During this time, any number of other tasks may run.

Related Knowledgebase Articles

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.