SlideShare a Scribd company logo
1 of 40
Download to read offline
SYSTEMS PROGRAMMING
LECTURE 6
THREADING
THREADS
• A typical UNIX process can be thought of as having
a single thread of control
• With multiples threads we can design programs to
do more than one thing at a time. Benefits:
• Simplify code that deal with asynchronous events
• Easy sharing of memory and file descriptors
• Some problems can be partitioned so that overall
program throughput can be improved
• Interactive program can realise improved
response time
THREADS
• A thread consists of the information necessary to
represent an execution context within a process
• Include thread ID, a set of register values, a
stack, a scheduling priority and policy, a signal
mask, an errno variable and thread-specific
data
• Everything within a process is shareable among
the threads in a process, including the text of the
executable, the program’s global and heap
memory, the stacks and file descriptors
THREAD CREATION
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp,
const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),
void restrict *arg);
Return 0 if OK, error number on failure
• The memory location pointed to by tidp is set to the
thread ID of the newly created thread
• The attr argument is used to customize various thread
attributes (NULL for default)
• New thread starts running at the address of start_fn
THREAD CREATION
• start_rtn takes a single argument, arg, which is
a typeless pointer
• If more than one argument are required, a structure
needs to defined, initialised and passed
• When a thread is created there is no guarantee
which runs first
• The new thread has access to the process address
space and inherits the calling thread’s floating point
environment and signal mask
• The set of pending signals for the thread is cleared
THREAD CREATION
#include <pthread.h>
pthread_t ntid;
void *thr_fn(void *arg)
{
printf(“New threadn”);
return (void *) 0;
}
int main(void)
{
pthread_create(&ntid, NULL, thr_fn, NULL);
printf(“Main threadn”);
exit(0);
}
THREAD TERMINATION
• If any thread within a process calls an exit function
then the entire process terminates
• When the default action is to terminate the process, a
signal sent to a thread will terminate the entire
process
• A single thread can exit in three ways:
• Return from the start routine
• Thread can be cancelled
• Thread calls pthread_exit
THREAD TERMINATION
#include <pthread.h>
void pthread_exit(void *rval_ptr);
int pthread_join(pthread_t thread,
void **rval_ptr);
Return 0 if OK, errno on failure
• When joining, the calling thread will block until the
specified thread call pthread_exit, returns from
its start routine or is cancelled
• If cancelled, memory location of rval_ptr is set to
PTHREAD_CANCELLED
THREAD TERMINATION
#include <pthread.h>
pthread_t ntid; void *tret;
void *thr_fn(void *arg)
{
printf(“New threadn”);
return (void *) 0;
}
int main(void)
{
pthread_create(&ntid, NULL, thr_fn, NULL);
printf(“Main threadn”);
pthread_join(ntid, &tret);
exit(0);
}
THREAD TERMINATION
#include <pthread.h>
void pthread_cancel(pthread_t tid);
Return 0 if OK, errno on failure
int pthread_cleanup_push(
void (*rtn) (void *), void *arg);
void pthread_cleanup_pop(int execute)
• pthead_cancel doesn’t wait for thread to
terminate, it merely make the request
• Threads can choose to ignore the cancel request, or
control how it is cancelled
THREAD TERMINATION
• A thread can arrange for functions to be called
when it exists, similar to atexit functions
• They are known as thread cleanup handlers
• More than one handler can be established for
each thread
• Handlers are recorded in a stack, so they are
executed in reverse order from that with which
they were registered
THREAD DETACHING
#include <pthread.h>
void pthead_detach(pthread_t tid);
Return 0 if OK, errno if failure
• By default a thread’s termination status is retained
until pthread_join is called
• A thread’s underlying storage can be reclaimed
immediately on termination if the thread is
detached
• A call to pthread_join for a detached process
will fail, returning EINVAL
THREAD ATTRIBUTES
• detachstate: detached thread attribute
(joinable or detached)
• guardsize: guard buffer size in bytes at the
end of thread stack
• stackaddr: lowest address of thread stack
• stacksize: size in bytes of thread stack
• Remember: threads share the virtual address
space, where threads’ stack reside
• Default guardsize is PAGESIZE bytes
THREAD SYNCHRONISATION
• When multiple threads share the same memory we
need to make sure that each thread sees a
consistent view of the data
• If each thread uses variables that other threads
don’t read or modify, or variables are readonly,
no consistency problems exist
• When one thread can modify a variable that
other thread can read or modify we need to
synchronise the threads to ensure that they don’t
use an invalid value when accessing the memory’s
content
THREAD SYNCHRONISATION
THREAD SYNCHRONISATION
MUTEXES
• A mutex (mutual exclusion lock) is a lock that we
set (lock) before accessing a shared resource and
release (unlock) when we’re done
• While it is set any other thread that tries to set it
will block until we release it
• If more than one thread is blocked when we
unlock the mutex, then they all will be made
runnable and the first one to run will be able to
set the lock
• Others will see mutex still locked and go back
waiting
MUTEXES
#include <pthread.h>
int ptrhead_mutex_init(
pthread_mutex_t *restrict mutex, const
pthread_mutexattr_t restrict addr);
int pthread_mutex_destroy(
pthread_mutex_t *mutex);
Return 0 if OK, errno on error
• A mutex variable is represented by the
pthread_mutex_t data type
• Mutexes must be initialised by either setting it to the
constant PRTHEAD_MUTEX_INITIALISER (for
static mutexes) or called pthread_mutex_init
MUTEXES
#include <pthread.h>
int pthread_mutex_lock(
pthread_mutex_t *mutex);
int pthread_mutex_trylock(
pthread_mutex_t *mutex);
int pthread_mutex_unlock(
pthread_mutex_t *mutex);
Return 0 if OK, errno on failure
• If thread can’t afford to block for a mutex to become
unlocked, pthread_mutex_trylock can be
used to lock the mutex conditionally
MUTEXES
See thread_mutexes.c
DEADLOCK AVOIDANCE
• A thread will deadlock itself if it tries to lock the
same mutex twice
• There are less obvious ways how deadlock can
occur
• They can be avoided by carefully controlling the
order in which mutexes are locked
• In case of complex program architectures, you
can try using pthread_mutex_lock to
avoid deadlocking
SYNCHRONISATION ATTRIBUTES
• By default, multiple threads can access the same
synchronisation objects (process-shared mutex =
PTHREAD_PROCESS_PRIVATE)
• Mechanism exist which allow independent processes
to map the same extent of memory into their
independent memory space
• If process-shared mutex =
PTHREAD_PROCESS_SHARED, a mutex
allocated from a shared memory extent may be
used for synchronisation by those processes
SYNCHRONISATION ATTRIBUTES
• type mutex attribute controls the characteristics of
the mutex:
• normal – standard mutex that doesn’t do any
special error checking or deadlock detection
• errorcheck – provides error checking
• recursive – allows the same thread to lock it
multiple times without first unlocking it (using
internal lock count)
• default – request default semantics
(implementation specific)
READERWRITER LOCKS
• RW locks allow for higher degrees of parallelism
• Three states are possible: locked in read mode, locked
in write mode, unlocked
• Only one thread at a time can hold a RW lock in write
mode, however multiple thread can hold it in read
mode at the same time
• When write-locked, all threads trying to lock it block
until unlocked
• When in read-mode, all thread trying to lock it in read
mode are given access, however threads attempting to
lock in write mode are blocked
READERWRITER LOCKS
• RW locks usually block additional readers if a
block is already held in read mode and a thread is
blocked trying to acquire it in write mode
• This avoids writer starvation
• RW locks are well suited for situations where data
structures are read more often than they are
modified
• RW are also called shared exclusive locks
• They must be initialised before use and destroyed
before freeing their underlying memory
READERWRITER LOCKS
#include <pthread.h>
int pthread_rw_lock_init(
pthread_rw_lock_t *restrict rwlock,
const pthread_rwlockattr_t attr);
int pthread_rwlock_destroy(
pthread_rwlock_t *rwlock)
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
int pthread_rwlock_tryrdlock(
pthread_rwlock_t *rwlock)
int pthread_rwlock_trywrlock(
pthread_rwlock_t *rwlock)
Return 0 if OK, errno if failure
CONDITION VARIABLES
• Synchronisation mechanism for threads
• Provide a place for threads to rendevouz
• When used with mutexes, condition variables allow
threads to wait in a race-free way for arbitrary
conditions to occur
• A thread must first lock a mutex to change the
condition state
• Other thread won’t notice the change until they
acquire the mutex
• Initialised with PTHREAD_COND_INITIALIZER
CONDITION VARIABLES
#include <pthread.h>
int pthread_cond_init(
pthread_cond_t *restrict cond,
pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(
pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
cont struct timespec *restrict timeout
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
Return 0 if OK, errno on failure
CONDITION VARIABLES
See condition_variables.c
BARRIERS
• Synchronisation mechanism that lets you corral
several cooperating threads, forcing them to
wait at a specific point until all have finished
before any one thread can continue
• Unlike pthread_join, you’re waiting for the
threads to rendevouz at a cerain point
• When the specified number of threads arrive at
a barrier we unlock all of them so they can
continue to run.
BARRIERS
#include <pthread.h>
int pthread_barrier_init(
pthread_barrier_t *barrier,
const pthread_barrierattr_t *attr,
unsigned int count);
int pthread_barrier_destroy(
pthread_barrier_t *barrier);
int pthread_barrier_wait(
pthread_barrier_t *barrier);
Return 0 if OK, errno on error
• count holds the number of thread that must call
pthread_barrier_wait
CONDITION VARIABLES
See barriers.c
THREAD-SPECIFIC DATA
• Also known as thread-private data
• A mechanism for storing and finding data
associated with a particular thread
• Each thread accesses its own separate copy of the
data without worrying about synchronising access
with other threads
• For example, this is useful to redefine errno such
that each thread has it’s own private version
• Except for using registers, there is no way for one
thread to prevent another from accessing its data.
THREAD-SPECIFIC DATA
#include <pthread.h>
int pthread_key_create(pthread_key_t *keyp,
void (*destructor) (void *));
int pthread_key_delete(pthread_key_t *key);
Return 0 if OK, errno on failure
• Before allocating thread-specific data we need to
create a key to associate with the data
• Same key can be used by all threads in the process,
but each thread will associate a different thread-
specific data address with the key
THREAD-SPECIFIC DATA
• pthread_key_create also associates an
optional destructor function with the key
• When thread exits the destructor is called with the
data address as the only argument
• Threads generally use malloc to allocate thread-
specific data, the destructor usually frees the
allocated memory
• When thread exits the destructors are called in an
implementation-defined order
• pthread_key_delete will not invoke the
destructor
THREAD-SPECIFIC DATA
• Depending on how the system schedules threads,
some threads might see one key value, whereas
other threads migh see a different key value
• This race can be solved by using pthread_once
• initflag must be a nonlocal variable and
initialised
#include <pthread.h>
pthread_once_t = PTHREAD_ONCE_INIT;
int pthread_once(pthread_once_t *initflag,
void (*init_routine)(void));
THREAD-SPECIFIC DATA
void destructor(void *)
pthread_key_t key;
pthread_once_t init_done = PTRHEAD_ONCE_INIT;
void thread_init(void)
{
err = pthread_key_create(&key, destructor);
}
int threadfunc(void *arg)
{
pthread_once(&init_done, thread_init);
...
}
THREAD-SPECIFIC DATA
• Once a key is created we can associate thread-
specific data with the key
• Once they are set, we can access the data anytime
#include <pthread.h>
void *pthread_getspecific(pthread_key_t key);
Returns thread-specific data or NULL if no
value has been associated with the key
int pthread_setspecific(pthread_key_t key,
cont void *value);
Return 0 of OK, errno on failure
THREADS AND SIGNALS
• Each thread has its own signal mask, but signal
disposition is shared by all threads in a process
• Individual threads can block signals, but when a
thread modifies the action associated with a signal
all threads share the action
• Signals are delivered to a single thread in the
process
• If the signal is related to a hardware fault or timer
it is sent to the thread whose action caused the
event
• Other signals are delivered arbitrarily
THREADS AND SIGNALS
#include <pthread.h>
int pthread_sigmask(int how,
const sigset_t *restrict set,
sigset_t *restrict oset
int sigwait(const sigset_t *restrict set,
int restrict signop);
int pthread_kill(pthread_t thread, int signo);
Return 0 if OK, errno on failure
• sigprocmask is undefined in a multithreaded
process, so pthread_signals has to be used.
• A thread can wait for one or more signals by calling
sigwait

More Related Content

What's hot (16)

Java and the machine - Martijn Verburg and Kirk Pepperdine
Java and the machine - Martijn Verburg and Kirk PepperdineJava and the machine - Martijn Verburg and Kirk Pepperdine
Java and the machine - Martijn Verburg and Kirk Pepperdine
 
Java threading
Java threadingJava threading
Java threading
 
Java synchronizers
Java synchronizersJava synchronizers
Java synchronizers
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Computer networkppt4577
Computer networkppt4577Computer networkppt4577
Computer networkppt4577
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Java threads
Java threadsJava threads
Java threads
 
Threads
ThreadsThreads
Threads
 
OOP for Hardware Verification--Demystified!
OOP for Hardware Verification--Demystified! OOP for Hardware Verification--Demystified!
OOP for Hardware Verification--Demystified!
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
Jnp
JnpJnp
Jnp
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
 

Viewers also liked

Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...
Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...
Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...fruitmax
 
Nuliv An Overview Of Nu Liv Science 2010 6 21
Nuliv An Overview Of Nu Liv Science 2010 6 21Nuliv An Overview Of Nu Liv Science 2010 6 21
Nuliv An Overview Of Nu Liv Science 2010 6 21fruitmax
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715HelpWithAssignment.com
 
Personal Memoir Slideshow
Personal Memoir SlideshowPersonal Memoir Slideshow
Personal Memoir SlideshowJosh Maroney
 
Elk Canada
Elk CanadaElk Canada
Elk Canadafruitmax
 
Horario 2012_ Ingeniería Electrónica
Horario 2012_ Ingeniería  Electrónica Horario 2012_ Ingeniería  Electrónica
Horario 2012_ Ingeniería Electrónica andrea botia
 
Get Multiple Regression Assignment Help
Get Multiple Regression Assignment Help Get Multiple Regression Assignment Help
Get Multiple Regression Assignment Help HelpWithAssignment.com
 
Horario 2012 1_electronica_yopal (1)
Horario 2012 1_electronica_yopal (1)Horario 2012 1_electronica_yopal (1)
Horario 2012 1_electronica_yopal (1)andrea botia
 
"営業マン"な自社サイトを作る
"営業マン"な自社サイトを作る"営業マン"な自社サイトを作る
"営業マン"な自社サイトを作るDigical Media
 
New England Business Expo 2009
New England Business Expo 2009New England Business Expo 2009
New England Business Expo 2009Dennys_Catering
 
株式会社デジカルメディア事業部 サービス紹介 Ver1.0
株式会社デジカルメディア事業部 サービス紹介 Ver1.0株式会社デジカルメディア事業部 サービス紹介 Ver1.0
株式会社デジカルメディア事業部 サービス紹介 Ver1.0Digical Media
 
Server and Client side comparision
Server and Client side comparisionServer and Client side comparision
Server and Client side comparisionStew Duncan
 

Viewers also liked (16)

Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...
Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...
Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...
 
Nuliv An Overview Of Nu Liv Science 2010 6 21
Nuliv An Overview Of Nu Liv Science 2010 6 21Nuliv An Overview Of Nu Liv Science 2010 6 21
Nuliv An Overview Of Nu Liv Science 2010 6 21
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
 
Personal Memoir Slideshow
Personal Memoir SlideshowPersonal Memoir Slideshow
Personal Memoir Slideshow
 
Elk Canada
Elk CanadaElk Canada
Elk Canada
 
Horario 2012_ Ingeniería Electrónica
Horario 2012_ Ingeniería  Electrónica Horario 2012_ Ingeniería  Electrónica
Horario 2012_ Ingeniería Electrónica
 
Organizational Change
Organizational ChangeOrganizational Change
Organizational Change
 
Satyam Scam
Satyam ScamSatyam Scam
Satyam Scam
 
Get Multiple Regression Assignment Help
Get Multiple Regression Assignment Help Get Multiple Regression Assignment Help
Get Multiple Regression Assignment Help
 
Lan2
Lan2Lan2
Lan2
 
Horario 2012 1_electronica_yopal (1)
Horario 2012 1_electronica_yopal (1)Horario 2012 1_electronica_yopal (1)
Horario 2012 1_electronica_yopal (1)
 
"営業マン"な自社サイトを作る
"営業マン"な自社サイトを作る"営業マン"な自社サイトを作る
"営業マン"な自社サイトを作る
 
New England Business Expo 2009
New England Business Expo 2009New England Business Expo 2009
New England Business Expo 2009
 
株式会社デジカルメディア事業部 サービス紹介 Ver1.0
株式会社デジカルメディア事業部 サービス紹介 Ver1.0株式会社デジカルメディア事業部 サービス紹介 Ver1.0
株式会社デジカルメディア事業部 サービス紹介 Ver1.0
 
Server and Client side comparision
Server and Client side comparisionServer and Client side comparision
Server and Client side comparision
 

Similar to System Programming - Threading

Similar to System Programming - Threading (20)

Chap7 slides
Chap7 slidesChap7 slides
Chap7 slides
 
P threads
P threadsP threads
P threads
 
OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3
 
Threads
ThreadsThreads
Threads
 
Threading
ThreadingThreading
Threading
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
 
PDCCLECTUREE.pptx
PDCCLECTUREE.pptxPDCCLECTUREE.pptx
PDCCLECTUREE.pptx
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 
Sysprog 14
Sysprog 14Sysprog 14
Sysprog 14
 
posix.pdf
posix.pdfposix.pdf
posix.pdf
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
 
Tutorial4 Threads
Tutorial4  ThreadsTutorial4  Threads
Tutorial4 Threads
 
MULTI THREADING.pptx
MULTI THREADING.pptxMULTI THREADING.pptx
MULTI THREADING.pptx
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

System Programming - Threading

  • 2. THREADS • A typical UNIX process can be thought of as having a single thread of control • With multiples threads we can design programs to do more than one thing at a time. Benefits: • Simplify code that deal with asynchronous events • Easy sharing of memory and file descriptors • Some problems can be partitioned so that overall program throughput can be improved • Interactive program can realise improved response time
  • 3. THREADS • A thread consists of the information necessary to represent an execution context within a process • Include thread ID, a set of register values, a stack, a scheduling priority and policy, a signal mask, an errno variable and thread-specific data • Everything within a process is shareable among the threads in a process, including the text of the executable, the program’s global and heap memory, the stacks and file descriptors
  • 4. THREAD CREATION #include <pthread.h> int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void restrict *arg); Return 0 if OK, error number on failure • The memory location pointed to by tidp is set to the thread ID of the newly created thread • The attr argument is used to customize various thread attributes (NULL for default) • New thread starts running at the address of start_fn
  • 5. THREAD CREATION • start_rtn takes a single argument, arg, which is a typeless pointer • If more than one argument are required, a structure needs to defined, initialised and passed • When a thread is created there is no guarantee which runs first • The new thread has access to the process address space and inherits the calling thread’s floating point environment and signal mask • The set of pending signals for the thread is cleared
  • 6. THREAD CREATION #include <pthread.h> pthread_t ntid; void *thr_fn(void *arg) { printf(“New threadn”); return (void *) 0; } int main(void) { pthread_create(&ntid, NULL, thr_fn, NULL); printf(“Main threadn”); exit(0); }
  • 7. THREAD TERMINATION • If any thread within a process calls an exit function then the entire process terminates • When the default action is to terminate the process, a signal sent to a thread will terminate the entire process • A single thread can exit in three ways: • Return from the start routine • Thread can be cancelled • Thread calls pthread_exit
  • 8. THREAD TERMINATION #include <pthread.h> void pthread_exit(void *rval_ptr); int pthread_join(pthread_t thread, void **rval_ptr); Return 0 if OK, errno on failure • When joining, the calling thread will block until the specified thread call pthread_exit, returns from its start routine or is cancelled • If cancelled, memory location of rval_ptr is set to PTHREAD_CANCELLED
  • 9. THREAD TERMINATION #include <pthread.h> pthread_t ntid; void *tret; void *thr_fn(void *arg) { printf(“New threadn”); return (void *) 0; } int main(void) { pthread_create(&ntid, NULL, thr_fn, NULL); printf(“Main threadn”); pthread_join(ntid, &tret); exit(0); }
  • 10. THREAD TERMINATION #include <pthread.h> void pthread_cancel(pthread_t tid); Return 0 if OK, errno on failure int pthread_cleanup_push( void (*rtn) (void *), void *arg); void pthread_cleanup_pop(int execute) • pthead_cancel doesn’t wait for thread to terminate, it merely make the request • Threads can choose to ignore the cancel request, or control how it is cancelled
  • 11. THREAD TERMINATION • A thread can arrange for functions to be called when it exists, similar to atexit functions • They are known as thread cleanup handlers • More than one handler can be established for each thread • Handlers are recorded in a stack, so they are executed in reverse order from that with which they were registered
  • 12. THREAD DETACHING #include <pthread.h> void pthead_detach(pthread_t tid); Return 0 if OK, errno if failure • By default a thread’s termination status is retained until pthread_join is called • A thread’s underlying storage can be reclaimed immediately on termination if the thread is detached • A call to pthread_join for a detached process will fail, returning EINVAL
  • 13. THREAD ATTRIBUTES • detachstate: detached thread attribute (joinable or detached) • guardsize: guard buffer size in bytes at the end of thread stack • stackaddr: lowest address of thread stack • stacksize: size in bytes of thread stack • Remember: threads share the virtual address space, where threads’ stack reside • Default guardsize is PAGESIZE bytes
  • 14. THREAD SYNCHRONISATION • When multiple threads share the same memory we need to make sure that each thread sees a consistent view of the data • If each thread uses variables that other threads don’t read or modify, or variables are readonly, no consistency problems exist • When one thread can modify a variable that other thread can read or modify we need to synchronise the threads to ensure that they don’t use an invalid value when accessing the memory’s content
  • 17. MUTEXES • A mutex (mutual exclusion lock) is a lock that we set (lock) before accessing a shared resource and release (unlock) when we’re done • While it is set any other thread that tries to set it will block until we release it • If more than one thread is blocked when we unlock the mutex, then they all will be made runnable and the first one to run will be able to set the lock • Others will see mutex still locked and go back waiting
  • 18. MUTEXES #include <pthread.h> int ptrhead_mutex_init( pthread_mutex_t *restrict mutex, const pthread_mutexattr_t restrict addr); int pthread_mutex_destroy( pthread_mutex_t *mutex); Return 0 if OK, errno on error • A mutex variable is represented by the pthread_mutex_t data type • Mutexes must be initialised by either setting it to the constant PRTHEAD_MUTEX_INITIALISER (for static mutexes) or called pthread_mutex_init
  • 19. MUTEXES #include <pthread.h> int pthread_mutex_lock( pthread_mutex_t *mutex); int pthread_mutex_trylock( pthread_mutex_t *mutex); int pthread_mutex_unlock( pthread_mutex_t *mutex); Return 0 if OK, errno on failure • If thread can’t afford to block for a mutex to become unlocked, pthread_mutex_trylock can be used to lock the mutex conditionally
  • 21. DEADLOCK AVOIDANCE • A thread will deadlock itself if it tries to lock the same mutex twice • There are less obvious ways how deadlock can occur • They can be avoided by carefully controlling the order in which mutexes are locked • In case of complex program architectures, you can try using pthread_mutex_lock to avoid deadlocking
  • 22. SYNCHRONISATION ATTRIBUTES • By default, multiple threads can access the same synchronisation objects (process-shared mutex = PTHREAD_PROCESS_PRIVATE) • Mechanism exist which allow independent processes to map the same extent of memory into their independent memory space • If process-shared mutex = PTHREAD_PROCESS_SHARED, a mutex allocated from a shared memory extent may be used for synchronisation by those processes
  • 23. SYNCHRONISATION ATTRIBUTES • type mutex attribute controls the characteristics of the mutex: • normal – standard mutex that doesn’t do any special error checking or deadlock detection • errorcheck – provides error checking • recursive – allows the same thread to lock it multiple times without first unlocking it (using internal lock count) • default – request default semantics (implementation specific)
  • 24. READERWRITER LOCKS • RW locks allow for higher degrees of parallelism • Three states are possible: locked in read mode, locked in write mode, unlocked • Only one thread at a time can hold a RW lock in write mode, however multiple thread can hold it in read mode at the same time • When write-locked, all threads trying to lock it block until unlocked • When in read-mode, all thread trying to lock it in read mode are given access, however threads attempting to lock in write mode are blocked
  • 25. READERWRITER LOCKS • RW locks usually block additional readers if a block is already held in read mode and a thread is blocked trying to acquire it in write mode • This avoids writer starvation • RW locks are well suited for situations where data structures are read more often than they are modified • RW are also called shared exclusive locks • They must be initialised before use and destroyed before freeing their underlying memory
  • 26. READERWRITER LOCKS #include <pthread.h> int pthread_rw_lock_init( pthread_rw_lock_t *restrict rwlock, const pthread_rwlockattr_t attr); int pthread_rwlock_destroy( pthread_rwlock_t *rwlock) int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) int pthread_rwlock_tryrdlock( pthread_rwlock_t *rwlock) int pthread_rwlock_trywrlock( pthread_rwlock_t *rwlock) Return 0 if OK, errno if failure
  • 27. CONDITION VARIABLES • Synchronisation mechanism for threads • Provide a place for threads to rendevouz • When used with mutexes, condition variables allow threads to wait in a race-free way for arbitrary conditions to occur • A thread must first lock a mutex to change the condition state • Other thread won’t notice the change until they acquire the mutex • Initialised with PTHREAD_COND_INITIALIZER
  • 28. CONDITION VARIABLES #include <pthread.h> int pthread_cond_init( pthread_cond_t *restrict cond, pthread_condattr_t *restrict attr); int pthread_cond_destroy(pthread_cond_t *cond); int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); int pthread_cond_timedwait( pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cont struct timespec *restrict timeout int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); Return 0 if OK, errno on failure
  • 30. BARRIERS • Synchronisation mechanism that lets you corral several cooperating threads, forcing them to wait at a specific point until all have finished before any one thread can continue • Unlike pthread_join, you’re waiting for the threads to rendevouz at a cerain point • When the specified number of threads arrive at a barrier we unlock all of them so they can continue to run.
  • 31. BARRIERS #include <pthread.h> int pthread_barrier_init( pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count); int pthread_barrier_destroy( pthread_barrier_t *barrier); int pthread_barrier_wait( pthread_barrier_t *barrier); Return 0 if OK, errno on error • count holds the number of thread that must call pthread_barrier_wait
  • 33. THREAD-SPECIFIC DATA • Also known as thread-private data • A mechanism for storing and finding data associated with a particular thread • Each thread accesses its own separate copy of the data without worrying about synchronising access with other threads • For example, this is useful to redefine errno such that each thread has it’s own private version • Except for using registers, there is no way for one thread to prevent another from accessing its data.
  • 34. THREAD-SPECIFIC DATA #include <pthread.h> int pthread_key_create(pthread_key_t *keyp, void (*destructor) (void *)); int pthread_key_delete(pthread_key_t *key); Return 0 if OK, errno on failure • Before allocating thread-specific data we need to create a key to associate with the data • Same key can be used by all threads in the process, but each thread will associate a different thread- specific data address with the key
  • 35. THREAD-SPECIFIC DATA • pthread_key_create also associates an optional destructor function with the key • When thread exits the destructor is called with the data address as the only argument • Threads generally use malloc to allocate thread- specific data, the destructor usually frees the allocated memory • When thread exits the destructors are called in an implementation-defined order • pthread_key_delete will not invoke the destructor
  • 36. THREAD-SPECIFIC DATA • Depending on how the system schedules threads, some threads might see one key value, whereas other threads migh see a different key value • This race can be solved by using pthread_once • initflag must be a nonlocal variable and initialised #include <pthread.h> pthread_once_t = PTHREAD_ONCE_INIT; int pthread_once(pthread_once_t *initflag, void (*init_routine)(void));
  • 37. THREAD-SPECIFIC DATA void destructor(void *) pthread_key_t key; pthread_once_t init_done = PTRHEAD_ONCE_INIT; void thread_init(void) { err = pthread_key_create(&key, destructor); } int threadfunc(void *arg) { pthread_once(&init_done, thread_init); ... }
  • 38. THREAD-SPECIFIC DATA • Once a key is created we can associate thread- specific data with the key • Once they are set, we can access the data anytime #include <pthread.h> void *pthread_getspecific(pthread_key_t key); Returns thread-specific data or NULL if no value has been associated with the key int pthread_setspecific(pthread_key_t key, cont void *value); Return 0 of OK, errno on failure
  • 39. THREADS AND SIGNALS • Each thread has its own signal mask, but signal disposition is shared by all threads in a process • Individual threads can block signals, but when a thread modifies the action associated with a signal all threads share the action • Signals are delivered to a single thread in the process • If the signal is related to a hardware fault or timer it is sent to the thread whose action caused the event • Other signals are delivered arbitrarily
  • 40. THREADS AND SIGNALS #include <pthread.h> int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict oset int sigwait(const sigset_t *restrict set, int restrict signop); int pthread_kill(pthread_t thread, int signo); Return 0 if OK, errno on failure • sigprocmask is undefined in a multithreaded process, so pthread_signals has to be used. • A thread can wait for one or more signals by calling sigwait