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

Using Pthreads in embedded Linux designs (Part 1)

Posted: 11 Jul 2014     Print Version  Bookmark and Share

Keywords:Linux  POSIX  Pthreads  API  multi-tasking 

This allows new arguments to be added to a pthread object by simply defining a corresponding pair of get and set functions for the API.

Here is part of the attribute API for thread objects:

int pthread_attr_init (pthread_attr_t *attr);
int pthread_attr_destroy (pthread_attr_t *attr);
int pthread_attr_getdetachstate (pthread_attr_t *attr, int *detachstate);
int pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate);

Before it can be used, an attribute object must be initialized. Then any of the attributes defined for that object may be set or retrieved with the appropriate functions. This must be done before the attribute object is used in a call to pthread_create(). If necessary, an attribute object can also be "destroyed". Note that a single attribute object can be used in the creation of multiple threads.

For threads there is only one required attribute, the detach state that we met earlier.

Thread scheduling policies
There are several optional thread attributes that have to do with scheduling policy, that is, how threads are scheduled relative to one another. These attributes and the strategies they represent are only present if the constant _POSIX_THREAD_PRIORITY_SCHEDULING is defined. Without this constant, Pthreads is notreal-time and simply falls back on the default Linux scheduling policy.

A threads implementation that defines _POSIX_THREAD_PRIORITY_SCHEDULING must also define a structure, sched_param, with at least one member, sched_priority, an integer. Interestingly, here is one place where a structure member is made explicitly visible. There is not a pair of functions for setting and getting sched_priority. You simply read and write the structure member directly. Note that sched_param is declared with a struct and not as a typedef.

Here is the API for thread scheduling attributes:

int pthread_attr_setschedparam (pthread_attr_t *attr, const struct sched_param *param);
int pthread_attr_getschedparam (const pthread_attr_t *attr, struct sched_param *param);
int pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
int pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *policy);
int pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
int pthread_attr_getinheritsched (const pthread_attr_t *attr, int *inherit);

int pthread_setschedparam (pthread_t pthread, int *policy, const struct sched_param *param);
int pthread_getschedparam (pthread_t pthread, int *policy, struct sched_param *param);
int sched_get_priority_max (int policy);
int sched_get_priority_min (int policy);

Pthreads defines two real-time scheduling policies that can be applied on a per-thread basis:

SCHED_FIFO: A thread runs until another thread of higher priority becomes ready or until it voluntarily blocks. When a thread with SCHED_FIFO scheduling policy becomes ready, it runs immediately if its priority is higher than that of the running thread.

SCHED_RR: Much like SCHED_FIFO with the addition that a SCHED_RR thread can be preempted by another SCHED_FIFO or SCHED_RR thread of the same or higher priority after a specified time interval or timeslice.

In Linux, the real-time scheduling policies may only be set by processes with superuser privileges. When you wish to explicitly set the scheduling policy or parameters of a thread, you must also set the inheritsched attribute. By default inheritsched is set to PTHREAD_INHERIT_SCHED, meaning that newly created threads will inherit the scheduling policies of the thread that created them. To be able to change scheduling policies you set inheritsched to PTHREAD_EXPLICIT_SCHED.

When threads with SCHED_FIFO or SCHED_RR scheduling policies block waiting for a resource, they wait up in priority order. That is, if several threads are waiting on a resource, the one with the highest priority is scheduled when the resource becomes available. If several threads of the same priority are waiting, the one that has been waiting longest will be scheduled (FIFO order).

You can get the minimum and maximum priority values for each of the real-time scheduling policies through the functions sched_get_priority_min() and sched_get_priority_max(). You can also get and set the scheduling policies of a running thread through pthread_getschedparam()and pthread_setschedparam().

Finally, Pthreads defines another scheduling policy, SCHED_OTHER, which is "not defined." In practice, this becomes the default Linux scheduling policy. In most cases it is a "fairness" algorithm that gradually raises the priority of low priority waiting threads until they run.

About the author
Doug Abbott is the principal of Intellimetrix, a consulting firm specialising in hardware and software for industrial and scientific data acquisition and embedded product applications. He's also a popular instructor and seminar leader, teaching classes on Linux and real-time programming in general. Doug has taught the techniques of embedded programming and multi-tasking operating systems to hundreds of professional engineers. This article is based on a paper he presented as part of a class at the Embedded Systems Conference on "Introduction to Pthreads: Asynchrnous programming in the Unix/Linux Environment (ESC-308).

To download the PDF version of this article, click here.

 First Page Previous Page 1 • 2 • 3 • 4

Comment on "Using Pthreads in embedded Linux des..."
*  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