Global Sources
EE Times-India
Stay in touch with EE Times India
EE Times-India > Embedded

Multi-tasking kernel in single line of code

Posted: 03 Oct 2014     Print Version  Bookmark and Share

Keywords:real-time operating systems  Linux  multi-threading  code  interrupt service routines 

Ultimately, run to completion scheduling is little more than the implementation of an infinite loop (architecture #1 above). However, there is some structure and the kernel may provide some additional services. A run-to-completion scheduler may be written entirely in C and does not require any assembly language coding.

Round robin scheduling
The problem with run-to-completion scheduling is that each task must terminate and then when it is scheduled again, it starts from the beginning. The code might use a static state variable to figure out what to do next, but this is rather untidy.

A better approach is to introduce the capability of context save-and-restore into the kernel. The 'context' is essentially the current value of the machine registers. 'Round robin' scheduling (figure 3) still requires each task to behave responsibly and cooperate, but instead of terminating, a task makes a system call to pause. Its context is saved and the next task is given control.

Figure 3: Round robin scheduling of tasks.

Next time a task is scheduled, after a pause, it continues execution from where it left off. A context switch inevitably requires some assembler programming.

Time sliced scheduling
Both run to completion and round robin scheduling require cooperation from each task. If a task were to hold on to the CPU for too long, the functionality of the other tasks would be adversely affected. This is often not acceptable, so some kind of pre-emptive scheduling is needed, where the scheduler can wrest control of the CPU from a task when it is time to schedule another one.

A simple pre-emptive mechanism is 'time sliced' scheduling. A timer interrupt is used to trigger the scheduler at an appropriate interval, which suspends the current task and wakes up the next in the sequence. This approach ensures that each task gets an even share of the CPU time.

Figure 4: Time-slice scheduling of tasks.

There are a couple of problems with a simple time slice like this. The first is what to do if a task has no further useful work to perform until another task has processed some data. What should it do? Simply looping is wasteful of CPU time, so a call to invoke the scheduler ahead of the next timer tick seems logical (figure 5). The drawback of this adjustment is that there is no way for a task to make effective use of its regular scheduling to behave in a real time fashion.

Figure 5: Invoking a scheduler at the wrong time prevents real time operations.

Time slice with background scheduling
A way to clean up time-sliced scheduling is to introduce a background task (figure 6) consisting of code (presumably low priority – maybe self-test) which is run when the scheduler does not have a normal task to run.

 First Page Previous Page 1 • 2 • 3 Next Page Last Page

Comment on "Multi-tasking kernel in single line ..."
*  You can enter [0] more charecters.
*Verify code:


Visit Asia Webinars to learn about the latest in technology and get practical design tips.


Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

Back to Top