1gwu cs 259 brad taylor spring 2004 systems programming meeting 6: posix threads, semaphores and...

92
U CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: Meeting 6: POSIX Threads, Semaphores POSIX Threads, Semaphores and Synchronization and Synchronization

Upload: elfreda-brown

Post on 04-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

1GWU CS 259 Brad Taylor Spring 2004

Systems Programming

Meeting 6:Meeting 6:

POSIX Threads, Semaphores POSIX Threads, Semaphores and Synchronizationand Synchronization

Page 2: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

2GWU CS 259 Brad Taylor Spring 2004

Objectives

•POSIX ThreadsPOSIX Threads

•Project Status BriefsProject Status Briefs

•SynchronizationSynchronization

•SemaphoresSemaphores

Page 3: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

3GWU CS 259 Brad Taylor Spring 2004

Seeking Interested Students

• Middleware ++Middleware ++• HabitatsHabitats• Complex Systems ProposalsComplex Systems Proposals• Interested Masters & Doctorate Interested Masters & Doctorate Students …Students …• Weekly Wednesday evening Weekly Wednesday evening meeting ~ 6:10 to 8:30meeting ~ 6:10 to 8:30• Computer Science Conf Room #736Computer Science Conf Room #736• Food Provided!!! (usually snacks, Food Provided!!! (usually snacks, but you never know)but you never know)• Many papers & degrees generatedMany papers & degrees generated• No ties required ;)No ties required ;)

Page 4: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

4GWU CS 259 Brad Taylor Spring 2004

POSIX Threads

• Pthreads: What are they? Why use Pthreads: What are they? Why use them?them?

• What is their terminology?What is their terminology?• How do they work?How do they work? • How to deal with multiple threads? How to deal with multiple threads?

Concurrency …Concurrency …• Multithreading ModelsMultithreading Models• Threading IssuesThreading Issues

Page 5: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

5GWU CS 259 Brad Taylor Spring 2004

Review: What is a thread?

In theory: Turing machineIn theory: Turing machine– tape (state), tape head (position)tape (state), tape head (position)

In practice: What’s needed to run code on In practice: What’s needed to run code on CPUCPU– ““execution stream in an execution context” execution stream in an execution context” – Execution stream: sequential seq. of instructionsExecution stream: sequential seq. of instructions

CPU execution context (1 thread)CPU execution context (1 thread)– State: stack, heap, registers State: stack, heap, registers – Position: program counter registerPosition: program counter register

OS execution context (n threads):OS execution context (n threads):– identity + open file descriptors, page table, …identity + open file descriptors, page table, …

add r1, r2, r3 sub r2, r3, r10

st r2, 0(r1)…

In practice, each thread has own copy of program counter, stack, registers. Share memoryIn practice, each thread has own copy of program counter, stack, registers. Share memory

Page 6: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

6GWU CS 259 Brad Taylor Spring 2004

Threads vs Procedures

• Threads may resume out of order: Threads may resume out of order: – Cannot use LIFO stack to save stateCannot use LIFO stack to save state– General solution: duplicate stacksGeneral solution: duplicate stacks

• Threads switch less often: Threads switch less often: – Don’t partition registers (why?)Don’t partition registers (why?)

• Involuntarily interrupted threads:Involuntarily interrupted threads:– Synchronous: procedure call can use compiler to save Synchronous: procedure call can use compiler to save

statestate– Asynchronous: thread switch code saves all registersAsynchronous: thread switch code saves all registers

• More than one than one thread can runMore than one than one thread can run– Thread scheduling: what to overlay on CPU next? Thread scheduling: what to overlay on CPU next? – Procedure call scheduling obvious: run called procedureProcedure call scheduling obvious: run called procedure

Page 7: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

7GWU CS 259 Brad Taylor Spring 2004

Processes vs. Threads

• Differences: How to share data? How to communicate? Differences: How to share data? How to communicate? • Processes (or Linux Tasks)Processes (or Linux Tasks)

– Dispatching UnitDispatching Unit: set of threads (computational entities) : set of threads (computational entities) – Collection of resourcesCollection of resources: Processors, other processes, files, IO, shared memory: Processors, other processes, files, IO, shared memory– Different processes have different privileges: switch OS user perceptionDifferent processes have different privileges: switch OS user perception– Different address spaces: switch paging table, etc.Different address spaces: switch paging table, etc.– Different than procedures: OS, not compiler, manages state savingDifferent than procedures: OS, not compiler, manages state saving

• Threads (or Lightweight Processes)Threads (or Lightweight Processes)– Represent Represent dynamic objectdynamic object (execution path and computational state) (execution path and computational state)– Own computational stateOwn computational state: PC, stack, user registers, private data (auto : PC, stack, user registers, private data (auto

variables)variables)– Remaining resources are shared among threads in a processRemaining resources are shared among threads in a process

• Protection:Protection:– Saving state in safe place (OS)Saving state in safe place (OS)– Support forcibly revoking processorSupport forcibly revoking processor– Prevent impostersPrevent imposters

Page 8: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

8GWU CS 259 Brad Taylor Spring 2004

Single and Multithreaded Processes

Page 9: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

9GWU CS 259 Brad Taylor Spring 2004

Benefits of Threads Sharing Process Address Space over

Processes

•ResponsivenessResponsiveness:: – Create Create new ornew or Terminate Terminate existing thread and existing thread and switchswitch

between process threads between process threads fasterfaster– Application with many independent activities redesigned Application with many independent activities redesigned

for each as a thread; example: in multithreaded GUI, user for each as a thread; example: in multithreaded GUI, user may start another activity before completing previous onemay start another activity before completing previous one

•Resource SharingResource Sharing– Programs accessing common data through shared memory Programs accessing common data through shared memory

by two or more processes already use more than one by two or more processes already use more than one thread of controlthread of control

– Each process has a full address space and operating Each process has a full address space and operating systems state; replicating state information makes each systems state; replicating state information makes each process more expensive than a thread in both time and process more expensive than a thread in both time and spacespace

– Inherent process separation requires extra programming Inherent process separation requires extra programming effort communicating and synchronizing different process’ effort communicating and synchronizing different process’ threads actions threads actions

Page 10: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

10GWU CS 259 Brad Taylor Spring 2004

Benefits of Sharing (con’t)

•EconomyEconomy:: – Communication overhead lower sharing address spaceCommunication overhead lower sharing address space– Data produced by one thread immediately available to all threads Data produced by one thread immediately available to all threads

•Utilization of Multi-Processor Architectures:Utilization of Multi-Processor Architectures:– Applications expressing concurrency requirements with threads Applications expressing concurrency requirements with threads

need not consider number of available processorsneed not consider number of available processors– Application performance improves transparently with additional Application performance improves transparently with additional

processorsprocessors– Implemented as threads on a multiprocessor, numerical Implemented as threads on a multiprocessor, numerical

algorithms with high degree of parallelism (such as matrix algorithms with high degree of parallelism (such as matrix multiplications) can run much fastermultiplications) can run much faster

•Improved program structureImproved program structure – Many programs more efficiently structured as multiple Many programs more efficiently structured as multiple

independent or semi-independent units of execution vs single, independent or semi-independent units of execution vs single, monolithic threadmonolithic thread

– Multithreaded programs more adaptive to user demand variations Multithreaded programs more adaptive to user demand variations than single threaded programsthan single threaded programs

Page 11: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

11GWU CS 259 Brad Taylor Spring 2004

Threads Terminology

ParallelismParallelism: Degree to which a multiprocessor : Degree to which a multiprocessor application achieves parallel executionapplication achieves parallel execution

ConcurrencyConcurrency: Maximum parallelism an : Maximum parallelism an application can achieve with unlimited application can achieve with unlimited processorsprocessors

System ConcurrencySystem Concurrency: Kernel recognizes multiple : Kernel recognizes multiple program control threadsprogram control threads

