0% found this document useful (0 votes)
117 views12 pages

4.6 Creating and Operating POSIX Threads

This document discusses threads and POSIX threads (Pthreads) for creating multithreaded programs. It defines a thread as an independent stream of instructions that can run simultaneously within a process. Pthreads are a standard for creating threads on UNIX systems. The key advantages of threads are lower overhead than processes and easier context switching. Pthreads are created using pthread_create() and terminated with pthread_exit(). Main threads must wait for child threads to finish using pthread_join() to avoid orphan threads. A sample Pthreads program is shown to create multiple threads, each printing a greeting from its unique thread ID.

Uploaded by

Alpha Placard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views12 pages

4.6 Creating and Operating POSIX Threads

This document discusses threads and POSIX threads (Pthreads) for creating multithreaded programs. It defines a thread as an independent stream of instructions that can run simultaneously within a process. Pthreads are a standard for creating threads on UNIX systems. The key advantages of threads are lower overhead than processes and easier context switching. Pthreads are created using pthread_create() and terminated with pthread_exit(). Main threads must wait for child threads to finish using pthread_join() to avoid orphan threads. A sample Pthreads program is shown to create multiple threads, each printing a greeting from its unique thread ID.

Uploaded by

Alpha Placard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Embedded Systems and RTOS

Session 4.6

Creating and Operating


POSIX Threads

Shripad V Deshpande

Electronics & Telecommunication Engineering


Symbiosis Institute of Technology, Pune
Symbiosis International (Deemed University), Pune
Thread
• An independent stream of instructions that can be
scheduled to run by the OS.
• “Function” that runs independently from main
program.
• Multi-threaded programs run several threads
simultaneously.
• A Thread exists within a process and uses the
process resources.
• Threads only duplicate the essential resources it
needs to be independently schedulable.
• A thread will die if the parent process dies.
• A thread is “lightweight” because most of the
overhead has already been accomplished through the
creation of the process.
2
Advantages and disadvantage of Threads
☺The overhead for creating a thread is
less
☺One process serves multiple clients
☺Switching between threads easier
☻Writing multithreaded programs require
more careful thought
☻More difficult to debug than single
threaded programs
☻For single processor machines, creating
several threads may not necessarily
improve performance
3
Threads Programming Model
• Pipeline model – threads are run one
after the other
• Master-slave model – master (main)
thread doesn't do any work, it just
waits for the slave threads to
finish working
• Equal-worker (or peer) model – all
threads work

4
POSIX Thread (PThread)
• POSIX – Portable Operating System Interface for
Unix.
• For UNIX systems, implementations of threads
that adhere to the IEEE POSIX 1003.1c standard
are Pthreads.
• Pthreads are C language programming types
defined in the pthread.h header/include file.
• Pthreads are best used with programs that can
be organized into discrete, independent tasks
which can execute concurrently.

5
Pthread Management
• The main() function comprises a single,
default thread.
• pthread_create() creates a new thread and
makes it executable.
• The maximum number of threads that may be
created by a process in implementation
dependent.
• Once created, threads are peers, and may
create other threads.
Creating Threads
• pthread_create() creates a new thread and makes it
executable.
int pthread_create( pthread_t *thread, pthread_attr_t *attr, void
*(*thread_function)(void *), void *arg );
• 1st arg – pointer to the identifier of the created
thread
• 2nd arg – thread attributes. If null, then the
thread is created with default attributes
• 3rd arg – pointer to the function the thread will
execute
• 4th arg – the arguments of the executed function
• returns 0 for success
Waiting threads
int pthread_join( pthread_t thread, void **thread_return )
• This function is called in main thread so it
will wait for daughter thread to finish
• 1st arg – the thread to wait for
• 2nd arg – pointer to a pointer to the return
value from the thread
• returns 0 for success
• threads should always be joined; otherwise, a
thread might keep on running even when the
main thread has already terminated
Terminating Threads
• Several ways to terminate a thread:
• The thread is complete and returns
• The pthread_exit() method is called
• The pthread_cancel() method is invoked
• The exit() method is called
• The pthread_exit() routine is called after a
thread has completed its work and is no longer
required to exist.
• If the main program finishes before the thread(s)
do, the other threads will continue to execute if
a pthread_exit() method exists.
• The pthread_exit() method does not close files;
any files opened inside the thread will remain
open, so cleanup must be kept in mind.
Pthread Example
int main (int argc, char *argv[])
#include <pthread.h>
{
#include <stdio.h>
pthread_t threads[NUM_THREADS];
#include <stdlib.h>
int rc, t;
for(t=0; t<NUM_THREADS; t++)
#define NUM_THREADS 5
{
printf("In main: creating thread
void *PrintHello(void *threadid)
%d\n", t);
{
rc = pthread_create(&threads[t],
int tid;
NULL, PrintHello, (void *)t);
tid = (int)threadid;
if (rc)
printf("Hello World! It's me,
{
thread #%d!\n", tid);
printf("ERROR; return code from
pthread_exit(NULL);
pthread_create() is %d\n", rc);
}
exit(-1);
}
}
pthread_exit(NULL);
}
Pthread Example - Output
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread #0!
In main: creating thread 2
Hello World! It's me, thread #1!
Hello World! It's me, thread #2!
In main: creating thread 3
In main: creating thread 4
Hello World! It's me, thread #3!
Hello World! It's me, thread #4!
Next Session 4.7 ->
Memory Management, Virtual Memory

You might also like