EECE.4810/EECE.5730 Operating Systems

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
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,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Homework-6 Questions : 2,10,15,22.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
CS703 – Advanced Operating Systems
Chapter 5: Process Synchronization
Threads Threads.
Atomic Operations in Hardware
Multithreading Tutorial
Atomic Operations in Hardware
Chapter 5: Process Synchronization
143a discussion session week 3
EECE.4810/EECE.5730 Operating Systems
Designing Parallel Algorithms (Synchronization)
Multithreading Tutorial
Module 7a: Classic Synchronization
Jonathan Walpole Computer Science Portland State University
Critical section problem
Grades.
Chapter 6: Process Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
EECE.4810/EECE.5730 Operating Systems
Lecture 20: Synchronization
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
CPS110: Thread cooperation
Presentation transcript:

EECE.4810/EECE.5730 Operating Systems Instructor: Dr. Michael Geiger Spring 2019 Lecture 10: Synchronization

Operating Systems: Lecture 10 Lecture outline Announcements/reminders Program 2 to be posted; due date TBD Exam 1 date still TBD Respond to scheduling poll ASAP Today’s lecture Review: Pthread examples Synchronization Locks Condition variables Monitors 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Review: Threads Thread: active sequence of instructions Basic unit of CPU utilization Thread creation is lightweight Multiple threads in same process can share address space Each thread needs own PC, register copies, stack + SP Threads provide concurrency within application HW support necessary for parallelism Major issue: non-deterministic ordering Solutions require atomic operations Avoid race condition: solution depends on timing/ordering of earlier events 7/20/2019 Operating Systems: Lecture 10

Review: Pthread specification Synchronous threading: parent thread needs to terminate the child thread. Programming preparation: #include <pthread.h> int pthread_attr_init(pthread_attr_t *attr); Initializes the thread attributes object pointed to by attr with default attribute values. int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Starts a new thread in the calling process. int pthread_join(pthread_t thread, void **retval); Waits for the thread specified by thread to terminate. Compile: gcc -lpthread 7/20/2019 Operating Systems: Lecture 10

Review: Pthread data types pthread_t: Thread ID pthread_attr_t: Structure specifying Scope: does thread compete for resources vs. all threads on system, or only own process? Linux only supports system scope Detach state: will parent thread wait for thread to join, or is new thread independent? Stack address/size: process can define, or let system handle it Scheduling policy/priority: can be inherited from parent or set separately 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Synchronization Constrain interleavings between threads Goal: force all possible interleavings to produce correct result Correct concurrent program should work regardless of processor speed Try to constrain as little as possible Some events are independent—order irrelevant Order only matters in dependent events Synchronization: Controlling execution and order of threads 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Synchronization do{ curr_reqt = recv_reqt(); reqt_queue[ptr] = curr_reqt; ptr++; }while(true) do{ curr_reqt = reqt_queue[ptr]; ptr--; process(curr_reqt); }while(true) Consumer Producer load mem[ptr], reg; add reg, 1; store reg, mem[ptr]; load mem[ptr], reg; add reg, -1; store reg, mem[ptr]; If instructions are interleaved, we have a problem … 7/20/2019 Operating Systems: Lecture 10

“Too much milk” problem Problem definition Janet & Peter want to keep refrigerator stocked with at most one milk jug If either sees fridge empty, she/he buys milk Simple solution (no synchronization) Peter Janet if (!hasMilk) { if (!hasMilk) { buy milk buy milk hasMilk = true; hasMilk = true; } } 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Mutual exclusion Ensure that only 1 thread is doing a certain thing at one time (other threads excluded) Example: only 1 person shops at a time Constrains thread interleaving: can’t operate at the same time 7/20/2019 Operating Systems: Lecture 10

Atomic operation solution Atomically do the following operations: Load the value of hasMilk from memory to register. Compare hasMilk (reg) with false. If hasMilk (reg) is false, swap it to true. Write hasMilk (reg) value back to memory. return original hasMilk (reg) to while condition Peter/Janet while (Atomic-Compare-and-Swap(hasMilk,false,true) == false) { buy milk; } 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Critical section Code section that needs to be run atomically with respect to selected other pieces of code Independent threads can still interrupt If A and B are critical sections with respect to each other, multiple threads can’t interleave events from A and B A and B mutually exclude each other A and B often same piece of code Critical sections must be atomic with respect to each other because they access shared resource In “too much milk”, critical section is if (!hasMilk) buy milk 7/20/2019 Operating Systems: Lecture 10