User ConcurrencyUser Concurrency: User space threads (library) : User space threads (library) provides natural concurrent applications provides natural concurrent applications programming model, supported thread programming model, supported thread runtime process code, not OSruntime process code, not OS

Page 12: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

12GWU CS 259 Brad Taylor Spring 2004

Threading Models

User level threadsUser level threads - user libraries implementation - user libraries implementation– Benefits: Benefits: no kernel modifications, flexible and low costno kernel modifications, flexible and low cost– Drawbacks: Drawbacks: thread library call, I/O, etc. may block entire process, no thread library call, I/O, etc. may block entire process, no

parallelism (i.e., 2 threads in process can’t run on 2 processors)parallelism (i.e., 2 threads in process can’t run on 2 processors)Kernel level threadsKernel level threads - kernel directly supports multiple process threads - kernel directly supports multiple process threads

(and multiple processors, if available)(and multiple processors, if available)– Benefits: Benefits: scheduling/synchronization coordination, less overhead than scheduling/synchronization coordination, less overhead than

process, suitable for parallel applicationprocess, suitable for parallel application– Drawbacks: Drawbacks: more expensive than user-level threads, generality leads more expensive than user-level threads, generality leads

to greater overheadto greater overheadLight-Weight ProcessesLight-Weight Processes (LWP) - kernel supported user thread (LWP) - kernel supported user thread

– LWP bound to kernel thread, not the converseLWP bound to kernel thread, not the converse– LWP is scheduled by kernelLWP is scheduled by kernel– User threads scheduled by library onto LWPsUser threads scheduled by library onto LWPs– Multiple LWPs per processMultiple LWPs per process

Hybrid ApproachHybrid Approach - combine the best of user and kernel approaches - combine the best of user and kernel approaches

Page 13: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

13GWU CS 259 Brad Taylor Spring 2004

Multithreading Models

• Many-to-OneMany-to-One

• One-to-OneOne-to-One

• Many-to-ManyMany-to-Many

Page 14: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

14GWU CS 259 Brad Taylor Spring 2004

Many-to-One (N:1)

• Many user-level Many user-level threads mapped threads mapped to single kernel to single kernel threadthread

• Used on systems Used on systems that do not that do not support kernel support kernel threadsthreads

Page 15: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

15GWU CS 259 Brad Taylor Spring 2004

One-to-One (1:1)

•Each user-level thread maps to kernel thread.Each user-level thread maps to kernel thread.

•ExamplesExamples- Windows 95/98/NT/2000- Windows 95/98/NT/2000- OS/2- OS/2

Page 16: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

16GWU CS 259 Brad Taylor Spring 2004

Many-to-Many Model (N:M)

•Allows many user Allows many user level threads to be level threads to be mapped to many mapped to many kernel threads.kernel threads.•Allows the operating Allows the operating system to create a system to create a sufficient number of sufficient number of kernel threads.kernel threads.•Solaris 2 Solaris 2 •Windows NT/2000 Windows NT/2000 with the with the ThreadFiberThreadFiber packagepackage

Page 17: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

17GWU CS 259 Brad Taylor Spring 2004

P P P

Solaris Hybrid: Processes, Threads and LWPs

L LL

Process 1

user

kernel

hardware

L

Process 2

...

Int kthr

......

Page 18: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

18GWU CS 259 Brad Taylor Spring 2004

Solaris User Threads

• Implemented in user librariesImplemented in user libraries• Library provides synchronization Library provides synchronization

& scheduling facilities& scheduling facilities• Threads may be bound to LWPsThreads may be bound to LWPs• Unbound threads compete for Unbound threads compete for

available LWPsavailable LWPs• Manage thread specific infoManage thread specific info

– thread id, saved register state, user thread id, saved register state, user stack, signal mask, priority*, thread stack, signal mask, priority*, thread local storagelocal storage

• Try man pthreadsTry man pthreads

proc_t

kthread_t

klwp_t

p_tlist

t_procp

t_forwlwp_thread

t_lwp

lwp_procp

Page 19: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

19GWU CS 259 Brad Taylor Spring 2004

Solaris Threads Support

• User threads (uthreads) via User threads (uthreads) via libthreadlibthread and and libpthreadlibpthread• LWPs act as virtual CPU for user threadsLWPs act as virtual CPU for user threads• Kernel threads (kthreads): every LWP is associated with Kernel threads (kthreads): every LWP is associated with one kthread, however a kthread may not have an LWP & one kthread, however a kthread may not have an LWP & may be scheduled by OS on different processorsmay be scheduled by OS on different processors

Page 20: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

20GWU CS 259 Brad Taylor Spring 2004

Pthreads

• POSIX standard (IEEE 1003.1c) API POSIX standard (IEEE 1003.1c) API for thread creation and for thread creation and synchronizationsynchronization

• API specifies behavior of the API specifies behavior of the thread library; vendor-specific thread library; vendor-specific library implementationlibrary implementation

• Common in UNIX operating Common in UNIX operating systemssystems

Page 21: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

21GWU CS 259 Brad Taylor Spring 2004

Threading Issues

• Thread creationThread creation• Thread completion Thread completion

– joinjoin, , detachdetach, , exitexit, , cancelcancel

• Thread communicationThread communication• Signal handlingSignal handling• Thread specific dataThread specific data• Thread prioritizationThread prioritization

Page 22: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

22GWU CS 259 Brad Taylor Spring 2004

Thread Creation

• pthread_create()pthread_create() adds new thread to current process; adds new thread to current process; if successful, created thread ID is stored in the location if successful, created thread ID is stored in the location named tid (prototype below)named tid (prototype below)

• start_routinestart_routine begins new thread execution; when it begins new thread execution; when it returns, the thread exits with an exit status value set by returns, the thread exits with an exit status value set by start_routinestart_routine

int pthread_create(pthread_t *tid, const pthread_attr_t int pthread_create(pthread_t *tid, const pthread_attr_t *tattr, void*(*start_routine) (void *), void *arg);*tattr, void*(*start_routine) (void *), void *arg);

• When attribute object not specified (NULL) a When attribute object not specified (NULL) a defaultdefault thread created with following attributes: thread created with following attributes: – unbounded unbounded – nondetached nondetached – default stack and stack size default stack and stack size – inherits parent's priority inherits parent's priority

Page 23: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

23GWU CS 259 Brad Taylor Spring 2004

Thread Creation Example

#include <pthread.h> #include <pthread.h> pthread_attr_t tattr; pthread_attr_t tattr; pthread_t tid; pthread_t tid; extern void *start_routine(void *arg); extern void *start_routine(void *arg); void *arg; void *arg; int rt; int rt; /* default behavior*/ /* default behavior*/ rt = pthread_create(&tid, NULL, start_routine, arg); rt = pthread_create(&tid, NULL, start_routine, arg); /* initialized with default attributes */ /* initialized with default attributes */ rt = pthread_attr_init(&tattr); rt = pthread_attr_init(&tattr); /* default behavior specified*/ /* default behavior specified*/ rt = pthread_create(&tid, &tattr, start_routine, rt = pthread_create(&tid, &tattr, start_routine,

arg); arg);

Page 24: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

24GWU CS 259 Brad Taylor Spring 2004

Thread Completion: pthread_join

• pthread_join pthread_join waits for a thread to terminatewaits for a thread to terminate (blocks calling thread (blocks calling thread until then); thread must be in until then); thread must be in current processcurrent process and and not detachednot detached

#include <pthread.h> #include <pthread.h> pthread_t tid; pthread_t tid; int rt; int rt; int status; int status; /* waiting to join thread "tid" returns status to location*/ /* waiting to join thread "tid" returns status to location*/ rt = pthread_join(tid, &status); rt = pthread_join(tid, &status); /* waiting to join thread "tid" without status */ /* waiting to join thread "tid" without status */ rt = pthread_join(tid, NULL);rt = pthread_join(tid, NULL);

