threads manohara pallewatta csim/sat. overview concurrency and parallelismconcurrency and...

46
Threads Threads Manohara Pallewatta Manohara Pallewatta CSIM/SAT CSIM/SAT

Upload: melissa-nelson

Post on 03-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

ThreadsThreads

Manohara PallewattaManohara Pallewatta

CSIM/SATCSIM/SAT

Page 2: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

OverviewOverview• Concurrency and parallelismConcurrency and parallelism

• Processes and threadsProcesses and threads

• Thread creationThread creation

• Bound and Unbound threadsBound and Unbound threads

• SynchronizationSynchronization– Mutex locksMutex locks

– Condition variables (Example 1: Condition variables (Example 1: Producer/Consumer problem)Producer/Consumer problem)

– Read Write locksRead Write locks

– Semaphores (Example 2: Linked lists)Semaphores (Example 2: Linked lists)

Page 3: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Concurrency and Concurrency and ParallelismParallelism

• Concurrency - Interwoven execution Concurrency - Interwoven execution of two or more tasks.of two or more tasks.

• Parallelism - Simultaneous execution Parallelism - Simultaneous execution of two or more tasks.of two or more tasks.

• In a single CPU machine concurrency In a single CPU machine concurrency can exist but not parallelism.can exist but not parallelism.

Page 4: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Processes and ThreadsProcesses and Threads

• Traditional UNIX Traditional UNIX modelmodel– fork() task creationfork() task creation

– exec task exec task executionexecution

– wait waiting wait waiting

– exit finishexit finish

• Threads modelThreads model– thr_create task thr_create task

creation creation and and

execution. execution.

– thr_join waitingthr_join waiting

– thr_exit exit thr_exit exit threadthread

Page 5: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Why threads ?Why threads ?

• The cost of creating a thread is The cost of creating a thread is much less than the cost of creating much less than the cost of creating a process. a process.

Page 6: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Types of threadsTypes of threads

•PthreadsPthreads •OS specific OS specific threadsthreads

•Solaris threadsSolaris threads

Page 7: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

A simple multi-threaded A simple multi-threaded programprogram

#define _REENTRANT#define _REENTRANT#include <stdio.h>#include <stdio.h>#include <thread.h>#include <thread.h>

void *func(void);void *func(void);

main()main(){{thread_t t_id;thread_t t_id;int exit_value;int exit_value;

thr_create(0,0,tprint,(void *)NULL,0,&t_id);thr_create(0,0,tprint,(void *)NULL,0,&t_id);thr_join(t_id,0,&exit_value);thr_join(t_id,0,&exit_value);}}

void *tprint(void) {void *tprint(void) {printf(“Printing from a thread”);printf(“Printing from a thread”);thr_exit(0);thr_exit(0);}}

Page 8: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

How many processes will How many processes will this program create ?.this program create ?.

main()main(){{ fork();fork(); fork();fork(); fork();fork();}}

Page 9: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

The answer is 8.The answer is 8.main()main(){{ fork();fork(); fork();fork(); fork();fork();}}

1 fork();1 fork(); fork();fork(); fork();fork();

1 fork();1 fork(); fork();fork();

2 fork();2 fork(); fork();fork();

3 fork();3 fork(); 2 fork();2 fork(); 4 fork();4 fork();1 fork();1 fork();

11 55 33 66 8822 77 44

Page 10: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

How many threads this How many threads this program create ?program create ?

main()main(){{ thr_create();thr_create(); thr_create();thr_create(); thr_create();thr_create();}}

Page 11: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

The answer is 4.The answer is 4.

main()main(){{ thr_create(..);thr_create(..);

thr_create(..);thr_create(..);

thr_create(..);thr_create(..);

thr_join(..);thr_join(..);}}

Thread 1Thread 1

Thread 2Thread 2

Thread 3Thread 3

Page 12: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Basic thread functionsBasic thread functions

