PThreads.

Slides:



Advertisements
Similar presentations
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Advertisements

Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Multi-core Programming Programming with Posix Threads.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
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,
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
The University of Adelaide, School of Computer Science
5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Problems with Semaphores Incorrect use of semaphore operations:
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Programming with POSIX* Threads Intel Software College.
Programming Shared Address Space Platforms Carl Tropper Department of Computer Science.
Pthreads: A shared memory programming model
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Deadlock A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. Example : “When.
Programming with Threads. Threads  Sometimes called a lightweight process  smaller execution unit than a process  Consists of:  program counter 
pThread synchronization
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
COMP7330/7336 Advanced Parallel and Distributed Computing POSIX Thread API Dr. Xiao Qin Auburn University
回到第一頁 What are threads n Threads are often called "lightweight processes” n In the UNIX environment a thread: u Exists within a process and uses the process.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
COMP7330/7336 Advanced Parallel and Distributed Computing POSIX Thread API Dr. Xiao Qin Auburn University
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Tutorial 4. In this tutorial session we’ll see Threads.
Synchronization Deadlocks and prevention
Background on the need for Synchronization
Realizing Concurrency using the thread model
Applied Operating System Concepts -
Threads Threads.
Boost String API & Threads
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Pthread Mutex Dr. Xiao Qin Auburn University.
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Thread synchronization
Realizing Concurrency using Posix Threads (pthreads)
Chapter 7 Deadlocks.
Chapter 7: Deadlocks.
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
COT 5611 Operating Systems Design Principles Spring 2014
COP 4600 Operating Systems Fall 2010
Chapter 7: Deadlocks.
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Deadlocks Peng Lu In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources; if the.
Chapter 8: Deadlocks.
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Deadlocks Session - 13.
Chapter 7: Deadlocks.
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
CSS430 Deadlocks Textbook Ch8
Chapter 7: Deadlock CSS503 Systems Programming
POSIX Threads(pthreads)
Presentation transcript:

PThreads

Shared Memory Model The logical machine model of a thread-based programming paradigm.

POSIX Threads Programming https://computing.llnl.gov/tutorials/pthreads/ What is a Thread? Difference between thread and a process Why Pthreads? Shared Memory Model

Example programs Example: Pthread Creation and Termination pthread_create pthread_exit Passing Arguments to Threads use struct Joining Threads – pthread_join (threadid, status)

Thread Basics: Creation and Termination Pthreads provides two basic functions for specifying concurrency in a program: #include <pthread.h> int pthread_create ( pthread_t *thread_handle, const pthread_attr_t *attribute, void * (*thread_function)(void *), void *arg); The function pthread_create invokes function thread_function as a thread int pthread_join (pthread_t thread, void **ptr);

Thread Termination There are several ways in which a thread may be terminated: The thread makes a call to the pthread_exit subroutine. The entire process is terminated due to making a call to exit() If main() finishes first, without calling pthread_exit itself All of the above

Synchronization Primitives in Pthreads When multiple threads attempt to manipulate the same data item, the results can often be incoherent if proper care is not taken to synchronize them. Consider: /* each thread tries to update variable best_cost as follows */ if (my_cost < best_cost) best_cost = my_cost; Assume that there are two threads, the initial value of best_cost is 100, and the values of my_cost are 50 and 75 at threads t1 and t2. Depending on the schedule of the threads, the value of best_cost could be 50 or 75! The value 75 does not correspond to any serialization of the threads.

Mutual Exclusion The code in the previous example corresponds to a critical segment; i.e., a segment that must be executed by only one thread at any time. Critical segments in Pthreads are implemented using mutex locks. Mutex-locks have two states: locked and unlocked. At any point of time, only one thread can lock a mutex lock. A lock is an atomic operation. A thread entering a critical segment first tries to get a lock. It goes ahead when the lock is granted.

Mutual Exclusion The Pthreads API provides the following functions for handling mutex-locks: int pthread_mutex_lock ( pthread_mutex_t *mutex_lock); int pthread_mutex_unlock ( int pthread_mutex_init ( pthread_mutex_t *mutex_lock, const pthread_mutexattr_t *lock_attr);

Mutual Exclusion We can now write our previously incorrect code segment as: pthread_mutex_t minimum_value_lock; ... main() { .... pthread_mutex_init(&minimum_value_lock, NULL); } void *find_min(void *list_ptr) { pthread_mutex_lock(&minimum_value_lock); if (my_min < minimum_value) minimum_value = my_min; /* and unlock the mutex */ pthread_mutex_unlock(&minimum_value_lock);

Producer-Consumer Using Locks The producer-consumer scenario imposes the following constraints: The producer thread must not overwrite the shared buffer when the previous task has not been picked up by a consumer thread. The consumer threads must not pick up tasks until there is something present in the shared data structure. Individual consumer threads should pick up tasks one at a time.

Producer-Consumer Using Locks pthread_mutex_t task_queue_lock; int task_available; ... main() { .... task_available = 0; pthread_mutex_init(&task_queue_lock, NULL); } void *producer(void *producer_thread_data) { while (!done()) { inserted = 0; create_task(&my_task); while (inserted == 0) { pthread_mutex_lock(&task_queue_lock); if (task_available == 0) { insert_into_queue(my_task); task_available = 1; inserted = 1; pthread_mutex_unlock(&task_queue_lock);

Producer-Consumer Using Locks void *consumer(void *consumer_thread_data) { int extracted; struct task my_task; /* local data structure declarations */ while (!done()) { extracted = 0; while (extracted == 0) { pthread_mutex_lock(&task_queue_lock); if (task_available == 1) { extract_from_queue(&my_task); task_available = 0; extracted = 1; } pthread_mutex_unlock(&task_queue_lock); process_task(my_task);

Deadlock When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.

The Deadlock Problem A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held by another process in the set Example #2 A system has 2 disk drives P1 and P2 each hold one disk drive and each needs the other one

Deadlocks Shared variables: A, B Processor Po Processor P1 // some computation lock (A) lock (B) ...

Deadlock Situation where a thread is waiting for an object lock that another thread holds, and this second thread is waiting for an object lock that the first thread holds Since each thread is waiting for the other thread to relinquish a lock, they remain waiting forever.

Deadlock in Distributed programs Processor Po Processor P1 // some computation get X from P1 get Y from Po send Y to P1 send X to Po ...

Causes of deadlocks Mutual exclusion of shared resources Hold and wait No preemption Cyclic wait For a deadlock, all conditions should be met.

Causes of deadlocks Mutual exclusion of shared resources - Only one process can access a resource at a given time. Hold and wait - Process already holding a resource can request additional resources, without relinquishing their current resources. No preemption – One process cannot forcibly remove another process’ resource. Cyclic wait- Two or more processes form a circular chain where each process is waiting on another resource in the chain.

Circular wait There exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, …........................................................., Pn–1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0

Methods for Handling Deadlocks Prevention - Ensure that the system will never enter a deadlock state Avoidance- Ensure that the system will never enter an unsafe state Detection - Allow the system to enter a deadlock state and then recover Do Nothing Ignore the problem and let the user or system administrator respond to the problem; used by most operating systems, including Windows and UNIX

Circular Wait elimination Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 1 - Card reader; 2 - Printer; 3 - Plotter 4 - Tape drive; 5 ≡ Card punch Request resource 2 and 4 = valid Request resource 3 and 2 = invalid