Pthread Synchronization Operating Systems Hebrew University Spring 2004.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Secure Operating Systems Lesson 5: Shared Objects.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda.
Pthread II. Outline Join Mutex Variables Condition Variables.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
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.
Atomic Operations David Monismith cs550 Operating Systems.
Class Announcement TA is expected to add/move office hours to tomorrow for Project 0 Project 0 deadline is extended to next Monday April 13 noon. A copy.
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.
Programming with POSIX* Threads Intel Software College.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
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.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
pThread synchronization
Real-Time Threads Time dependent, usually wrt deadline –Periodic –Aperiodic –Sporadic Priority scheduled Fault tolerant.
1 Programming with Shared Memory - 2 Issues with sharing data ITCS 4145 Parallel Programming B. Wilkinson Jan 22, _Prog_Shared_Memory_II.ppt.
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.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
CMSC621: Advanced Operating Systems Advanced Operating Systems
Chapter 5: Process Synchronization – Part 3
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
Principles of Operating Systems Lecture 11
Operating Systems CMPSC 473
Lecture 13: Producer-Consumer and Semaphores
Lecture 14: Pthreads Mutex and Condition Variables
Process Synchronization
CMPT 300: Operating Systems I
Lecture 14: Pthreads Mutex and Condition Variables
Lecture 13: Producer-Consumer and Semaphores
Chapter 6: Synchronization Tools
Programming with Shared Memory - 2 Issues with sharing data
CSE 451 Section 1/27/2000.
CS 144 Advanced C++ Programming May 7 Class Meeting
“The Little Book on Semaphores” Allen B. Downey
POSIX Threads(pthreads)
Presentation transcript:

Pthread Synchronization Operating Systems Hebrew University Spring 2004

What’s the problem Process thread1 { foo = 1; bar = 2; } Process thread2 { foo = 3; bar = 4; } What are the possible results?

Race condition Two threads racing to perform the same task Interleaving of operations can cause incorrect behavior

Atomic Updates Perform the following items as a single unit When we are done, exactly (A or B) is true –not (A and B) –not part of A and part of B Critical Section!

Mutex Enter and Exit critical section #include int pthread_mutex_lock(mutex); int pthread_mutex_unlock(mutex); mutex_var Thread AThread B lock block var access

No problem Process thread1 { pthread_mutex_lock(l); foo = 1; bar = 2; pthread_mutex_unlock(l); } Process thread2 { pthread_mutex_lock(l); foo = 3; bar = 4; pthread_mutex_unlock(l); } What are the possible results?

What’s the problem? Process producer { while(1) { while(count == 1) no_op; data = c; count = 1; } Process consumer { while(1) { while(count == 0) no_op; c = data; count = 0; // consume c }

Try it with a mutex

One solution Process producer { while(1) { lock(mutex); if (count == 0){ // produce c data = c; count = 1; } unlock(mutex); } Process consumer { while(1) { lock(mutex); if (count == 1){ c = data; count = 0; } unlock(mutex); // consume c } Problems Produce inside of lock Starvation Busy wait

Better solution Process producer { while(1) { // produce c lock(empty); lock(mutex); data = c; unlock(mutex); unlock(full); } Process consumer { while(1) { lock(full); lock(mutex); c = data; unlock(mutex); unlock(empty); // consume c } How many mutexs are in use? What is the initial state of the mutexs?

Condition Signals Wait on a condition Associated with mutex to prevent race condition #include pthread_mutex_t m; pthread_cond_t c; int pthread_cond_wait(&c, &m) int pthread_cond_signal(&c); int pthread_cond_broadcast(&c);

Solution using conditions void producer() { int i = 1; while (1) { pthread_mutex_lock(&mutex); while (count == 1) { pthread_cond_wait(&control, &mutex); } data = i; count = 1; pthread_cond_signal(&control); pthread_mutex_unlock(&mutex); } void consumer() { int i = 0; while (1) { pthread_mutex_lock(&mutex); while (count == 0) { pthread_cond_wait(&control, &mutex); } i = data; count = 0; pthread_cond_signal(&control); pthread_mutex_unlock(&mutex); }

Counting Mutex Linux Extension PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; A Partial Semaphore Can be called recursively in a single thread Counts number of calls and unlocks once count is zero

Error Checking Mutex Linux Extension: PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; If called recursively, return an error! Very helpful for debugging deadlocks

Mutex Maintenance int pthread_mutex_init(&m, &flags) int pthread_mutex_trylock(&m); int pthread_mutex_destroy(&m);

Condition Maintenance PTHREAD_COND_INITIALIZER int pthread_cond_init(&c) int pthread_cond_timedwait(&c, &m, &t); int pthread_cond_destroy(&c)

Problems with Solutions Barrier –Wait until all have registered –Wait until at least one has registered –Insure that a function is called exactly once

Two Phase locking Get all the locks in a consistent order Do the work Release the locks in the inverse order Reduces Deadlocks