Keil Logo

Round-Robin Scheduling

RTX can 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 RTX 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, RTX switches to the next task that is ready to run and has the same priority. If no other task with the same priority is ready to run, the currently running task resumes it execution. The duration of a time slice can be defined in the RTX_config.c configuration file.

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

#include <rtl.h>

int counter1;
int counter2;

__task void job1 (void);
__task void job2 (void);

__task void job1 (void) {
  os_tsk_create (job2, 0);   /* Create task 2 and mark it as ready */
  while (1) {                /* loop forever */
    counter1++;              /* update the counter */
  }
}

__task void job2 (void) {
  while (1) {                /* loop forever */
    counter2++;              /* update the counter */
  }
}

void main (void) {
  os_sys_init (job1);        /* Initialize RTX Kernel and start task 1 */
  for (;;);
}

Note

  • Rather than wait for a task's time slice to expire, you can use one of the system wait functions or the os_tsk_pass function to signal to the RTX kernel that it can switch to another task. The system wait function suspends the current task (changes it to the WAIT_xxx state) until the specified event occurs. The task is then changed to the READY state. During this time, any number of other tasks can run.
  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.