460 likes | 470 Views
Overview of concurrency and parallelism in CSIM/SAT. Covers processes and threads, thread creation, synchronization using mutex locks and condition variables, and the use of semaphores.
E N D
Threads Manohara Pallewatta CSIM/SAT
Overview • Concurrency and parallelism • Processes and threads • Thread creation • Bound and Unbound threads • Synchronization • Mutex locks • Condition variables (Example 1: Producer/Consumer problem) • Read Write locks • Semaphores (Example 2: Linked lists)
Concurrency and Parallelism • Concurrency - Interwoven execution of two or more tasks. • Parallelism - Simultaneous execution of two or more tasks. • In a single CPU machine concurrency can exist but not parallelism.
Traditional UNIX model fork() task creation exec task execution wait waiting exit finish Threads model thr_create task creation and execution. thr_join waiting thr_exit exit thread Processes and Threads
Why threads ? • The cost of creating a thread is much less than the cost of creating a process.
Types of threads • Pthreads • OS specific threads • Solaris threads
A simple multi-threaded program #define _REENTRANT #include <stdio.h> #include <thread.h> void *func(void); main() { thread_t t_id; int exit_value; thr_create(0,0,tprint,(void *)NULL,0,&t_id); thr_join(t_id,0,&exit_value); } void *tprint(void) { printf(“Printing from a thread”); thr_exit(0); }
How many processes will this program create ?. main() { fork(); fork(); fork(); }
1 fork(); fork(); fork(); The answer is 8. main() { fork(); fork(); fork(); } 1 fork(); fork(); 2 fork(); fork(); 1 fork(); 3 fork(); 2 fork(); 4 fork(); 1 5 3 6 2 7 4 8
How many threads this program create ? main() { thr_create(); thr_create(); thr_create(); }
The answer is 4. Thread 1 main() { thr_create(..); thr_create(..); thr_create(..); thr_join(..); } Thread 2 Thread 3
Basic 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_size Used only when the default stack is inadequate. Can be 0 for most operations. flags For binding threads, Creation of LWPs. THR_NEW_LWP THR_BOUND
Basic thread functions (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.
Basic thread functions (cont.) #include <thread.h> void thr_exit(void *status); Terminates the calling thread.
Bound and Unbound threads • 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.
Bound and Unbound threads (cont.) • Unbound threads • Threads which are scheduled on the pool of available LWPs. • Bound threads • Threads which are permanently bound to a LWP.
Synchronization Objects • Mutex locks • Condition Variables • Read/Write locks • Semaphores
Mutex locks #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 locks (cont.) #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.)Restricting access to an integer while being incremented mutex_t count_mutex; int count; void increment_count() { mutex_lock(&count_mutex); count = count + 1; mutex_unlock(&count_mutex); }
Mutex locks (cont.)Deadlock situations m1 m2 Thread 1 . . mutex_lock(&m1); mutex_lock(&m2); . . mutex_unlock(&m2); mutex_unlock(&m1); . Thread 2 . . mutex_lock(&m2); mutex_lock(&m1); . . mutex_unlock(&m1); mutex_unlock(&m2); .
Mutex locks (cont.)Conditional locking(A solution for deadlocks) m1 m2 Thread 2 . for(;;;) { mutex_lock(&m2); if (mutex_trylock(&m1)==0) break; mutex_unlock(&m2); } . mutex_unlock(&m1); mutex_unlock(&m2); . Thread 1 . . mutex_lock(&m1); mutex_lock(&m2); . . mutex_unlock(&m2); mutex_unlock(&m1); .
Mutex locks (cont.) Another solution for deadlocks. • Access locks in the same order.
Condition 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.
Condition Variables (cont.) #include <synch.h> int cond_wait(cond_t *cvp, mutex_t *mp); • Condition variables are used with mutex locks. . . mutex_lock(&m); while(!(condition)) cond_wait(condition,&m); mutex_unlock(&m); . .
Condition Variables (cont.) • Rule: sum >= 0. int sum; cond_t sum_is_pos; mutex_t m; . . mutex_init(&m,.....); cond_init(&sum_is_pos,...); Thread 1 (inc) . mutex_lock(&m); sum++; cond_signal(&sum_is_pos) mutex_unlock(&m); . Thread 2 (dec) . mutex_lock(&m); while (sum==0) cond_wait(&sum_is_pos,&m); sum--; mutex_unlock(&m);
Condition Variables (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);
Condition Variables (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.
Producer/Consumer example Prod 1 value 1 Cons 1 value 1 sum Prod 2 value 2 Cons 2 value 2 lock Prod 3 value 3 Cons 3 value 3 • 0<sum<100 • Mutex lock should be acquired before accessing sum.
Read/Write locks • Allow simultaneous read access by many threads while restricting the write access only to one thread. • When the write access is granted reading is not allowed (ie. blocked).
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.
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.
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.
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);
Read/Write locks (cont.) float balance; rwlock_t account_lock; . rwlock_init(&account_lock,...); Thread 1 . balance=check_balance(); Thread 2 . deposit(100.0); 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); }
Semaphores • 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.
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.
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.
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);
Double-linked lists • Enqueue operation elem prev next queue.head prev next prev next
Double-linked lists (cont.) • Dequeue operation elem prev next queue.tail prev next prev next
Double-linked lists (cont.) • Null list queue.tail queue.head prev next prev next
List example • Two queues, free queue and processed queue implemented as double-linked lists. • Each element has a mutex lock and a data area. • Queue heads and tails are elements, complete with mutex locks.
List example (cont.) • Input threads will dequeue from the free queue and enqueue in the processed queue. • Output threads will dequeue from the processed queue and enqueue in the free queue. • Free queue is filled up at the beginning while the processed queue is empty.
List example (cont.) Free queue head tail Output thread Output thread Input thread Input thread Processed queue tail head