Critical section solution Code section that only can be run by one thread! Peter if (hasMilk == false) { //Critical section buy milk; hasMilk = true; //Exit critical section } Janet if (hasMilk == false) { //Critical section buy milk; hasMilk = true; //Exit critical section } 7/20/2019 Operating Systems: Lecture 10

Critical section requirements Mutual exclusion: ≤1 thread executes CS at a time Progress: if >1 thread attempts CS at same time, 1 thread guaranteed to be selected Bounded waiting: if thread T requests access to its CS, limit on # times other threads can access their CS before T does 7/20/2019 Operating Systems: Lecture 10

Critical section example Dekker’s algorithm: 1st known CS solution (pre-synch primitives) for two threads Shared variables: boolean flag[2]; // Both initially false int turn; // Initialized to 0 or 1 Process Pi (i == 0 or 1) shown in handout/next slide; other process is Pj (j == 1 or 0) Prove algorithm satisfies CS properties Mutual exclusion (only 1 thread in CS at time) Progress (1 thread guaranteed to access CS if >1 req) Bounded waiting (limit on time thread will wait for CS) 7/20/2019 Operating Systems: Lecture 10

Dekker’s algorithm code for Pi do { flag[i] = true; while (flag[j] == true) { if (turn == j) { flag[i] = false; while (turn == j) ; // do nothing—busy waiting } /* critical section would be placed here */ turn = j; } while (true); 7/20/2019 Operating Systems: Lecture 10

Critical section solution Solution satisfies the conditions as follows: Mutual exclusion: If both processes set their flag variables to true, only process whose turn it is will move forward Progress: turn chooses the process to move forward if both attempt to enter the critical section simultaneously Bounded waiting: Since one process sets turn to value of other process, waiting process will be allowed to enter its critical section next 7/20/2019 Operating Systems: Lecture 10

Higher-level synchronization Higher-level abstraction  easier for programmers Applications Concurrent programs Operating System High-level synchronization primitives (mutex, semaphore, condition variable, monitor) Hardware Atomic operation support (load/store, interrupt enable/disable, test/set) 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Locks A lock (or mutex) prevents another thread from entering a critical section “Lock fridge while checking milk & shopping” Two operations: lock(): wait until lock is free, then acquire it do { if (lock is free) { // code in red acquire lock // is atomic break out of loop } } while (1); unlock(): release lock 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Lock usage Lock initialized to free Thread acquires lock before entering crit. section Waits if needed Thread that acquired lock releases when done with critical section Locks make earlier problem trivial Peter milk.lock() if (noMilk) { buy milk } milk.unlock() Janet milk.lock() if (noMilk) { buy milk } milk.unlock() 7/20/2019 Operating Systems: Lecture 10

Case study: thread-safe shared queue 7/20/2019 Operating Systems: Lecture 10

Shared queue (continued) Potential problems with this approach? What happens if >1 thread accesses queue at once? 7/20/2019 Operating Systems: Lecture 10

Shared queue with locks 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Invariants When can enqueue() unlock? Must restore queue to stable state Stable state is called invariant Condition that is “always” true for queue For example, each node appears exactly once when traversing list from head to tail Hold lock when you’re manipulating shared data (and therefore breaking invariant) 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Fine-grained locking One lock per queue  only one thread can access queue at once One lock per node: fine-grained locking What’s the major benefit? Lock each node as queue is traversed, release once it’s safe, allowing other threads to traverse queue 7/20/2019 Operating Systems: Lecture 10

Fine-grained locking example lock A get pointer to B unlock A lock B read B unlock B What’s potential downside of this approach? 7/20/2019 Operating Systems: Lecture 10

Hand-over-hand locking lock A get pointer to B lock B unlock A read B unlock B Lock next node before releasing last node 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Final notes Next time Continue synchronization discussion Reminders: Program 2 to be posted; due date TBD Exam 1 date still TBD Respond to scheduling poll ASAP 7/20/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Acknowledgements These slides are adapted from the following sources: Silberschatz, Galvin, & Gagne, Operating Systems Concepts, 9th edition Chen & Madhyastha, EECS 482 lecture notes, University of Michigan, Fall 2016 Dr. Hang Liu 7/20/2019 Operating Systems: Lecture 10