• Use Use pthread_join()pthread_join() only for nondetached target threads – reserve only for nondetached target threads – reserve for only those situations requiredfor only those situations required

• If thread termination synchronization not required, use If thread termination synchronization not required, use pthread_detach()pthread_detach() – most instances – most instances

Page 25: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

25GWU CS 259 Brad Taylor Spring 2004

Thread Completion: pthread_detach

• pthread_detach()pthread_detach() an alternative to an alternative to pthread_join()pthread_join(),, reclaims storage used by a thread reclaims storage used by a thread

#include <pthread.h> #include <pthread.h> pthread_t tid; pthread_t tid; int rt; int rt; /* detach thread tid */ /* detach thread tid */ rt = pthread_detach(tid);rt = pthread_detach(tid);

Page 26: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

26GWU CS 259 Brad Taylor Spring 2004

Thread Completion: pthread_exit

• pthread_exit(void *status)pthread_exit(void *status) self-terminates a thread self-terminates a thread similar fashion to similar fashion to exit()exit() for a process for a process

#include <pthread.h> #include <pthread.h> int status; int status; pthread_exit(&status); pthread_exit(&status); /* exit with status *//* exit with status */

• All thread-specific data bindings are releasedAll thread-specific data bindings are released• If this thread is not detached, then If this thread is not detached, then tidtid & exit status & exit status

retained until thread waited for (blocked)retained until thread waited for (blocked)• If detached, status ignored & If detached, status ignored & tidtid reclaimable reclaimable

immediately immediately

Page 27: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

27GWU CS 259 Brad Taylor Spring 2004

Thread Completion: pthread_cancel

• pthread_cancel(pthread_t thread)pthread_cancel(pthread_t thread) sends a cancellation request to the sends a cancellation request to the target thread target thread threadthread

#include <pthread.h> #include <pthread.h> pthread_t thread; pthread_t thread; int rt; int rt; rt = pthread_cancel(thread); rt = pthread_cancel(thread);

• Target thread handles request based on its Target thread handles request based on its Cancel StateCancel State & & Cancel TypeCancel Type; ; improper cleanup may fail to release resources (lock, open fd)improper cleanup may fail to release resources (lock, open fd)

• pthread_setcancelstatepthread_setcancelstate: Cancel State value may be : Cancel State value may be EnabledEnabled or or DisabledDisabled (disabled: thread will not follow request until enabled; default: enabled)(disabled: thread will not follow request until enabled; default: enabled)

• pthread_setcanceltypepthread_setcanceltype: Cancel Type value may be : Cancel Type value may be AsynchronousAsynchronous or or DeferredDeferred; (if state == enabled):; (if state == enabled):– Choice basis: Will thread exit leave process in unacceptable condition?Choice basis: Will thread exit leave process in unacceptable condition?– Asynchronous: self-terminate upon receiptAsynchronous: self-terminate upon receipt– Deferred: self-terminate upon reaching cancellation pointDeferred: self-terminate upon reaching cancellation point– Default: deferred; should only be set otherwise when code & library calls async-Default: deferred; should only be set otherwise when code & library calls async-

cancel safecancel safe• A function setting cancel state or type should reset it b4 returningA function setting cancel state or type should reset it b4 returning

Page 28: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

28GWU CS 259 Brad Taylor Spring 2004

Thread Communication

• pthread_self()pthread_self() returns the calling thread ID returns the calling thread ID

#include <pthread.h> #include <pthread.h> pthread_t tid; pthread_t tid; tid = pthread_self();tid = pthread_self();

• pthread_equal()pthread_equal() compares the thread IDs of two threads compares the thread IDs of two threads

#include <pthread.h> #include <pthread.h> pthread_t tid1, tid2; pthread_t tid1, tid2; int rt; int rt; rt = pthread_equal(tid1, tid2);rt = pthread_equal(tid1, tid2); • sched_yield()sched_yield() causes current thread to yield execution in favor of causes current thread to yield execution in favor of

another thread with same or greater priority another thread with same or greater priority

#include <sched.h> #include <sched.h> int rt; int rt; rt = sched_yield(); rt = sched_yield();

Page 29: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

29GWU CS 259 Brad Taylor Spring 2004

Thread Signal Handling

• pthread_kill (tid, sig)pthread_kill (tid, sig) sends a signal to a thread sends a signal to a thread tidtid in a in a similar fashion to similar fashion to kill(sig)kill(sig) for a process for a process

#include <pthread.h> #include <pthread.h> #include <signal.h> #include <signal.h> int sig; int sig; pthread_t tid; pthread_t tid; int rt; int rt; rt = pthread_kill(tid, sig);rt = pthread_kill(tid, sig);

• pthread_sigmask()pthread_sigmask() changes or examines the calling thread changes or examines the calling thread signal masksignal mask

#include <pthread.h> #include <pthread.h> #include <signal.h> #include <signal.h> int rt; int rt; sigset_t old, new; sigset_t old, new; rt = pthread_sigmask(SIG_SETMASK, &new, &old); /* set new mask */ rt = pthread_sigmask(SIG_SETMASK, &new, &old); /* set new mask */ rt = pthread_sigmask(SIG_BLOCK, &new, &old); /* blocking mask */ rt = pthread_sigmask(SIG_BLOCK, &new, &old); /* blocking mask */ rt = pthread_sigmask(SIG_UNBLOCK, &new, &old); /* unblocking */rt = pthread_sigmask(SIG_UNBLOCK, &new, &old); /* unblocking */

Page 30: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

30GWU CS 259 Brad Taylor Spring 2004

Thread Specific Data

Thread-Specific Data Global but Thread-Specific Data Global but PrivatePrivate

• pthread_keycreate()pthread_keycreate() used to used to allocate a key to identify thread-allocate a key to identify thread-specific data in a process specific data in a process

#include <pthread.h> #include <pthread.h> pthread_key_t key; pthread_key_t key; int rt; int rt; /* key create without destructor*/ /* key create without destructor*/ rt = pthread_key_create(&key, rt = pthread_key_create(&key,

NULL); NULL); /* key create with destructor */ /* key create with destructor */ rt = pthread_key_create(&key, rt = pthread_key_create(&key,

destructor);destructor);

• pthread_keydelete()pthread_keydelete() destroys destroys existing TSD keyexisting TSD key

… … Powerful tools!Powerful tools!• pthread_setspecific() pthread_setspecific() &&

pthread_getspecific()pthread_getspecific() sets (gets) sets (gets) the thread-specific binding for a key the thread-specific binding for a key (the latter, storing results)(the latter, storing results)

#include <pthread.h> #include <pthread.h> pthread_key_t key; pthread_key_t key; void *value; void *value; int rt; int rt; /* key previously created */ /* key previously created */ rt = pthread_setspecific(key, rt = pthread_setspecific(key,

value); value);

#include <pthread.h> #include <pthread.h> pthread_key_t key; pthread_key_t key; void *value; void *value; /* key previously created */ /* key previously created */ value = pthread_getspecific(key); value = pthread_getspecific(key);

Page 31: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

31GWU CS 259 Brad Taylor Spring 2004

Thread Prioritization

• pthread_setschedparam(pthread_t tid, int policy, const pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param)struct sched_param *param) modifies priority of an existing modifies priority of an existing thread, but has no effect on scheduling policy thread, but has no effect on scheduling policy

#include <pthread.h> #include <pthread.h> pthread_t tid; pthread_t tid; int rt; int rt; struct sched_param param; struct sched_param param; int priority; int priority; /* sched_priority will be the priority of the thread */ /* sched_priority will be the priority of the thread */ sched_param.sched_priority = priority; sched_param.sched_priority = priority; /* only supported policy, others will result in ENOTSUP */ /* only supported policy, others will result in ENOTSUP */ policy = SCHED_OTHER; policy = SCHED_OTHER; /* scheduling parameters of target thread */ /* scheduling parameters of target thread */ rt = pthread_setschedparam(tid, policy, &param);rt = pthread_setschedparam(tid, policy, &param);

