0% found this document useful (0 votes)
17 views14 pages

Overview of POSIX Threads in OS

The document discusses programming models for shared address space platforms. It describes process-based and thread-based models, with the thread-based model assuming all memory is globally accessible by default. It provides an overview of POSIX threads including basic concepts like each thread having its own registers and stack but sharing the process's address space. It also describes common pthread functions like pthread_create to spawn a new thread and pthread_exit to terminate a thread.

Uploaded by

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

Overview of POSIX Threads in OS

The document discusses programming models for shared address space platforms. It describes process-based and thread-based models, with the thread-based model assuming all memory is globally accessible by default. It provides an overview of POSIX threads including basic concepts like each thread having its own registers and stack but sharing the process's address space. It also describes common pthread functions like pthread_create to spawn a new thread and pthread_exit to terminate a thread.

Uploaded by

KIRAN SAVANDE
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

PROGRAMMING

SHARED ADDRESS
SPACE PLATFORMS
.
Overview of Programming Models
• Programming models provide support for
expressing concurrency and synchronization.
• Process based models assume that all data
associated with a process is private, by default,
unless otherwise specified.
• Lightweight processes and threads assume that
all memory is global.
• Directive based programming models extend the
threaded model by facilitating creation and
synchronization of threads.
Overview of Programming Models
• A thread is a single stream of control in the flow of a program. A
program like:
for (row = 0; row < n; row++)
for (column = 0; column < n; column++)
c[row][column] =
dot_product( get_row(a, row),
get_col(b, col));
can be transformed to:
for (row = 0; row < n; row++)
for (column = 0; column < n; column++)
c[row][column] =
create_thread( dot_product(get_row(a, row),
get_col(b, col)));
In this case, one may think of the thread as an instance of a function
that returns before the function has finished executing.
Thread Basics
• All memory in the logical machine model of a
thread is globally accessible to every thread.

• The stack corresponding to the function call is


generally treated as being local to the thread for
liveness reasons.

• This implies a logical machine model with both


global memory (default) and local memory
(stacks).

• It is important to note that such a flat model may


result in very poor performance since memory is
physically distributed in typical machines.
Thread Basics

• The logical machine model of a thread-based


programming paradigm.
Thread Basics (Advantages)
• Threads provide software portability.
• Inherent support for latency hiding.
• Scheduling and load balancing.
• Ease of programming and widespread use.
POSIX THREAD
(PTHREAD)
POSIX thread
• The POSIX thread libraries are a standards based thread
API for C/C++.

• It allows one to spawn a new concurrent process flow. It is


most effective on multi-processor or multi-core systems
where the process flow can be scheduled to run on
another processor thus gaining speed through parallel or
distributed processing.

• All threads within a process share the same address


space
Thread Basics
• Thread operations include thread creation, termination,
synchronization (joins,blocking), scheduling, data
management and process interaction.
• A thread does not maintain a list of created threads, nor
does it know the thread that created it.
• All threads within a process share the same address space.
• Threads in the same process share:
• Process instructions
• Most data
• open files (descriptors)
• signals and signal handlers
• current working directory
• User and group id
Thread Basics…
• Each thread has a unique:
• Thread ID
• set of registers, stack pointer
• stack for local variables, return addresses
• signal mask
• priority
• Return value: errno

• pthread functions return "0" if OK

• pthread1.c
pthread_create
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
pthread_create…
• Arguments:
• thread - returns the thread id. (unsigned long int defined in
bits/pthreadtypes.h)

• attr - Set to NULL if default thread attributes are used. (else define members
of the struct pthread_attr_t defined in bits/pthreadtypes.h) Attributes include:
• detached state (joinable? Default: PTHREAD_CREATE_JOINABLE. Other option:
PTHREAD_CREATE_DETACHED)
• scheduling policy (real-time?
PTHREAD_INHERIT_SCHED,PTHREAD_EXPLICIT_SCHED,SCHED_OTHER)
• scheduling parameter
• inheritsched attribute (Default: PTHREAD_EXPLICIT_SCHED Inherit from parent
thread: PTHREAD_INHERIT_SCHED)
• scope (Kernel threads: PTHREAD_SCOPE_SYSTEM User threads:
PTHREAD_SCOPE_PROCESS Pick one or the other not both.)
• guard size
• stack address (See unistd.h and bits/posix_opt.h
_POSIX_THREAD_ATTR_STACKADDR)
• stack size (default minimum PTHREAD_STACK_SIZE set in pthread.h),
• .
pthread_create…
• void * (*start_routine) - pointer to the function to
be threaded. Function has a single argument:
pointer to void.

• *arg - pointer to argument of function. To pass


multiple arguments, send a pointer to a
structure
pthread_exit
• void pthread_exit(void *retval);

• Arguments:
• retval - Return value of thread.

• This routine kills the thread. The pthread_exit


function never returns. If the thread is not
detached, the thread id and return value may be
examined from another thread by using
pthread_join.

You might also like