Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread."— Presentation transcript:

1 Threads Manohara Pallewatta CSIM/SAT

2 Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread creationThread creation Bound and Unbound threadsBound and Unbound threads SynchronizationSynchronization –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.Concurrency - Interwoven execution of two or more tasks. Parallelism - Simultaneous 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.In a single CPU machine concurrency can exist but not parallelism.

4 Processes and Threads Traditional UNIX modelTraditional 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

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

6 Types of threads PthreadsPthreads OS specific threadsOS specific threads Solaris threadsSolaris threads

7 A simple multi-threaded program #define _REENTRANT #include #include 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(); }

9 The answer is 8. main(){ fork(); fork(); } 1 fork(); fork(); fork(); 1 fork(); fork(); fork(); 2 fork(); fork(); fork(); 3 fork(); 2 fork(); 4 fork(); 1 fork(); 15368274

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

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

12 Basic thread functions #include 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 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 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 #include 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 Mutex locks

19 #include ( 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.)

20 mutex_t count_mutex; int count; void increment_count() { mutex_lock(&count_mutex); count = count + 1; mutex_unlock(&count_mutex); } Mutex locks (cont.) Restricting access to an integer while being incremented Mutex locks (cont.) Restricting access to an integer while being incremented

21 Thread 1. mutex_lock(&m1); mutex_lock(&m2);. mutex_unlock(&m2) ; mutex_unlock(&m1) ;. Mutex locks (cont.) Deadlock situations Mutex locks (cont.) Deadlock situations m1m2 Thread 2. mutex_lock(&m2); mutex_lock(&m1);. mutex_unlock(&m1) ; mutex_unlock(&m2) ;.

22 Mutex locks (cont.) Conditional locking(A solution for deadlocks) Mutex locks (cont.) Conditional locking(A solution for deadlocks) Thread 2. for(;;;) { mutex_lock(&m2); if (mutex_trylock(&m1)==0) break; mutex_unlock(&m2); }. mutex_unlock(&m1); mutex_unlock(&m2);. m1m2 Thread 1. mutex_lock(&m1); mutex_lock(&m2);. mutex_unlock(&m2) ; mutex_unlock(&m1) ;.

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

24 Condition Variables Condition Variables #include 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.) Condition Variables (cont.) #include 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 mutex locks.Condition variables are used with mutex locks.

26 Condition Variables (cont.) Condition Variables (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.

27 Condition Variables (cont.) Condition Variables (cont.) #include 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.) 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 Producer/Consumer example 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 before accessing sum.Mutex lock should be acquired before accessing sum.

30 Read/Write locks Read/Write locks Allow simultaneous read access by many threads while restricting the write access only to one thread.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).When the write access is granted reading is not allowed (ie. blocked).

31 Read/Write locks (cont.) Read/Write locks (cont.) #include 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_THREADThe readers/writer lock can be used to synchronize threads in this process, only. arg is ignored.

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

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

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

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

36 Semaphores 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.) 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.) 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.) 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 Double-linked lists prev next queue.head prev next prev next Enqueue operation Enqueue operation elem

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

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

43 List example List example Two queues, free queue and processed queue implemented as double-linked lists.Two queues, free queue and processed queue implemented as double-linked lists. Each element has a mutex lock and a data area.Each element has a mutex lock and a data area. Queue heads and tails are elements, complete with mutex locks.Queue heads and tails are elements, complete with mutex locks.

44 List example (cont.) List example (cont.) Input threads will dequeue from the free queue and enqueue in the processed queue.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.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.Free queue is filled up at the beginning while the processed queue is empty.

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

46


Download ppt "Threads Manohara Pallewatta CSIM/SAT. Overview Concurrency and parallelismConcurrency and parallelism Processes and threadsProcesses and threads Thread."

Similar presentations


Ads by Google