Page 32: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

32GWU CS 259 Brad Taylor Spring 2004

Thread Prioritization (con’t)

• pthread_getschedparam(pthread_t tid, int policy, struct pthread_getschedparam(pthread_t tid, int policy, struct schedparam *param)schedparam *param) gets existing thread priority gets existing thread priority

#include <pthread.h> #include <pthread.h> pthread_t tid; pthread_t tid; sched_param param; sched_param param; int priority; int priority; int policy; int policy; int rt; int rt; /* scheduling parameters of target thread */ /* scheduling parameters of target thread */ rt = pthread_getschedparam (tid, &policy, &param); rt = pthread_getschedparam (tid, &policy, &param); /* sched_priority contains the priority of the thread */ /* sched_priority contains the priority of the thread */ priority = param.sched_priority;priority = param.sched_priority;

Page 33: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

33GWU CS 259 Brad Taylor Spring 2004

Debugging Multithreaded Programs

• Passing a caller's stack pointer as argument to new threadPassing a caller's stack pointer as argument to new thread• Accessing global memory without synchronization mechanism protection Accessing global memory without synchronization mechanism protection • Creating deadlocks by two threads acquiring rights to same pair of global Creating deadlocks by two threads acquiring rights to same pair of global

resources in alternate order (one thread controls the first resource and other resources in alternate order (one thread controls the first resource and other controls the second resource; neither can proceed until the other gives up)controls the second resource; neither can proceed until the other gives up)

• Trying to reacquire a lock already held (recursive deadlock)Trying to reacquire a lock already held (recursive deadlock)• Creating a hidden gap in synchronization protection: a code segment protected Creating a hidden gap in synchronization protection: a code segment protected

by a synchronization mechanism contains a function call that frees and by a synchronization mechanism contains a function call that frees and reacquires the mechanism before it returning to caller, resulting in appearing reacquires the mechanism before it returning to caller, resulting in appearing that global data protected when actually notthat global data protected when actually not

• Mixing UNIX signals & threads: sigwait() handles asynchronous signals better Mixing UNIX signals & threads: sigwait() handles asynchronous signals better • Forgetting default threads created with PTHREAD_CREATE_JOINABLE must be Forgetting default threads created with PTHREAD_CREATE_JOINABLE must be

reclaimed with pthread_join() as pthread_exit() does not free up its storage reclaimed with pthread_join() as pthread_exit() does not free up its storage spacespace

• Deeply nested, recursive calls and large automatic arrays cause problems as Deeply nested, recursive calls and large automatic arrays cause problems as multithreaded programs have more limited stack size than single-threaded multithreaded programs have more limited stack size than single-threaded onesones

• Specifying inadequate or non-default stack sizeSpecifying inadequate or non-default stack size• Multithreaded programs (especially those with bugs) often behave differently in Multithreaded programs (especially those with bugs) often behave differently in

two successive runs, given identical inputs, because of thread scheduling order two successive runs, given identical inputs, because of thread scheduling order differencesdifferences

Typical oversights and errors causing bugs:

Page 34: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

34GWU CS 259 Brad Taylor Spring 2004

Java Threads

• Java threads may be created by:Java threads may be created by:– Extending Thread classExtending Thread class– Implementing the Runnable interfaceImplementing the Runnable interface

• Java threads are managed by the JVMJava threads are managed by the JVM• One modern language that uses monitors is JavaOne modern language that uses monitors is Java

– Each object has its own monitorEach object has its own monitor– Methods are put in the monitor using the Methods are put in the monitor using the synchronizedsynchronized

keyword keyword – Each monitor has one condition variable (CV)Each monitor has one condition variable (CV)– The methods on the CVs are: The methods on the CVs are: wait()wait(),, notify() notify(),, andand

notifyAll()notifyAll() – As monitor each has only one CV, it is not explicitly As monitor each has only one CV, it is not explicitly

specified specified

Page 35: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

35GWU CS 259 Brad Taylor Spring 2004

Java Thread States

Page 36: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

36GWU CS 259 Brad Taylor Spring 2004

Basic Threads Summary

Thread = pointer to instruction + stateThread = pointer to instruction + stateProcess = thread + address spaceProcess = thread + address spaceKey aspects:Key aspects:

– per-thread stateper-thread state– picking a thread to runpicking a thread to run– switching between threadsswitching between threads

Scheduling (after midterm):Scheduling (after midterm):– How to pick the right thread to run?How to pick the right thread to run?– How to share state among threads?How to share state among threads?– Processes, tooProcesses, too

Page 37: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

37GWU CS 259 Brad Taylor Spring 2004

Typical Real-Time System

Controlling System

Environment

sensorsensorsensorsensor

actuatoractuatoractuatoractuator

Controlled System

ACTUATORDESIRED

RESPONSE

FEEDBACK ELEMENTS

{SENSOR(s)}

+

_Error

SOFTWARECONTROL {PID, MBC,

etc.} ENVIRONMENT{Temp, Pres,

D/P, Flow, etc.}

PLANT

CO

ND

I-T

ION

ING

Page 38: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

38GWU CS 259 Brad Taylor Spring 2004

Project Status Report

Group I: Dan, Ricardo & Kamal / Wire SocketGroup I: Dan, Ricardo & Kamal / Wire Socket

Group II: Clayton, Jason / Named PipesGroup II: Clayton, Jason / Named Pipes

Group III: Brooke, Nush, Ram / Shared Memory, Group III: Brooke, Nush, Ram / Shared Memory, Semaphores & FutexsSemaphores & Futexs

Integration: Nate & BradIntegration: Nate & BradIssues to discuss:Issues to discuss:Error checkingError checkingRough draft code submission for integration reviewRough draft code submission for integration reviewFurther Guidance, QuestionsFurther Guidance, Questions

Page 39: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

39GWU CS 259 Brad Taylor Spring 2004

Synchronization: Semaphores & more

• BackgroundBackground• Critical-Section ProblemCritical-Section Problem• Synchronization HardwareSynchronization Hardware• SemaphoresSemaphores• Classical Synchronization ProblemsClassical Synchronization Problems• Critical RegionsCritical Regions• MonitorsMonitors• POSIX SynchronizationPOSIX Synchronization

Page 40: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

40GWU CS 259 Brad Taylor Spring 2004

Background

Data inconsistency may result from concurrent Data inconsistency may result from concurrent access to shared dataaccess to shared data

Maintaining consistency requires mechanisms Maintaining consistency requires mechanisms ensuring cooperating processes orderly ensuring cooperating processes orderly executionexecution

Shared-memory solution to bounded-buffer Shared-memory solution to bounded-buffer problem allows at most problem allows at most n n – 1 items in buffer at – 1 items in buffer at the same time: solution, where all the same time: solution, where all n n buffers buffers used not simpleused not simple– Suppose producer-consumer code modified by adding Suppose producer-consumer code modified by adding

a variable a variable countercounter, initialized to 0, incremented each , initialized to 0, incremented each time a new item is added to the buffertime a new item is added to the buffer

Page 41: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

41GWU CS 259 Brad Taylor Spring 2004

Remember …Bounded-Buffer

Shared dataShared data

#define BUFFER_SIZE 10#define BUFFER_SIZE 10

typedef struct {typedef struct {

. . .. . .

} item} item;;

item buffer[BUFFER_SIZE];item buffer[BUFFER_SIZE];

int in = 0;int in = 0;

int out = 0;int out = 0;

int counter = 0;int counter = 0;

Page 42: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

42GWU CS 259 Brad Taylor Spring 2004

