Share this article:
Blocking and Non-blocking Real Time Operating System APIs
At the heart of a real time operating system (RTOS) is the kernel, which is comprised of the task scheduler and a bunch of services that are available for application programs. Control of the scheduler and access to these services is by means of the kernel’s application program interface (API). APIs differ from one RTOS to another (although there are some standards, like POSIX), but there are some characteristics which are common to many. One of those similarities is the concept of blocking and non-blocking calls.
The idea is fairly straightforward. A program may make an API call to request a specific resource or service. Such a call may normally return with the required result and/or a pointer to requested resources. There may also be the possibility for an error result. But, what if the call is valid, but the resource or service cannot be provided at this time? There are two ways to make such a call, which differ in their response to this unavailability: a blocking call results in the task being suspended (put to sleep) and the task will be woken when the request can be fulfilled; a non-blocking call results in an error code being returned and the task has the option of trying the call again later.
What if several tasks are blocked pending the availability of a resource, which then becomes available? Which task is woken up to have its request satisfied? This depends upon the specific RTOS. Typically tasks are either woken in priority order (i.e. a higher priority task will be woken first) or they are woken in the order in which they suspended (“first in, first out”- FIFO). This may be a kernel configuration choice or may be selectable on a per-object basis.
The choice of using a blocking or non-blocking call is generally up to the applications programmer. Using a non-blocking call is simple to understand – typically the task would put itself to sleep for a while then try again. A blocking call is simpler to code and reduces the use of system resources, as the task remains suspended and not in contention for CPU time until the resource is available.
We can look at a real example. The Nucleus RTOS supports semaphores, which may be created with a call like this:
STATUS NU_Create_Semaphore(NU_SEMAPHORE *semaphore, CHAR *name, UNSIGNED initial_count, OPTION suspend_type);
The last parameter determines how blocked tasks are woken up; the options are priority (NU_PRIORITY) or FIFO (NU_FIFO) order.
Obtaining a semaphore requires a call like this:
STATUS NU_Obtain_Semaphore(NU_SEMAPHORE *semaphore, UNSIGNED suspend);
The blocking/non-blocking behavior is determined by the last parameter. This may be set to NU_SUSPEND (indefinite blocking), NU_NO_SUSPEND (non-blocking), or to a timeout value, which specifies how long (in clock ticks) the task will be suspended for pending the availability of the semaphore.
Colin’s most recent publication, Embedded Software: The Works is available now on the Elsevier Store. Save 30% on his book and other Newnes Press and embedded systems books. Use discount code “SAVE3013″ at checkout.
About the Author
You can read more about Colin and his work on embedded systems at The Colin Walls Blog at Mentor Graphics here. Connect with Colin online here:
Computing functionality is ubiquitous. Today this logic is built into almost any machine you can think of, from home electronics and appliances to motor vehicles, and it governs the infrastructures we depend on daily — telecommunication, public utilities, transportation. Maintaining it all and driving it forward are professionals and researchers in computer science, across disciplines including:
- Computer Architecture and Computer Organization and Design
- Data Management, Big Data, Data Warehousing, Data Mining, and Business Intelligence (BI)
- Human Computer Interaction (HCI), User Experience (UX), User Interface (UI), Interaction Design and Usability
- Artificial intelligence (AI)