html5-img
1 / 46

Threads

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).

laksha
Download Presentation

Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threads Manohara Pallewatta CSIM/SAT

  2. 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)

  3. 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.

  4. 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

  5. Why threads ? • The cost of creating a thread is much less than the cost of creating a process.

  6. Types of threads • Pthreads • OS specific threads • Solaris threads

  7. 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); }

  8. How many processes will this program create ?. main() { fork(); fork(); fork(); }

  9. 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

  10. How many threads this program create ? main() { thr_create(); thr_create(); thr_create(); }

  11. The answer is 4. Thread 1 main() { thr_create(..); thr_create(..); thr_create(..); thr_join(..); } Thread 2 Thread 3

  12. 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

  13. 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.

  14. Basic thread functions (cont.) #include <thread.h> void thr_exit(void *status); Terminates the calling thread.

  15. 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.

  16. 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.

  17. Synchronization Objects • Mutex locks • Condition Variables • Read/Write locks • Semaphores

  18. 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.

  19. 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);

  20. 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); }

  21. 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); .

  22. 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); .

  23. Mutex locks (cont.) Another solution for deadlocks. • Access locks in the same order.

  24. 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.

  25. 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); . .

  26. 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);

  27. 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);

  28. 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.

  29. 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.

  30. 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).

  31. 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.

  32. 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.

  33. 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.

  34. 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);

  35. 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); }

  36. 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.

  37. 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.

  38. 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.

  39. 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);

  40. Double-linked lists • Enqueue operation elem prev next queue.head prev next prev next

  41. Double-linked lists (cont.) • Dequeue operation elem prev next queue.tail prev next prev next

  42. Double-linked lists (cont.) • Null list queue.tail queue.head prev next prev next

  43. 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.

  44. 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.

  45. List example (cont.) Free queue head tail Output thread Output thread Input thread Input thread Processed queue tail head

More Related