2.1.5. POSIX Threads: BITS Pilani

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

BITS Pilani BITS Pilani

Pilani | Dubai | Goa | Hyderabad

2.1.5. POSIX Threads


In the previous segment
BITS Pilani

In the last lecture, we have discussed briefly about

Linux Processes.
In this segment
BITS Pilani

We will discuss about

POSIX Threads in Linux.


POSIX Threads
BITS Pilani
 Historically different Hardware vendors used to implement their proprietary
threads.
 In order to standardise them IEEE 1003.1 series standard was formed, which is
known as POSIX (Programmable Operating System Interface).
 It was specified for UNIX for the first time, and later other Operating Systems
adapted that.
 The advantage of adapting POSIX is that portability of the code using POSIX
threads across operating systems is faster.
 Now a days most of the well-known Operating Systems support POSIX
standards.
 The Threads defined by POSIX standard are known as POSIX Threads or
‘pthread’.
 While compiling code using POSIX thread in UNIX/Linux, you need to explicitly
link the shared library ‘libpthread.so’ by adding the switch ‘-lpthread’ in the
compilation command.
POSIX Thread Creation
BITS Pilani
 pthread_create(), creates a new thread.
 pthread_create arguments:
o thread: An opaque, unique identifier for the new thread returned by the subroutine.
o attr: An opaque attribute object that may be used to set thread attributes. You can specify a
thread attributes object, or NULL for the default values.
o start_routine: the C routine that the thread will execute once it is created.
o arg: A single argument that may be passed to start_routine. It must be passed by reference as
a pointer cast of type void. NULL may be used if no argument is to be passed.
Example:
void *ThreadFn(void *threadid) {

}
int main (int argc, char *argv[]) {
pthread_t threads;
int rc;
printf("In main: creating thread \n");
rc = pthread_create(&threads, NULL, ThreadFn, (void *)0);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
POSIX Thread Attributes
BITS Pilani
 By default, a thread is created with certain attributes.
 Some of these attributes can be changed by the programmer via the thread
attribute object.
 pthread_attr_init and pthread_attr_destroy are used to initialize/destroy the
thread attribute object.
 Other routines are then used to query/set specific attributes in the thread
attribute object.
 Attributes include:
o Detached or joinable state
o Scheduling inheritance
o Scheduling policy
o Scheduling parameters
o Scheduling contention scope
o Stack size
o Stack address
o Stack guard (overflow) size
POSIX Thread Termination
BITS Pilani
Example:
void *ThreadFn(void *threadid) { Several ways in which a thread may
long tid; be terminated:
tid = (long)threadid;  The thread returns normally
printf("Hello World! My thread #%ld!\n", tid); from its starting routine.
pthread_exit(NULL);  The thread makes a call to
} the pthread_exit function.
int main (int argc, char *argv[]) {
 The thread is canceled by
pthread_t threads;
another thread via
int rc;
printf("In main: creating thread \n");
the pthread_cancel function.
rc = pthread_create(&threads[t],  The entire process is
NULL, terminated due to making a
ThreadFn, call to either the exec() or exit()
(void *)0);  If main() finishes first, without
if (rc){ calling pthread_exit explicitly
printf("ERROR; pthread_create() failed\n");
itself.
exit(-1);
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
POSIX Thread – Joining and Detaching Threads
BITS Pilani
"Joining" is the way to accomplish synchronization between threads.
The pthread_join() subroutine blocks the main thread until the specified child
thread terminates.

Parent Thread pthread_create() pthread_join()

Child Thread Do Work pthread_exit()

For this the child thread should be created as joinable.


If the thread is not created joinable and the main thread doesn’t join, then:

The moment the main thread terminates, the child thread will also terminate.
Steps to make the child thread joinable
BITS Pilani
 When a thread is created, one of its attributes defines whether it is joinable or
detached.
 The attr argument in the pthread_create() routine, indicates whether the thread
going to be created is joinable or not.
 Only threads that are created as joinable can be joined.
 If a thread is created as detached, it can never be joined.

 Steps to make a thread joinable:


o Declare a pthread attribute variable of the pthread_attr_t data type
o Initialize the attribute variable with pthread_attr_init()
o Set the attribute detached status with pthread_attr_setdetachstate()
o When done, free library resources used by the attribute
with pthread_attr_destroy()
POSIX Threads – ‘Hello World’ Program
BITS Pilani
#include <pthread.h>
#include <stdio.h> /* Free attribute */
pthread_attr_destroy(&attr);
void *ThreadFn() { /* Wait for the child thread to terminate */
printf("Hello World – From the child rc = pthread_join(thread, &status);
if (rc) {
thread!\n");
printf("ERROR; pthread_join failed\n");
pthread_exit(NULL); pthread_exit(NULL);
} }
int main (int argc, char *argv[]) { printf("Main: program completed. Exiting.\n");
pthread_t thread; pthread_exit(NULL);
pthread_attr_t attr; }
int rc;
long t;
void *status;

/* Initialize and set thread detached


attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_JOINABLE);
Save this file as ‘pthread_helloworld.c’.
printf("Main: creating thread\n"); During compilation, you have to link
rc = pthread_create(&thread, &attr, ThreadFn, pthread library explicitly (libpthread.so)
NULL); Compile it using command:
if (rc) { $ cc pthread_helloworld.c -o
printf("ERROR; thread creation pthread_helloworld -lpthread
failed\n");
pthread_exit(NULL); Then run it by
} $ ./ pthread_helloworld
Summary
BITS Pilani

In this lecture, we have discussed about

POSIX Threads in Linux.

In the next lecture, we will discuss about

Real Time Systems.

You might also like