Bounded-Buffer (con’t)

Producer thread Producer thread

item nextProduced;item nextProduced;

while (1) {while (1) {while (counter == BUFFER_SIZE)while (counter == BUFFER_SIZE)

; /* do nothing */; /* do nothing */buffer[in] = nextProduced;buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;in = (in + 1) % BUFFER_SIZE;counter++;counter++;

}}

Page 43: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

43GWU CS 259 Brad Taylor Spring 2004

Bounded-Buffer (con’t)

Consumer thread Consumer thread

item nextConsumed;item nextConsumed;

while (1) {while (1) {while (counter == 0)while (counter == 0)

; /* do nothing */; /* do nothing */nextConsumed = buffer[out];nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;out = (out + 1) % BUFFER_SIZE;counter--;counter--;

}}

Page 44: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

44GWU CS 259 Brad Taylor Spring 2004

Bounded-Buffer (con’t)

• The statementsThe statements

counter++;counter++;counter--;counter--;

must be performed must be performed atomicallyatomically..

• Atomic operation means an operation that completes in its entirety without Atomic operation means an operation that completes in its entirety without interruptioninterruption

• If both the producer and consumer attempt to update the buffer concurrently, If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleavedthe assembly language statements may get interleaved

• Interleaving depends upon how producer and consumer threads scheduledInterleaving depends upon how producer and consumer threads scheduled

• Race conditionRace condition: The situation where several threads access – and manipulate : The situation where several threads access – and manipulate shared data concurrently: the final value of the shared data depends upon shared data concurrently: the final value of the shared data depends upon which process finishes lastwhich process finishes last

• To prevent race conditions, concurrent threads must be To prevent race conditions, concurrent threads must be synchronizedsynchronized

Page 45: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

45GWU CS 259 Brad Taylor Spring 2004

The Critical-Section Problem

• nn threads all competing to use threads all competing to use some shared datasome shared data

• Each thread has a code segment, Each thread has a code segment, called called critical sectioncritical section, in which , in which shared data accessedshared data accessed

• Problem – ensure that when one Problem – ensure that when one thread is executing in its critical thread is executing in its critical section, no other thread allowed to section, no other thread allowed to execute in its critical sectionexecute in its critical section

Page 46: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

46GWU CS 259 Brad Taylor Spring 2004

Solution to Critical-Section Problem

1.1. Mutual ExclusionMutual Exclusion. If thread . If thread TTii is executing in its is executing in its critical section, then no other threads can be executing critical section, then no other threads can be executing in their critical sectionsin their critical sections {yellow submarine {yellow submarine solution}solution}

2.2. ProgressProgress. If no thread is executing in its critical . If no thread is executing in its critical section and there exist some threads that wish to enter section and there exist some threads that wish to enter their critical section, then selection of threads that will their critical section, then selection of threads that will enter the critical section next cannot be postponed enter the critical section next cannot be postponed indefinitelyindefinitely

3.3. Bounded WaitingBounded Waiting. A bound must exist on number of . A bound must exist on number of times that other threads are allowed to enter their times that other threads are allowed to enter their critical sections after a thread has made a request to critical sections after a thread has made a request to enter its critical section and before that request is enter its critical section and before that request is grantedgranted Assume that each thread executes at a nonzero speed Assume that each thread executes at a nonzero speed No assumption concerning relative speed of the No assumption concerning relative speed of the nn threads threads

Page 47: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

47GWU CS 259 Brad Taylor Spring 2004

Initial Attempts to Solve Problem

Only 2 threads , Only 2 threads , TT00 and and TT11

General structure of thread General structure of thread TTii (other thread (other thread TTjj))dodo { {

entry sectionentry sectioncritical sectioncritical section

exit sectionexit sectionreminder sectionreminder section

} } while (1)while (1);;Threads may share some common variables to Threads may share some common variables to

synchronize their actionssynchronize their actions

Page 48: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

48GWU CS 259 Brad Taylor Spring 2004

Algorithm 1

Shared variables: Shared variables: – int turnint turn;;

initially initially turn = 0turn = 0– turn - iturn - i TTii can enter its critical section can enter its critical section

Thread Thread TTii

dodo { {while (turn != i) while (turn != i) ;;

critical sectioncritical sectionturn = jturn = j;;

reminder sectionreminder section} } while (1)while (1);;

Satisfies mutual exclusion, but not progressSatisfies mutual exclusion, but not progress

Page 49: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

49GWU CS 259 Brad Taylor Spring 2004

Algorithm 2

Shared variablesShared variables– boolean flag[2]boolean flag[2];;

initially initially flag [0] = flag [1] = false.flag [0] = flag [1] = false.– flag [i] = trueflag [i] = true TTii ready to enter its critical section ready to enter its critical section

Thread Thread TTii

do {do {flag[i] := true;flag[i] := true;while (flag[j]) ;while (flag[j]) ;

critical sectioncritical sectionflag [i] = false;flag [i] = false;

remainder sectionremainder section} while (1);} while (1);

Satisfies mutual exclusion, but not progress Satisfies mutual exclusion, but not progress requirementrequirement

Page 50: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

50GWU CS 259 Brad Taylor Spring 2004

Algorithm 3

Combined shared variables of algorithms 1 and 2.Combined shared variables of algorithms 1 and 2.Thread Thread TTii

dodo { {flag [i]:= true;flag [i]:= true;turn = j;turn = j;while (flag [j] and turn = j) ;while (flag [j] and turn = j) ;

critical sectioncritical sectionflag [i] = false;flag [i] = false;

remainder sectionremainder section} } while (1);while (1);

Meets all three requirements; solves the critical-Meets all three requirements; solves the critical-section problem for two processes.section problem for two processes.

Page 51: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

51GWU CS 259 Brad Taylor Spring 2004

Bakery Algorithm

• Before entering its critical section, thread receives a Before entering its critical section, thread receives a number: holder of smallest number enters critical sectionnumber: holder of smallest number enters critical section

• If threads If threads TTii and and TTjj receive the same number, if receive the same number, if ii < < jj, then , then TTii is served first; else is served first; else TTjj is served first is served first

• The numbering scheme always generates numbers in The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...

