Roadmap. Shared Variables: count=0, buffer[] Producer: Background. Consumer: while (1) { Race Condition. Race Condition.

Similar documents
Process Synchronization - I

Principles of Operating Systems CS 446/646

Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Process Synchronization

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Dept. of CSE, York Univ. 1

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

CS370 Operating Systems

CS420: Operating Systems. Process Synchronization

Chapter 7: Process Synchronization!

CS370 Operating Systems

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Virtual Memory - I. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - XV. University at Buffalo.

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - XI Deadlocks - II. University at Buffalo. Deadlocks. October 6 th, 2011

Chapter 6: Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Virtual Memory - I. Roadmap. Background. Demand Paging. valid-invalid bit. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

IV. Process Synchronisation

Mutual Exclusion and Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - X Deadlocks - II. Louisiana State University. Deadlocks

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Process Synchronization (Part I)

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Process Synchronization

Process Synchronization

Roadmap. Deadlock Prevention. Deadlock Prevention (Cont.) Deadlock Detection. Exercise. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Chapter 7: Process Synchronization. Background

CSE 421/521 - Operating Systems Fall Lecture - XII Main Memory Management. Tevfik Koşar. University at Buffalo. October 18 th, 2012.

Synchronization for Concurrent Tasks

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Concurrent Processes Rab Nawaz Jadoon

Process Synchronization

! The Process Control Block (PCB) " is included in the context,

Distributed Systems - II

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - XIV Virtual Memory - I. Louisiana State University.

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Lesson 6: Process Synchronization

Chapter 6: Process Synchronization

OS Design Approaches. Roadmap. OS Design Approaches. Tevfik Koşar. Operating System Design and Implementation

Synchronization Principles I

PROCESS SYNCHRONIZATION

Introduction to Operating Systems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - VII CPU Scheduling - II. Louisiana State University

Roadmap. Multilevel Queue Scheduling. Multilevel Queue. Example of Multilevel Feedback Queue. Multilevel Feedback Queue. Tevfik Ko!

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

CSC Operating Systems Spring Lecture - XIV Virtual Memory - II. Tevfik Ko!ar. Louisiana State University. March 27 th, 2008.

Dealing with Issues for Interprocess Communication

Module 6: Process Synchronization

CSE 421/521 - Operating Systems Fall Lecture - VII CPU Scheduling - II. University at Buffalo

CHAPTER 6: PROCESS SYNCHRONIZATION

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - III Processes. Louisiana State University. Virtual Machines Processes

CS370 Operating Systems Midterm Review

Synchronization Principles

Virtual Memory - II. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - XVI. University at Buffalo.

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Chapter 6: Process Synchronization

Process Coordination

Chapter 5: Synchronization 1

OS Design Approaches. Roadmap. System Calls. Tevfik Koşar. Operating System Design and Implementation. CSE 421/521 - Operating Systems Fall 2013

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - II OS Structures. University at Buffalo. OS Design and Implementation

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Chapter 6: Process Synchronization

Chapter 5: Process Synchronization

OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL. Chap. 2.2 Interprocess Communication

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

Chapter 6: Process Synchronization

Chapter 5 Asynchronous Concurrent Execution

Announcements. Computer System Organization. Roadmap. Major OS Components. Processes. Tevfik Ko!ar. CSC Operating Systems Fall 2009

CSC Operating Systems Fall Lecture - II OS Structures. Tevfik Ko!ar. Louisiana State University. August 27 th, 2009.

CS370 Operating Systems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Distributed Systems - III

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Process Synchronization

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CSE 4/521 Introduction to Operating Systems

Concurrency. Chapter 5

Chapter 5: Process Synchronization

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Part II Process Management Chapter 6: Process Synchronization

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Transcription:

CSE / - Operating Systems Fall 0 Lecture - VIII Process Synchronization - I Tevfik Koşar Roadmap Process Synchronization s Critical-Section Problem Solutions to Critical Section Different Implementations Semaphores Classic Problems of Synchronization University at uffalo September nd, 0 ackground Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Consider consumer-producer problem: Initially, count is set to 0 It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer. Shared Variables: count=0, buffer[] Producer: while (true) /* produce an item and put in nextproduced Consumer: while () while (count == UFFER_SIZE) ; // do nothing buffer [in] = nextproduced; in = (in + ) % UFFER_SIZE; count++; while (count == 0) ; // do nothing nextconsumed = buffer[out]; out = (out + ) % UFFER_SIZE; count--; /* consume the item in nextconsumed Race condition: The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized. Ensure that only one process at a time is manipulating the variable counter. The statements count++; count--; must be performed atomically. tomic operation means an operation without interruption. count++ could be implemented as register = count register = register + count = register count-- could be implemented as register = count register = register - count = register Consider this execution interleaving with count = initially: S0: producer execute register = count register = S: producer execute register = register + register = S: consumer execute register = count register = S: consumer execute register = register - register = S: producer execute count = register count = S: consumer execute count = register count =

//shared do lucky! //shared do //shared do unlucky # //shared do ee... Multithreaded echo (lucky) Multithreaded echo (unlucky) 7 8 do unlucky # do eh... Multithreaded echo (unlucky) 9 $ in this case, replacing the global variables with local variables did not solve the problem $ we actually had two race conditions here: % one race condition in the shared variables and the order of value assignment % another race condition in the shared output stream: which thread is going to write to output first (this race persisted even after making the variables local to each thread) ==> generally, problematic race conditions may occur whenever resources and/or data are shared (by processes unaware of each other or processes indirectly aware of each other) 0 Critical Section/Region Critical section/region: segment of code in which the process may be changing shared data (eg. common variables) No two processes should be executing in their critical sections at the same time --> prevents race conditions Critical section problem: design a protocol that the processes use to cooperate Critical Section " The indivisible execution blocks are s $ a is a section of code that may be executed by only one process or thread at a time common $ although it is not necessarily the same region of memory or section of program in both processes s ==> but physically different or not, what matters is that these regions cannot be interleaved or executed in parallel (pseudo or real) s

Solution to Critical-Section Problem solution to the critical-section problem must satisfy the following requirements:. - If process P i is executing in its critical section, then no other processes can be executing in their critical sections. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely Solution to Critical-Section Problem. ounded Waiting - bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted! ssume that each process executes at a nonzero speed! No assumption concerning relative speed of the N processes Critical Section " We need mutual exclusion from s $ s can be protected from concurrent access by padding them with entrance and exit gates (we ll see how later): a thread must try to check in, then it must check out do enter? exit do enter? exit HOW is this achieved??. thread reaches the gate to the (CR) before. thread enters CR first, preventing from entering ( is waiting or is blocked). thread exits CR; thread can now enter. thread enters CR " Desired effect: mutual exclusion from the " Implementation disabling hardware interrupts. thread reaches the gate to the (CR) before. as soon as enters CR, it disables all interrupts, thus cannot be scheduled. as soon as exits CR, it enables interrupts; can be scheduled again. thread enters CR 7 " Implementation disabling hardware interrupts & $ it works, but not reasonable! $ what guarantees that the user process is going to ever exit the? $ meanwhile, the cannot interleave any other task, even unrelated to this race condition $ the becomes one physically indivisible block, not logically $ also, this is not working in multiprocessors do disable hardware interrupts enable hardware interrupts 8

" Implementation simple lock variable. thread reaches CR and finds a lock at 0, which means that can enter. thread sets the lock to and enters CR, which prevents from entering. thread exits CR and resets lock to 0; thread can now enter. thread sets the lock to and enters CR " Implementation simple lock variable $ the lock is a shared variable $ entering the means testing and then setting the lock $ exiting means resetting the lock while (lock); /* do nothing: loop */ lock = TRUE; lock = FLSE; bool lock = FLSE; do test lock, then set lock reset lock 9 0 " Implementation simple lock variable &. thread reaches CR and finds a lock at 0, which means that can enter. but before can set the lock to, reaches CR and finds the lock is 0, too. sets the lock to and enters CR but cannot prevent the fact that....... is going to set the lock to and enter CR, too " Implementation simple lock variable & $ suffers from the very flaw we want to avoid: a race condition $ the problem comes from the small gap between testing that the lock is off and setting the lock while (lock); lock = TRUE; $ it may happen that the other thread gets scheduled exactly in between these two actions (falls in the gap) $ so they both find the lock off and then they both set it and enter bool lock = FLSE; do test lock, then set lock reset lock " Implementation indivisible lock variable '. thread reaches CR and finds the lock at 0 and sets it in one shot, then enters. even if comes right behind, it will find that the lock is already at. thread exits CR, then resets lock to 0. thread finds the lock at 0 and sets it to in one shot, just before entering CR " Implementation indivisible lock variable ' TSL Tanenbaum,. S. (00) Modern Operating Systems (nd Edition). $ the indivisibility of the test-lockand-set-lock operation can be implemented with the hardware instruction TSL do test-and-set-lock set lock off

" Implementation indivisible lock one key '. thread reaches CR and finds a key and takes it. even if comes right behind, it will not find a key. thread exits CR and puts the key back in place. thread finds the key and takes it, just before entering CR " Implementation indivisible lock one key ' $ holding a unique object, like a key, is an equivalent metaphor for test-and-set $ this is similar to the speaker s baton in some assemblies: only one person can hold it at a time $ holding is an indivisible action: you see it and grab it in one shot $ after you are done, you release the object, so another process can hold on to it do take key and run return key Summary cknowledgements Process Synchronization s Critical-Section Problem Solutions to Critical Section Different Implementations Hmm.. Operating Systems Concepts book and supplementary material by. Silberschatz, P. Galvin and G. Gagne Operating Systems: Internals and Design Principles book and supplementary material by W. Stallings Modern Operating Systems book and supplementary material by. Tanenbaum Next Lecture: Synchronization - II Reading ssignment: Chapter from Silberschatz. R. Doursat and M. Yuksel from UNR 7 8