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

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements


Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
Multiprocessors and Multithreading – classroom slides.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
University of Pennsylvania 9/21/00CSE 3801 Concurrent Process Synchronization (Lock and semaphore) CSE 380 Lecture Note 5 Insup Lee.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Concurrency: Deadlock and Starvation Chapter 6.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
CS510 Concurrent Systems Introduction to Concurrency.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 10: Threads and Thread Synchronization.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Race condition The scourge of parallel and distributed computing...
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Threads CSIT 402 Data Structures II. many to oneone to one many to many Multithreading models.
Pthreads: A shared memory programming model
Synchronizing Threads with Semaphores
CSNB334 Advanced Operating Systems 4. Concurrency : Mutual Exclusion and Synchronization.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
CS 3204 Operating Systems Godmar Back Lecture 7. 12/12/2015CS 3204 Fall Announcements Project 1 due on Sep 29, 11:59pm Reading: –Read carefully.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
pThread synchronization
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
PROCESS MANAGEMENT IN MACH
Background on the need for Synchronization
Process Synchronization
Threads Threads.
Chapter 4: Threads.
CS252: Systems Programming
PTHREADS AND SEMAPHORES
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
“The Little Book on Semaphores” Allen B. Downey
EECE.4810/EECE.5730 Operating Systems
POSIX Threads(pthreads)
Presentation transcript:

Threads Manohara Pallewatta CSIM/SAT

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)

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.

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

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.

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

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

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

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

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

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

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

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.

Basic thread functions (cont.) #include 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

#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

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

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

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

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

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.

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.

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.

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.

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

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.

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.

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

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.

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.

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.

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

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

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.

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.

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.

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

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

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

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

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.

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.

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