Task switching with RTOS

Daily Newsletters

Sign up to ZDNet UK's daily newsletter.

ANALYSIS
Switching from a "one-big-loop" style of programming to a multithreaded real-time operating system (RTOS) can make sense in many applications. An RTOS can switch between multiple tasks, or threads of execution, so it appears that the tasks are running concurrently. Although the processor can execute only one task at a time, RTOS task switching gives the illusion of concurrent execution. Most embedded RTOSs use preemptive priority-based task switching, meaning that the CPU can switch from one task to another at almost any time, usually in response to an external interrupt or RTOS system call. The RTOS executes high-priority tasks in preference to lower-priority tasks. This ability to run multiple tasks brings new complications, however, and RTOS services for intertask communication (e.g., message queues), synchronisation, and mutual exclusion (e.g., semaphores) are needed to address these issues. Here's a look at the advantages of using an RTOS, along with several key issues to watch out for. RTOS benefits
Reduced interdependencies: An RTOS allows lengthy jobs to run independently in different tasks. Unlike with the one-big-loop method, there's no need to divide software unnaturally into small subjobs to take account of the timing and execution requirements of other parts of the system. Easier collaboration: An RTOS makes it easier to write modular code you can expand without fear of interfering with other parts of the system. This in turn makes it easier for a team of programmers to collaborate on a large project. Better response time: A preemptive RTOS can improve the real-time response to external events by ensuring that an appropriate high-priority task resumes execution as soon as the RTOS receives an event. In contrast to the one-big-loop method, it isn't necessary to wait for other, less important jobs to complete first. Pitfalls
Shared resources: A preemptive task switch may occur while a task is accessing shared hardware or data. This may result in another task also attempting to access the same hardware or data before the first task has completed its operation. You can overcome these problems with strict and careful use of RTOS's mutual exclusion mechanisms. These include using semaphores, locking the RTOS scheduler, or briefly disabling interrupts. Non-reentrant code: Functions should be reentrant, so you need to avoid static/global data and using local (stack-based) storage for function variables. This allows multiple tasks to execute the same function at the same time without interference. Sometimes, tasks must share static data structures. Using the mutual exclusion techniques can protect them. Priority inversion: When a high-priority task can't run because it's waiting for a resource (e.g., semaphore) owned by a lower-priority task, priority inversion occurs. While it's waiting, the high-priority task effectively has the same priority as the lower-priority task. It can't run until the scheduling of the lower-priority task and the release of the resource. A high-priority task that occasionally behaves like a lower-priority task can have a disastrous affect on the real-time performance of your software. Some RTOSs avoid this problem using a priority-inheritance mechanism that temporarily bumps up the priority of the lower-priority task, but not all RTOSs support this feature. Deadlock: When two tasks can't proceed because they're each waiting for a resource owned by the other, you have a deadlock. The tasks effectively hang. Avoiding deadlock conditions requires careful attention to the way in which multiple tasks share semaphores and other RTOS resources. Deadlock conditions don't always show up easily during software testing. Extra CPU overhead: A small overhead is associated with task switching and other RTOS duties, although, in practice, it isn't necessarily a serious problem. The task-switching overheads serve to ensure that software is responsive and always switches to run the most important task. Extra memory requirement: Extra ROM/RAM is required for the RTOS code and associated data structures. Additionally, each task requires extra RAM for its own private stack space. Complex debugging: The dynamics of the code execution are more complex and less predictable in a multitasking RTOS environment. This can make it more difficult to reproduce the exact circumstances in which a bug occurred. Reaping the benefits
Although you need to be careful how you use an RTOS, the payoff may be worth it. RTOS task switching enables you to run lengthy jobs independently, write modular code to facilitate collaboration, and improve response time by prioritising tasks.
Have your say instantly in the Tech Update forum. Find out what's where in the new Tech Update with our Guided Tour. Let the editors know what you think in the Mailroom.

Post your comment

In order to post a comment you need to be registered and logged in.

You can also log in with Facebook. Log in or create your ZDNet UK account below

  • Login

Will not be displayed with your comment

By signing up for this service, you indicate that you agree to our Terms and Conditions and have read and understood our Privacy Policy. Questions about membership? Find the answers in the Community FAQ

Get ZDNet UK's daily newsletter

Enter your email address to sign up

ZDNet UK Live

JCB33

How dare film makers, artists or anybody that invests in creativity stop us pirating their works for free. I want to be able to walk into my local...

5 hours ago by JCB33 on ACTA stumbles in Germany
Moley

@GrueMaster. I prefer horses for courses rather than one size fits all. I, and I suspect most other computer users, do not really wish to have...

7 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
greycynic

The product that scares me every time I have to use it is the Office 2007 version of Excel. The first bug that I found was applying the median...

7 hours ago by greycynic on Ten flawed products that derail productivity
GrueMaster

Nice review and very informative. One thing I'd like to add (in reply to whs001's 1st question), the main reason to have the same interface from...

9 hours ago by GrueMaster on A tale of two distros: Ubuntu and Linux Mint
Frederick Wrigley

I'be been using Mint 12 since the RC came out, and I am far more happy with the Cinnamon, the Mate, and, yes (with extensions), theGnome 3...

10 hours ago by Frederick Wrigley via Facebook on A tale of two distros: Ubuntu and Linux Mint
bdantas

Excellent article. One small correction, though--although a fresh installation of Linux Mint 12 will, indeed, provide the user with a version of...

10 hours ago by bdantas on A tale of two distros: Ubuntu and Linux Mint
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

11 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

11 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Moley

For Gnome 2 die-hards, it is possible to add icons to the bottom panel (or top top panel, if you prefer) which provide the exact Gnome 2...

12 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
ramwellian

Your comments would seem pretty naive and immature. Your 'solution' appears to be, "gee, let's all just give in to the hackers and give them...

12 hours ago by ramwellian on Cloud computing security: no more oxymoron?
BugStalker

"Interesting thought ... If you installed Win7 as a dual boot on a machine that previously only had Linux, and it wrecked your Linux installation,...

12 hours ago by BugStalker on Windows 7 Declares War on GRUB
whs001

This is an excellent summary of Ubuntu and Mint and the interface differences between them. Most such articles take a very partisan position for...

12 hours ago by whs001 on A tale of two distros: Ubuntu and Linux Mint
Moley

@ewallace. Not so clear. Anyone can obtain the text, for example from here http://www.ustr.gov/webfm_send/2379. I support ACTA so long as it and...

13 hours ago by Moley on ACTA: Facts, misconceptions and questions
45283

I think WinRT is fantastic. I just wish it was an option for people that didn't want to go through Microsoft's App Store with its attendant...

16 hours ago by 45283 on Why Windows 8 needs architectural hygiene for WOA
Burn-IT

Nine people? £30m? Who's back pocket is that lot going in? And IF they say it is for new buildings, what about all the ones the government has...

17 hours ago by Burn-IT on Police set to launch three £30m e-crime hubs
ewallace

Just to be clear, nobody knows what is in the text of ACTA, here is a photograph of the text of ACTA http://twitpic.com/8h9iju as submitted to the...

17 hours ago by ewallace on ACTA: Facts, misconceptions and questions
fgvrg56

Unfortunately main issue is that ASUS is refusing to accept that they make some mistake on this version of asus Transformer prime. 1 - GPS sensor...

18 hours ago by fgvrg56 on Asus Eee Pad Transformer Prime Wi-Fi & GPS problems?
Ben Woods

@Marcus A fair question. Just talked with Archos which said it was working on an announcement for next week....

19 hours ago by Ben Woods on Archos confirms G9 Ice Cream Sandwich update schedule
Marcus Karlsson

Any update on this, considering the claimed "first week of February"?

20 hours ago by Marcus Karlsson via Facebook on Archos confirms G9 Ice Cream Sandwich update schedule
apexwm

Bill Goodrich : Just as al_langevin pointed out, with Windows Server 2008 there is no Services for Macintosh anymore. It's gone, not available....

1 day ago by apexwm on Windows Server 2008 drops the ball for Mac compatibility