#include <thread.h>

int thr_create(void *stack_base, size_t stack_size, void *(*start_routine)(void *), void *arg, long flags, thread_t *new_thread);

stack_base and stack_sizeUsed only when the default stack is inadequate. Can be 0for most operations.

flagsFor binding threads, Creation of LWPs.THR_NEW_LWP THR_BOUND

Page 13: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Basic thread functions Basic thread functions (cont.)(cont.)

#include <thread.h>

int thr_join(thread_t wait_for, thread_t *departed, void **status);

Blocks the calling thread until the thread specified by wait_for terminates. If wait_for is (thread_t)0, then thr_join() waits for any undetached thread in the process to terminate.

Page 14: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Basic thread functions Basic thread functions (cont.)(cont.)

#include <thread.h>

void thr_exit(void *status);

Terminates the calling thread.

Page 15: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Bound and Unbound Bound and Unbound threadsthreads

• Light Weight Process A virtual CPU that executes code and system calls.

The kernel schedules LWPs on the CPU resourses.

The threads library schedules threads in a process on the pool of LWPs.

Page 16: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Bound and Unbound Bound and Unbound threads (cont.)threads (cont.)

• Unbound threads Threads which are scheduled on the pool of

available LWPs.

• Bound threads Threads which are permanently bound to a LWP.

Page 17: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Synchronization ObjectsSynchronization Objects

• Mutex locks

• Condition Variables

• Read/Write locks

• Semaphores

Page 18: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

#include <synch.h> int mutex_init(mutex_t *mp, int type, void * arg);

type

USYNC_PROCESS The mutex can be used to synchronize threads in this

process and other processes. Only one process should initialize the mutex. arg is ignored.

USYNC_THREAD The mutex can be used to synchronize threads in this

process, only. arg is ignored.

• Mutex locksMutex locks

Page 19: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

#include <synch.h> ( included by threads.h )

int mutex_destroy(mutex_t *mp); mutex_destroy() destroys any state associated with the

mutex pointed to by mp. int mutex_lock(mutex_t *mp);

int mutex_trylock(mutex_t *mp); Attempts to lock the mutex pointed to by mp. If the mutex is already locked it returns with an error.

int mutex_unlock(mutex_t *mp);

• Mutex locks (cont.)Mutex locks (cont.)

Page 20: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

mutex_t count_mutex;int count;

void increment_count(){

mutex_lock(&count_mutex);count = count + 1;mutex_unlock(&count_mutex);

}

• Mutex locks (cont.)Mutex locks (cont.) Restricting access to an integer while Restricting access to an integer while being incrementedbeing incremented

Page 21: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

Thread 1..mutex_lock(&m1);mutex_lock(&m2);..mutex_unlock(&m2);mutex_unlock(&m1);.

• Mutex locks (cont.)Mutex locks (cont.) Deadlock situationsDeadlock situations

m1 m2

Thread 2..mutex_lock(&m2);mutex_lock(&m1);..mutex_unlock(&m1);mutex_unlock(&m2);.

Page 22: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Mutex locks (cont.)Mutex locks (cont.) Conditional locking(A solution for Conditional locking(A solution for deadlocks)deadlocks)

Thread 2.for(;;;) {mutex_lock(&m2);if (mutex_trylock(&m1)==0)

break;mutex_unlock(&m2);}.mutex_unlock(&m1);mutex_unlock(&m2);.

m1 m2

Thread 1..mutex_lock(&m1);mutex_lock(&m2);..mutex_unlock(&m2);mutex_unlock(&m1);.

Page 23: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Mutex locks (cont.)Mutex locks (cont.) Another solution for deadlocks. Another solution for deadlocks.

• Access locks in the same order. Access locks in the same order.

Page 24: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Condition VariablesCondition Variables #include <synch.h>

int cond_init(cond_t *cvp, int type, int arg);

type

USYNC_PROCESS The mutex can be used to synchronize threads in this