• Notation <Notation < lexicographical order (ticket #, process id #) lexicographical order (ticket #, process id #)– ((a,ba,b) < ) < c,dc,d) if ) if aa < < cc or if or if aa = = cc and and b b < < dd– max (max (aa00,…, ,…, aann-1-1) is a number, ) is a number, kk, such that , such that kk a aii for for ii - 0, - 0,

…, …, nn – 1 – 1• Shared dataShared data• boolean choosing[n];boolean choosing[n];• int number[n];int number[n];• Data structures are initialized to Data structures are initialized to falsefalse and and 00 respectively respectively

Critical section for n processes

Page 52: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

52GWU CS 259 Brad Taylor Spring 2004

Bakery Algorithm

do { do { choosing[i] = true;choosing[i] = true;number[i] = max(number[0], number[1], …, number number[i] = max(number[0], number[1], …, number [n – 1])+1;[n – 1])+1;choosing[i] = false;choosing[i] = false;for (j = 0; j < n; j++) {for (j = 0; j < n; j++) {

while (choosing[j]) ; while (choosing[j]) ; while ((number[j] != 0) && (number[j,j] < while ((number[j] != 0) && (number[j,j] <

number[i,i])) ;number[i,i])) ;}}

critical sectioncritical sectionnumber[i] = 0;number[i] = 0;

remainder sectionremainder section} while (1);} while (1);

Page 53: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

53GWU CS 259 Brad Taylor Spring 2004

Mutual Exclusion (mutex)

• Mutex shortened form of words “mutual exclusion”Mutex shortened form of words “mutual exclusion”• Basic pthread mutex concept: only one thread can lock Basic pthread mutex concept: only one thread can lock

(or own) a mutex at any given time(or own) a mutex at any given time• Even if several threads try to lock a mutex, only one Even if several threads try to lock a mutex, only one

thread will be successfulthread will be successful• No other thread can own the mutex until owning thread No other thread can own the mutex until owning thread

unlocks that mutex unlocks that mutex • Typical mutex sequence: Typical mutex sequence:

– Create mutex Create mutex – Several threads attempt to lock mutex Several threads attempt to lock mutex – Only one succeeds; that thread owns mutex Only one succeeds; that thread owns mutex – Owner thread performs some set of actions Owner thread performs some set of actions – Owner unlocks mutex Owner unlocks mutex – Another thread acquires mutex and repeats process Another thread acquires mutex and repeats process – Finally mutex destroyed Finally mutex destroyed

Page 54: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

54GWU CS 259 Brad Taylor Spring 2004

Mutual Exclusion with Test-and-Set

Shared data: Shared data: boolean lock = false;boolean lock = false;

Thread Thread TTii

do {do {while (TestAndSet(lock)) ;while (TestAndSet(lock)) ;

critical sectioncritical sectionlock = false;lock = false;

remainder sectionremainder section}}

Page 55: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

55GWU CS 259 Brad Taylor Spring 2004

Semaphores

• Synchronization tool not requiring busy waitingSynchronization tool not requiring busy waiting• Semaphore Semaphore SS an integer variable, accessed an integer variable, accessed

only by two atomic operationsonly by two atomic operationswaitwait ( (SS): ):

while while SS 0 do 0 do no-opno-op;;SS--;--;

signalsignal ( (SS): ): S++;S++;

• Each Semaphore has an associated queueEach Semaphore has an associated queue• Can define a non-blocking version of wait(Can define a non-blocking version of wait(SS))

Page 56: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

56GWU CS 259 Brad Taylor Spring 2004

Semaphores

• Integer variable supporting two operations :Integer variable supporting two operations :– waitwait(S): (S):

» if S if S 1 then S := S - 1 and return, 1 then S := S - 1 and return,» else block thread on the semaphore queueelse block thread on the semaphore queue

– signalsignal(S): (S): » if blocked threads, wake them upif blocked threads, wake them up» else S := S + 1else S := S + 1

• Kernel guarantees operations are atomicKernel guarantees operations are atomic• Can define a non-blocking version of Can define a non-blocking version of trytry(CP)(CP)• Threads are woken up in FIFO order - Threads are woken up in FIFO order -

– Semaphore convoysSemaphore convoys - results in unnecessary context switches - results in unnecessary context switches• How would you use semaphores to implement mutual exclusion How would you use semaphores to implement mutual exclusion

or event waiting?or event waiting?– Mutual Exclusion: binary semaphore initialized to 1Mutual Exclusion: binary semaphore initialized to 1– Event Waiting: binary semaphore initialized to 0Event Waiting: binary semaphore initialized to 0

• How would you implement a resource counting semaphore?How would you implement a resource counting semaphore?– Initialized to number of available resourcesInitialized to number of available resources

Page 57: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

57GWU CS 259 Brad Taylor Spring 2004

Critical Section of n Processes

Shared data:Shared data: semaphore mutex; //semaphore mutex; //initially initially mutexmutex = 1 = 1

Thread Thread TTii::

do {do { wait(mutex); wait(mutex); critical sectioncritical section

signal(mutex);signal(mutex); remainder section remainder section} while (1);} while (1);

Page 58: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

58GWU CS 259 Brad Taylor Spring 2004

Semaphore Implementation

Define a semaphore as a recordDefine a semaphore as a recordtypedef struct {typedef struct { int value;int value; struct thread *L; struct thread *L;} semaphore;} semaphore;

Assume two simple operations:Assume two simple operations:– blockblock suspends the process that invokes it. suspends the process that invokes it.– wakeup(wakeup(TT)) resumes the execution of a resumes the execution of a

blocked thread blocked thread TT

Page 59: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

59GWU CS 259 Brad Taylor Spring 2004

Implementation

Semaphore operations now defined as Semaphore operations now defined as waitwait((SS):):

S.value--;S.value--;if (S.value < 0) { if (S.value < 0) {

add this thread toadd this thread to S.L; S.L;block;block;

}}

signalsignal((SS): ): S.value++;S.value++;if (S.value <= 0) {if (S.value <= 0) {

remove a thread remove a thread T T fromfrom S.L;S.L;

wakeup(T);wakeup(T);}}

Page 60: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

60GWU CS 259 Brad Taylor Spring 2004

Implementation - Threads

void waitvoid wait(sem_t *(sem_t *SS))

{{

S->value--S->value--;;

while (while (S->valueS->value < 0) { < 0) {

addthreadaddthread((S->sqS->sq););

sleepsleep();();

}}

return;return;

}}

void signalvoid signal(sem_t *(sem_t *SS))

{{

thread_t t;thread_t t;

S->value++S->value++;;

if (if (S->valueS->value 0) { 0) {

t = t = getthreadgetthread((S->sqS->sq););wakeupwakeup(t);(t);

}}

return;return;

}}

Page 61: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

61GWU CS 259 Brad Taylor Spring 2004

Deadlock and Starvation

DeadlockDeadlock – two or more threads waiting indefinitely for an – two or more threads waiting indefinitely for an event that can only be caused by a waiting threadevent that can only be caused by a waiting thread

Let Let SS and and QQ be two semaphores initialized to 1 be two semaphores initialized to 1PP00 PP11

waitwait((SS);); waitwait((QQ););waitwait((QQ);); waitwait((SS););

signalsignal((SS);); signalsignal((QQ););signalsignal((QQ)) signalsignal((SS););

StarvationStarvation – indefinite blocking: a thread may never be – indefinite blocking: a thread may never be removed from the semaphore queue in which it is removed from the semaphore queue in which it is suspendedsuspended

Page 62: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

62GWU CS 259 Brad Taylor Spring 2004

Two Types of Semaphores

• CountingCounting semaphore – integer value semaphore – integer value can range over an unrestricted can range over an unrestricted domaindomain

• BinaryBinary semaphore – integer value can semaphore – integer value can range only between 0 and 1; can be range only between 0 and 1; can be simpler to implementsimpler to implement

• Can implement a counting Can implement a counting semaphore semaphore SS as a binary semaphore as a binary semaphore

Page 63: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

63GWU CS 259 Brad Taylor Spring 2004

Implementing S as a Binary Semaphore

Data structures:Data structures:binary-semaphore S1, S2;binary-semaphore S1, S2;

int C:int C: Initialization:Initialization:

S1 = 1S1 = 1S2 = 0S2 = 0C = C = initial value of semaphoreinitial value of semaphore S S

Page 64: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

64GWU CS 259 Brad Taylor Spring 2004

Implementing S

waitwait operation operationwait(S1);wait(S1);C--;C--;if (C < 0) {if (C < 0) {

signal(S1);signal(S1);wait(S2);wait(S2);

}}signal(S1);signal(S1);

signalsignal operation operationwait(S1);wait(S1);C ++;C ++;if (C <= 0)if (C <= 0)

signal(S2);signal(S2);elseelse

signal(S1);signal(S1);

Page 65: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

65GWU CS 259 Brad Taylor Spring 2004

Classical Problems of Synchronization

• Bounded-Buffer ProblemBounded-Buffer Problem

• Readers and Writers ProblemReaders and Writers Problem

• Dining-Philosophers ProblemDining-Philosophers Problem

Page 66: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

66GWU CS 259 Brad Taylor Spring 2004

Readers-Writers Problem

Shared dataShared data

semaphore mutex, wrt;semaphore mutex, wrt;

InitiallyInitially

mutex = 1, wrt = 1, readcount = 0mutex = 1, wrt = 1, readcount = 0

Page 67: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

67GWU CS 259 Brad Taylor Spring 2004

Readers-Writers Problem Writer Process

wait(wrt);wait(wrt); … …

writing is performedwriting is performed … …

signal(wrt);signal(wrt);

Page 68: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

68GWU CS 259 Brad Taylor Spring 2004

Readers-Writers Problem Reader Process

wait(mutex);wait(mutex);readcount++;readcount++;if (readcount == 1)if (readcount == 1)

wait(rt);wait(rt);signal(mutex);signal(mutex);

… …reading is performedreading is performed

… …wait(mutex);wait(mutex);readcount--;readcount--;if (readcount == 0)if (readcount == 0)signal(wrt);signal(wrt);

signal(mutex):signal(mutex):

Page 69: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

69GWU CS 259 Brad Taylor Spring 2004

Critical Regions

• High-level synchronization constructHigh-level synchronization construct• A shared variable A shared variable vv of type of type TT, is declared as:, is declared as:

v:v: sharedshared TT• Variable Variable vv accessed only inside statement accessed only inside statement

regionregion vv whenwhen BB dodo SS

where where BB is a boolean expression is a boolean expression• While statement While statement SS is being executed, no other process can access is being executed, no other process can access

variable variable vv• Regions referring to same shared variable exclude each other in Regions referring to same shared variable exclude each other in

timetime• When a thread tries to execute the region statement, Boolean When a thread tries to execute the region statement, Boolean

expression expression BB evaluated; if evaluated; if BB true, statement true, statement SS executed; else if executed; else if false, the threadfalse, the thread is delayed until is delayed until BB becomes true and no other becomes true and no other threadthread is in the region associated with is in the region associated with vv

Page 70: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

70GWU CS 259 Brad Taylor Spring 2004

Implementation region x when B do S

Associate with the shared variable Associate with the shared variable xx, the following , the following variables:variables:semaphore mutex, first-delay, second-semaphore mutex, first-delay, second-delay;delay; int first-count, second-count; int first-count, second-count;

Mutually exclusive access to the critical section is Mutually exclusive access to the critical section is provided by provided by mutexmutex

If a process cannot enter the critical section If a process cannot enter the critical section because the Boolean expression because the Boolean expression BB is false, it is false, it initially waits on the initially waits on the first-delay first-delay semaphore; semaphore; moved to the moved to the second-delay second-delay semaphore before semaphore before it is allowed to reevaluate it is allowed to reevaluate BB

Page 71: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

71GWU CS 259 Brad Taylor Spring 2004

Implementation

Keep track of the number of threads Keep track of the number of threads waiting on waiting on first-delayfirst-delay and and second-second-delaydelay, with , with first-countfirst-count and and second-second-countcount respectively respectively

The algorithm assumes a FIFO ordering in The algorithm assumes a FIFO ordering in the queuing of threads for a semaphorethe queuing of threads for a semaphore

For an arbitrary queuing discipline, a For an arbitrary queuing discipline, a more complicated implementation is more complicated implementation is requiredrequired

Page 72: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

72GWU CS 259 Brad Taylor Spring 2004

Spin Locks (MP Support)

The idea is to provide a basic, HW supported The idea is to provide a basic, HW supported primitive with primitive with lowlow overhead.overhead.

Lock held for Lock held for shortshort periods of time periods of timeIf locked, then busy-wait on the resourceIf locked, then busy-wait on the resourceMust not give up processor! In other Must not give up processor! In other

words, can not block.words, can not block.void spin_lock (spinlock_t *s){ while (test_and_set (s) != 0) while (*s != 0) ;}

void spin_unlock (spinlock_t *s){ s = 0;}

Page 73: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

73GWU CS 259 Brad Taylor Spring 2004

Monitors

High-level synchronization construct that allows the safe sharing of High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent threadsan abstract data type among concurrent threads

monitor monitor monitor-namemonitor-name{{

shared variable declarationsshared variable declarationsprocedure bodyprocedure body P1P1 (…) {(…) {

. . .. . .}}procedureprocedure bodybody P2 P2 (…) {(…) {

. . .. . .} } procedure bodyprocedure body PnPn (…) {(…) {

. . .. . .} } {{

initialization codeinitialization code}}

}}

Page 74: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

74GWU CS 259 Brad Taylor Spring 2004

Monitors

• To allow a process to wait within the monitor, a To allow a process to wait within the monitor, a conditioncondition variablevariable (***) must be declared, as (***) must be declared, as

condition x, y;condition x, y;• Condition variable can only be used with the Condition variable can only be used with the

operations operations waitwait and and signalsignal– The operation The operation x.wait();x.wait();

means that the thread invoking this operation is means that the thread invoking this operation is suspended until another thread invokessuspended until another thread invokes

– The operation The operation x.signal();x.signal();resumes exactly one suspended thread if no thread is resumes exactly one suspended thread if no thread is suspended, then the suspended, then the signalsignal operation has no effect operation has no effect

(***) Discussed in a few slides ;)(***) Discussed in a few slides ;)

Page 75: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

75GWU CS 259 Brad Taylor Spring 2004

Schematic View of a Monitor

Page 76: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

76GWU CS 259 Brad Taylor Spring 2004

Monitor With Condition Variables

Page 77: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

77GWU CS 259 Brad Taylor Spring 2004

Monitor Implementation Using Semaphores

Variables Variables semaphore mutex; // (initially = 1)semaphore mutex; // (initially = 1)semaphore next; // (initially = 0)semaphore next; // (initially = 0)int next-count = 0;int next-count = 0;

Each external procedure Each external procedure FF will be replaced by will be replaced bywait(mutex);wait(mutex);

… …body of body of FF;;

… …if (next-count > 0)if (next-count > 0) signal(next)signal(next)else else signal(mutex);signal(mutex);

Mutual exclusion within a monitor is ensuredMutual exclusion within a monitor is ensured

Page 78: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

78GWU CS 259 Brad Taylor Spring 2004

Monitor Implementation

For each condition variable For each condition variable xx, we have:, we have:semaphore x-sem; // (initially = 0)semaphore x-sem; // (initially = 0)int x-count = 0;int x-count = 0;

The operation The operation x.waitx.wait can be implemented as:can be implemented as:

x-count++;x-count++;if (next-count > 0)if (next-count > 0)

signal(next);signal(next);elseelse

signal(mutex);signal(mutex);wait(x-sem);wait(x-sem);x-count--;x-count--;

Page 79: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

79GWU CS 259 Brad Taylor Spring 2004

Monitor Implementation

The operation The operation x.signalx.signal can be can be implemented as:implemented as:

if (x-count > 0) {if (x-count > 0) {next-count++;next-count++;signal(x-sem);signal(x-sem);wait(next);wait(next);next-count--;next-count--;

}}

Page 80: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

80GWU CS 259 Brad Taylor Spring 2004

Monitor Implementation

• Conditional-waitConditional-wait construct: construct: x.wait(cx.wait(c):):– c:c: integer expression evaluated when integer expression evaluated when waitwait operation executed operation executed– value of value of cc (a (a priority numberpriority number) stored with name of thread ) stored with name of thread

suspendedsuspended– when when x.signalx.signal executed, thread with smallest associated executed, thread with smallest associated

priority number resumed nextpriority number resumed next

• Check two conditions to establish system correctness: Check two conditions to establish system correctness: – User threads must always make calls on the monitor in correct User threads must always make calls on the monitor in correct

sequencesequence– Must ensure that uncooperative thread does not ignore mutual-Must ensure that uncooperative thread does not ignore mutual-

exclusion gateway provided by monitor: accessing shared exclusion gateway provided by monitor: accessing shared resource directly, without using access protocolsresource directly, without using access protocols

Page 81: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

81GWU CS 259 Brad Taylor Spring 2004

Condition Variables: Simple Explanation

• Often encounter situations where a thread needs to wait on Often encounter situations where a thread needs to wait on meeting a condition before proceeding with its task meeting a condition before proceeding with its task – Ex: one thread requires data from another before it can Ex: one thread requires data from another before it can

proceedingproceeding• An approach: thread constantly polls (possibly in critical An approach: thread constantly polls (possibly in critical

section), to check if the condition is met; section), to check if the condition is met; very resource very resource intensiveintensive as thread continuously busy in this activity as thread continuously busy in this activity

• Condition variable achieves same goalCondition variable achieves same goal• A condition variable used in conjunction with a mutex lock A condition variable used in conjunction with a mutex lock

and a predicate (typically a Boolean variable) to allow one and a predicate (typically a Boolean variable) to allow one thread to signal (or wake up) other threads which are thread to signal (or wake up) other threads which are waiting on that condition variablewaiting on that condition variable

Page 82: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

82GWU CS 259 Brad Taylor Spring 2004

Condition Variables: Typical Sequence

• Create a condition variable Create a condition variable • Create an associated mutex and define a predicate Create an associated mutex and define a predicate

variable variable • Waiting on a condition variableWaiting on a condition variable

– Lock the mutex Lock the mutex – While predicate is unchanged wait on condition variable While predicate is unchanged wait on condition variable – Do work Do work – Unlock the mutex Unlock the mutex

• Signaling on a condition variableSignaling on a condition variable – Lock the mutex Lock the mutex – Do work Do work – Change predicate Change predicate – Signal on the condition variable Signal on the condition variable – Unlock mutex Unlock mutex

Page 83: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

83GWU CS 259 Brad Taylor Spring 2004

Condition Variables

mutex

predicate Condition variable

List of blocked threads

mutex (for list)

kthread_3kthread_2

kthread_1

Thread sets event

update predicate

wake up one thread

Page 84: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

84GWU CS 259 Brad Taylor Spring 2004

Condition Variables

• Associated with a predicate which is Associated with a predicate which is protected by a mutex (protected by a mutex (usually a spin lockusually a spin lock))

• Useful for event notificationUseful for event notification• Can wakeup one or all sleeping threads!Can wakeup one or all sleeping threads!• Up to 3 or more mutex are typically Up to 3 or more mutex are typically

required:required:– one for the predicateone for the predicate– one for the sleep queue (or CV list)one for the sleep queue (or CV list)– one or more for the scheduler queue (switch())one or more for the scheduler queue (switch())

• deadlock avoided by requiring a strict orderdeadlock avoided by requiring a strict order

Page 85: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

85GWU CS 259 Brad Taylor Spring 2004

CV Implementation

void wait (cv *c, mutex_t *s){ lock (&cv->listlock); add thread to queue unlock (&cv->listlock); unlock (s); swtch (); /* return => after wakup */ lock (s); return;}

void signal (cv *c){ lock (&cv->listlock); remove a thread from list unlock (&cv->listlock); if thread, make runnable; return; }void broadcast (cv *c){ lock (&cv->listlock); while (list is nonempty) { remove a thread make it runnable} unlock (&cv->listlock); return;}

Page 86: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

86GWU CS 259 Brad Taylor Spring 2004

Reader/Writer Locks

• Many readers, one writerMany readers, one writer• Writer can only acquire lock if no Writer can only acquire lock if no

readers are active - readers are active - lockexclusive lockexclusive (rwlock_t *r)(rwlock_t *r)

• Any reader can acquire lock if no Any reader can acquire lock if no writers are active - writers are active - lockshared lockshared (rwlock_t *r)(rwlock_t *r)

• Writer release lock, do we wake up Writer release lock, do we wake up all readers or a writer?all readers or a writer?

Page 87: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

87GWU CS 259 Brad Taylor Spring 2004

RW Locks

• Preferred solution, Preferred solution, – writer wakes up all sleeping readers, writer wakes up all sleeping readers, – readers do nothing if there are still active readers do nothing if there are still active

readers.readers.– But, this could lead to writer starvation, avoided But, this could lead to writer starvation, avoided

if if locksharedlockshared fails when there is a waiting writer. fails when there is a waiting writer.

• What about when a reader wants to What about when a reader wants to upgrade to an exclusive lock? upgrade to an exclusive lock? – must give preference to reader's must give preference to reader's upgradeupgrade– multiple multiple upgradeupgrade requests are problematic, requests are problematic,

could return fail and release lockcould return fail and release lock

Page 88: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

88GWU CS 259 Brad Taylor Spring 2004

Reference Counts

• Protects object when outstanding Protects object when outstanding referencereference

• Consider the case where process/kernel as Consider the case where process/kernel as reference to data object which has been reference to data object which has been reallocated for another purpose.reallocated for another purpose.

• Fix: kernel reference counts objects Fix: kernel reference counts objects – When kernel provides reference to object it When kernel provides reference to object it

increments the reference countincrements the reference count– When process releases object then reference When process releases object then reference

count is decrementedcount is decremented

• When reference count falls to zero, no When reference count falls to zero, no outstanding object references guaranteedoutstanding object references guaranteed

Page 89: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

89GWU CS 259 Brad Taylor Spring 2004

Considerations

• Deadlock avoidance: Deadlock avoidance: – hierarchical - impose order on lockshierarchical - impose order on locks– stochastic locking - when violating hierarchy, use stochastic locking - when violating hierarchy, use

trylocktrylock• Recursive Locks - attempting to acquire a Recursive Locks - attempting to acquire a

lock already owned by a thread succeedslock already owned by a thread succeeds• Adaptive Locks (Mutex)Adaptive Locks (Mutex)

– either a spin or blocking lock used depending on either a spin or blocking lock used depending on circumstances and performance considerationscircumstances and performance considerations

• Protecting invariants, predicates, data, Protecting invariants, predicates, data, operations (monitors)operations (monitors)

• Granularity and DurationGranularity and Duration

Page 90: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

90GWU CS 259 Brad Taylor Spring 2004

SVR4.2/MP

• Basic Locks: nonrecursive mutex, Basic Locks: nonrecursive mutex, short-term, thread may not blockshort-term, thread may not block

• Read-Write Locks: nonrecursive; short-Read-Write Locks: nonrecursive; short-term; thread may not block; single-term; thread may not block; single-writer, multiple reader semanticswriter, multiple reader semantics

• Sleep Locks: nonrecursive locks, long-Sleep Locks: nonrecursive locks, long-term, thread may blockterm, thread may block

• Synchronization variable (CV)Synchronization variable (CV)

Page 91: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

91GWU CS 259 Brad Taylor Spring 2004

Solaris

• Types:Types:– Mutexes - spin, adaptiveMutexes - spin, adaptive– Condition VariablesCondition Variables– Counting SemaphoresCounting Semaphores– Multiple-Reader, Single-WriterMultiple-Reader, Single-Writer

• Queuing mechanism:Queuing mechanism:– turnstiles - used for mutexes, turnstiles - used for mutexes,

reader/writer and semaphoresreader/writer and semaphores– sleep queues - condition variablessleep queues - condition variables

Page 92: 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 6: POSIX Threads, Semaphores and Synchronization

92GWU CS 259 Brad Taylor Spring 2004

Solaris 2 Synchronization

• Implements variety of locks to support multitasking, Implements variety of locks to support multitasking, multithreading (including real-time threads), and multithreading (including real-time threads), and multiprocessingmultiprocessing

• Uses Uses adaptive mutexesadaptive mutexes for efficiency when protecting for efficiency when protecting data from short code segmentsdata from short code segments

• Uses Uses condition variablescondition variables and and readers-writersreaders-writers locks when locks when longer sections of code need access to data longer sections of code need access to data

• Uses Uses turnstilesturnstiles to order the list of threads waiting to to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lockacquire either an adaptive mutex or reader-writer lock