Global Sources
EE Times-India
Stay in touch with EE Times India
 
EE Times-India > Embedded
 
 
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 

The Interrupt. Let us digress for a moment to consider the essence of the asynchronous programming paradigm. In real life, "events" often occur asynchronously while you're engaged in some other activity. The alarm goes off while you're sleeping. The boss rushes into your office with some emergency while you're deep in thought on a coding problem. A telemarketer calls to sell you insurance while you're eating dinner.

In all these cases you are "interrupted" from what you were doing and are forced to respond. How you respond depends on the nature and source of the interrupt. You chew out the telemarketer, slam the phone down and go back to eating dinner. The interruption is short if nonetheless irritating. You stop your coding to listen to the boss's perceived emergency. You may have to drop what you're doing and go do something else. When the alarm goes off, there's no question you're going to do something else. It's time to get up.

In computer terms, an interrupt is the processor's response to the occurrence of an event. . The event "interrupts" the current flow of instruction execution invoking another stream of instructions that services the event. When servicing is complete, control normally returns to where the original instruction stream was interrupted. But under supervision of the operating system, control may switch to some other activity.

Interrupts are the basis of high performance, highly responsive computer systems. Perhaps not surprisingly they are also the cause of most of the problems in real-time programming.

Consider a pair of threads acting in a simple producer/consumer paradigm (figure 2). Thread 1 produces data that it writes to a global data structure. Thread 2 consumes the data from the same structure. Thread 1 has higher priority than Thread 2 and we'll assume that this is a preemptive system. (Set aside for the moment that I haven't defined what preemptive is. It should become clear.)

Figure 2: Interrupts cause problems.

Thread 1 produces data in response to some event, i.e. data is available from the source, an A/D converter perhaps. The event is signalled by an interrupt. The interrupt may be from the A/D converter saying that it has finished a conversion, or it may simply be the timer tick saying that the specified time interval has elapsed. This leads to some problems.

The problem should be relatively obvious. The event signalling "data ready" may occur while Thread 2 is in the middle of reading the data structure. Thread 1 preempts Thread 2 and writes new data into the structure. When Thread 2 resumes, it finds inconsistent data.

The Multitasking paradigm. This then is the essence of the real-time programming problem; managing asynchronous events such that they can be serviced only when it is safe to do so.

Fundamentally, multi-tasking is a paradigm for safely and reliably handling asynchronous events. Beyond that it is also a useful way to break a large problem down into a set of much smaller, more tractable problems that may be treated independently. Each part of the problem is implemented as a thread. Each thread does one thing to keep it simple. Then we pretend that all the threads run in parallel.

To reiterate, threads are the way in which multi-tasking is implemented in Unix-like systems. With this background we can now begin exploring the world of Posix threads.

The Threads API
Creating a Thread. The mechanism for creating and managing a thread is analogous to creating and managing a process, as follows:

int pthread_create (pthread_t *thread, pthread_attr_t *attr, void *(*start_ routine) (void *), void *arg);
void pthread_exit (void *retval);
int pthread_join (pthread_t thread, void **thread_return);
pthread_t pthread_self (void);
int sched_yield (void);

The pthread_create() function is like fork() except that the new thread doesn't return from pthread_create() but rather begins execution at start_routine(), which takes one void * argument and returns void * as its value. The arguments to pthread_create() are:
 • pthread_t – A thread object that represents or identifies the thread. pthread_create() initializes this as necessary..
 • Pointer to a thread attribute object. More on that later.
 • Pointer to the start routine.
 • Argument to be passed to the start routine when it is called.

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



Comment on "Using Pthreads in embedded Linux des..."
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

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