process and other processes. Only one process should initialize the mutex. arg is ignored.

USYNC_THREAD The mutex can be used to synchronize threads in this

process, only. arg is ignored.

Page 25: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Condition Variables Condition Variables (cont.)(cont.)

#include <synch.h>

int cond_wait(cond_t *cvp, mutex_t *mp);

.

.

mutex_lock(&m);while(!(condition))

cond_wait(condition,&m);mutex_unlock(&m);

.

.

• Condition variables are used with Condition variables are used with mutex locks. mutex locks.

Page 26: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Condition Variables Condition Variables (cont.)(cont.)

int sum;cond_t sum_is_pos;mutex_t m;..mutex_init(&m,.....);cond_init(&sum_is_pos,...);

Thread 2 (dec).mutex_lock(&m);while (sum==0)cond_wait(&sum_is_pos,&m);

sum--;mutex_unlock(&m);

Thread 1 (inc).

mutex_lock(&m);sum++;cond_signal(&sum_is_pos)mutex_unlock(&m);.

• Rule: sum >= 0. Rule: sum >= 0.

Page 27: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Condition Variables Condition Variables (cont.)(cont.)

#include <synch.h> int cond_signal(cond_t *cvp);

Unblocks one thread that is blocked on the condition variable pointed to by cvp.

int cond_broadcast(cond_t *cvp); Unblocks all threads that are blocked on the condition variable pointed to by cvp.

int cond_destroy(cond_t *cvp);

Page 28: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Condition Variables Condition Variables (cont.)(cont.)

Note:

If no threads are blocked on the condition variable then cond_signal() and cond_broadcast() have no effect.

cond_signal() and cond_broadcast() should be called under the protection of the same mutex that is used with the condition variable being signaled. Otherwise the condition variable may be signaled between the test of the associated condition and blocking in cond_wait(). This can cause an infinite wait.

Page 29: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Producer/Consumer Producer/Consumer exampleexample

Prod 1

value 1

Prod 2

value 2

Prod 3

value 3

Cons 1

value 1

Cons 2

value 2

Cons 3

value 3

sum

lock

• 0<sum<1000<sum<100

• Mutex lock should be acquired Mutex lock should be acquired before accessing sum. before accessing sum.

Page 30: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Read/Write locksRead/Write locks

• Allow simultaneous read access by Allow simultaneous read access by many threads while restricting the many threads while restricting the write access only to one thread.write access only to one thread.

• When the write access is granted When the write access is granted reading is not allowed (ie. blocked).reading is not allowed (ie. blocked).

Page 31: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Read/Write locks (cont.)Read/Write locks (cont.)

#include <synch.h>

int rwlock_init(rwlock_t *rwlp, int type, void * arg);

USYNC_PROCESS The readers/writer lock can be used to synchronize threads in this process and other processes. Only one process should initialize the readers/writer lock. arg is ignored.

USYNC_THREAD The readers/writer lock can be used to synchronize threads in this process, only. arg is ignored.

Page 32: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Read/Write locks (cont.)Read/Write locks (cont.)int rw_rdlock(rwlock_t *rwlp); Acquires a read lock on the readers/writer lock pointed to by rwlp. If the readers/writer lock is already locked for writing, the calling thread blocks until the write lock is released. More than one thread may hold a read lock on a readers/writer lock at any one time.

int rw_wrlock(rwlock_t *rwlp); Acquires a write lock on the readers/writer lock pointed to by rwlp. If the readers/writer lock is already locked for reading or writing, the calling thread blocks until all the read locks and write locks are released. Only one thread may hold a write lock on a readers/writer lock at any one time.

Page 33: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Read/Write locks (cont.)Read/Write locks (cont.)

int rw_tryrdlock(rwlock_t *rwlp); Attempts to acquire a read lock on the readers/writer lock pointed to by rwlp. If the readers/writer lock is already locked for writing, it returns an error. Otherwise the read lock is acquired.

int rw_trywrlock(rwlock_t *rwlp); Attempts to acquire a write lock on the readers/writer lock pointed to by rwlp. If the readers/writer lock is already locked for reading or writ- ing, it returns an error.

Page 34: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Read/Write locks (cont.)Read/Write locks (cont.)

int rw_unlock(rwlock_t *rwlp);

Unlocks a readers/writer lock pointed to by rwlp. The readers/writer lock must be locked and the cal- ling thread must hold the lock either for reading or writ- ing.

int rwlock_destroy(rwlock_t *rwlp);

Page 35: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Read/Write locks (cont.)Read/Write locks (cont.)

float balance;rwlock_t account_lock;.rwlock_init(&account_lock,...);

Thread 2.deposit(100.0);

Thread 1 .balance=check_balance();

float check_balance(){float bal; rw_rdlock(&account_lock);bal=balance;rw_unlock(&account_lock);return(bal);}

void deposit(float amount){ rw_wrlock(&account_lock);balance += amount;rw_unlock(&account_lock);

}

Page 36: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• SemaphoresSemaphores

•Wait Operation - Atomically decrease the semaphore count.

•Signal Operation - Atomically increase the semaphore count.

•sema_wait and sema_post are the threads’ equivalent of wait and signal operations.

•There is also a sema_trywait which is a conditional form of the wait operation.

Page 37: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Semaphores (cont.)Semaphores (cont.) int sema_init(sema_t *sp, unsigned int count, int type, void * arg);

USYNC_PROCESS The semaphore can be used to synchronize threads in this process and other processes. Only one process should ini- tialize the semaphore. arg is ignored.

USYNC_THREAD The semaphore can be used to synchronize threads in this process, only. arg is ignored.

Page 38: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Semaphores (cont.)Semaphores (cont.)int sema_wait(sema_t *sp);

Blocks the calling thread until the count in the semaphore pointed to by sp becomes greater than zero and then atomically decrements it.

int sema_trywait(sema_t *sp);

Atomically decrements the count in the semaphore pointed to by sp if the count is greater than zero. Otherwise it returns an error.

Page 39: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Semaphores (cont.)Semaphores (cont.)int sema_post(sema_t *sp);

Atomically increments the count semaphore pointed to by sp. If there are any threads blocked on the semaphore, one is unblocked.

int sema_destroy(sema_t *sp);

Page 40: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Double-linked listsDouble-linked lists

prev

next queue.head

prev

nextprev

next

• Enqueue operationEnqueue operation

elem

Page 41: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Double-linked lists (cont.)Double-linked lists (cont.)

prev

next queue.tail

prev

nextprev

next

• Dequeue operationDequeue operation

elem

Page 42: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• Double-linked lists (cont.)Double-linked lists (cont.)

queue.tail

queue.head

prev

next

• Null listNull list

prev

next

Page 43: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• List exampleList example

• Two queues, free queue and Two queues, free queue and processed queue implemented as processed queue implemented as double-linked lists.double-linked lists.

• Each element has a mutex lock and a Each element has a mutex lock and a data area.data area.

• Queue heads and tails are elements, Queue heads and tails are elements, complete with mutex locks.complete with mutex locks.

Page 44: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• List example (cont.)List example (cont.)

• Input threads will dequeue from the free Input threads will dequeue from the free queue and enqueue in the processed queue and enqueue in the processed queue.queue.

• Output threads will dequeue from the Output threads will dequeue from the processed queue and enqueue in the free processed queue and enqueue in the free queue.queue.

• Free queue is filled up at the beginning Free queue is filled up at the beginning while the processed queue is empty.while the processed queue is empty.

Page 45: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread

• List example (cont.)List example (cont.)

Free queue

head tail

Inputthread

Processed queue

tail head

Inputthread

Outputthread

Outputthread

Page 46: Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread