Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Similar documents
Chapter 5: Process Synchronization

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

CS370 Operating Systems

Process Synchronization

Chapter 5: Process Synchronization

Lesson 6: Process Synchronization

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Chapter 6: Process Synchronization

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

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Chapter 6: Process Synchronization

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Module 6: Process Synchronization

Chapter 5: Process Synchronization

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CSE 4/521 Introduction to Operating Systems

Chapter 6 Synchronization

Chapter 6: Process Synchronization

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

Synchronization Principles

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

CS370 Operating Systems

Chapter 6: Synchronization

Real-Time Operating Systems M. 5. Process Synchronization

Process Synchronization

Chapter 6 Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Chapter 7: Process Synchronization. Background

Process Synchronization

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization!

Process Synchronization

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

Introduction to Operating Systems

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Chapter 6: Process Synchronization

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

Process Co-ordination OPERATING SYSTEMS

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 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Lecture 3: Synchronization & Deadlocks

9/30/2014. CS341: Operating System High Level Construct: Monitor Deadlock Conditions Prevention, Avoidance Detection and Recovery

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Module 6: Process Synchronization

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

Fall 2015 COMP Operating Systems. Lab 06

Classic Problems of Synchronization

Lecture 5: Inter-process Communication and Synchronization

Operating Systems. Synchronization Based on Ch. 5 of OS Concepts by SGG

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

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

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

Semaphores (by Dijkstra)

High-level Synchronization

CSE Opera,ng System Principles

PESIT Bangalore South Campus

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Chapter 7 Process Synchronization

CS370 Operating Systems

CS420: Operating Systems. Process Synchronization

Concurrency: Mutual Exclusion and

Synchronization Principles II

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

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

Process Synchronization

Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Interprocess Communication By: Kaushik Vaghani

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CS370 Opera;ng Systems Midterm Review. Yashwant K Malaiya Spring 2018

$ %! 0,-./ + %/ 0"/ C (" &() + A &B' 7! .+ N!! O8K + 8 N. (Monitors) 3+!

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

Process Management And Synchronization

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

Process Synchronization(2)

Concurrency and Synchronisation

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Concurrency and Synchronisation

Processes-Process Concept:

Process Synchronization(2)

IV. Process Synchronisation

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS370 Operating Systems

Process Synchronization

Process Synchronization(2)

More on Synchronization and Deadlock

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Transcription:

Chapter 6: Process Synchronization Operating System Concepts 9 th Edit9on Silberschatz, Galvin and Gagne 2013

Objectives To present the concept of process synchronization. To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To present both software and hardware solutions of the critical-section problem To examine several classical process-synchronization problems To explore several tools that are used to solve process synchronization problems 2 Silberschatz, Galvin and Gagne 2013

Chapter 6: Process Synchronization 1. Background 2. The Critical-Section Problem 3. Peterson s Solution 4. Synchronization Hardware 5. Mutex Locks 6. Semaphores 7. Classic Problems of Synchronization 8. Monitors 9. Synchronization Examples 10. Alternative Approaches 3 Silberschatz, Galvin and Gagne 2013

6.8 MONITORS 4 Silberschatz, Galvin and Gagne 2013

Problems with Semaphores Incorrect use of semaphore operations: signal (mutex). wait (mutex) wait (mutex) wait (mutex) Omitting of wait (mutex) or signal (mutex) (or both) Deadlock and starvation are possible. 5 Silberschatz, Galvin and Gagne 2013

Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Abstract data type, internal variables only accessible by code within the procedure Only one process may be active within the monitor at a time But not powerful enough to model some synchronization schemes monitor monitor-name { // shared variable declarations procedure P1 ( ) {. procedure Pn ( ) { Initialization code ( ) { 6 Silberschatz, Galvin and Gagne 2013

Schematic view of a Monitor 7 Silberschatz, Galvin and Gagne 2013

Condition Variables condition x, y; Two operations are allowed on a condition variable: x.wait() a process that invokes the operation is suspended until x.signal() x.signal() resumes one of processes (if any) that invoked x.wait() If no x.wait() on the variable, then it has no effect on the variable 8 Silberschatz, Galvin and Gagne 2013

Monitor with Condition Variables 9 Silberschatz, Galvin and Gagne 2013

Condition Variables Choices If process P invokes x.signal(), and process Q is suspended in x.wait(), what should happen next? Both Q and P cannot execute in paralel. If Q is resumed, then P must wait Options include Signal and wait P waits until Q either leaves the monitor or it waits for another condition Signal and continue Q waits until P either leaves the monitor or it waits for another condition Both have pros and cons language implementer can decide Monitors implemented in Concurrent Pascal compromise P executing signal immediately leaves the monitor, Q is resumed Implemented in other languages including Mesa, C#, Java 10 Silberschatz, Galvin and Gagne 2013

6.8.2 Monitor Solution to Dining Philosophers monitor DiningPhilosophers { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i]!= EATING) self[i].wait; void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); 11 Silberschatz, Galvin and Gagne 2013

Solution to Dining Philosophers (Cont.) void test (int i) { if ((state[(i + 4) % 5]!= EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5]!= EATING) ) { state[i] = EATING ; self[i].signal () ; initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; 12 Silberschatz, Galvin and Gagne 2013

Solution to Dining Philosophers (Cont.) Each philosopher i invokes the operations pickup() and putdown() in the following sequence: DiningPhilosophers.pickup(i); EAT DiningPhilosophers.putdown(i); No deadlock, but starvation is possible 13 Silberschatz, Galvin and Gagne 2013

6.8.3 Monitor Implementation Using Semaphores Variables semaphore mutex; // (initially = 1) semaphore next; // (initially = 0) int next_count = 0; Each procedure F will be replaced by wait(mutex); body of F; if (next_count > 0) signal(next) else signal(mutex); Mutual exclusion within a monitor is ensured 14 Silberschatz, Galvin and Gagne 2013

Monitor Implementation Condition Variables For each condition variable x, we have: semaphore x_sem; // (initially = 0) int x_count = 0; The operation x.wait can be implemented as: x_count++; if (next_count > 0) signal(next); else signal(mutex); wait(x_sem); x_count--; 15 Silberschatz, Galvin and Gagne 2013

Monitor Implementation (Cont.) The operation x.signal can be implemented as: if (x_count > 0) { next_count++; signal(x_sem); wait(next); next_count--; 16 Silberschatz, Galvin and Gagne 2013

6.8.4 Resuming Processes within a Monitor If several processes queued on condition x, and x.signal() executed, which should be resumed? FCFS frequently not adequate conditional-wait construct of the form x.wait(c) Where c is priority number Process with lowest number (highest priority) is scheduled next 17 Silberschatz, Galvin and Gagne 2013

Single Resource allocation Allocate a single resource among competing processes using priority numbers that specify the maximum time a process plans to use the resource R.acquire(t);... access the resurce;... R.release; Where R is an instance of type ResourceAllocator 18 Silberschatz, Galvin and Gagne 2013

A Monitor to Allocate Single Resource monitor ResourceAllocator { boolean busy; condition x; void acquire(int time) { if (busy) x.wait(time); busy = TRUE; void release() { busy = FALSE; x.signal(); initialization code() { busy = FALSE; 19 Silberschatz, Galvin and Gagne 2013

6.9 SYNCHRONIZATION EXAMPLES 20 Silberschatz, Galvin and Gagne 2013

Windows Synchronization Uses interrupt masks to protect access to global resources on uniprocessor systems Uses spinlocks on multiprocessor systems Spinlocking-thread will never be preempted Also provides dispatcher objects user-land which may act mutexes, semaphores, events, and timers Events An event acts much like a condition variable Timers notify one or more thread when time expired Dispatcher objects either signaled-state (object available) or nonsignaled state (thread will block) 21 Silberschatz, Galvin and Gagne 2013

Linux Synchronization Linux: Prior to kernel Version 2.6, disables interrupts to implement short critical sections Version 2.6 and later, fully preemptive Linux provides: Semaphores atomic integers spinlocks reader-writer versions of both On single-cpu system, spinlocks replaced by enabling and disabling kernel preemption 22 Silberschatz, Galvin and Gagne 2013

Solaris Synchronization Implements a variety of locks to support multitasking, multithreading (including real-time threads), and multiprocessing Uses adaptive mutexes for efficiency when protecting data from short code segments Starts as a standard semaphore spin-lock If lock held, and by a thread running on another CPU, spins If lock held by non-run-state thread, block and sleep waiting for signal of lock being released Uses condition variables Uses readers-writers locks when longer sections of code need access to data Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock Turnstiles are per-lock-holding-thread, not per-object Priority-inheritance per-turnstile gives the running thread the highest of the priorities of the threads in its turnstile 23 Silberschatz, Galvin and Gagne 2013

Pthreads Synchronization Pthreads API is OS-independent It provides: mutex locks condition variable Non-portable extensions include: read-write locks spinlocks 24 Silberschatz, Galvin and Gagne 2013

6.10 ALTERNATIVE APPROACHES 25 Silberschatz, Galvin and Gagne 2013

6.10.1 Transactional Memory A memory transaction is a sequence of read-write operations to memory that are performed atomically. void update() { /* read/write memory */ 26 Silberschatz, Galvin and Gagne 2013

6.10.2 OpenMP OpenMP is a set of compiler directives and API that support parallel progamming. void update(int value) { #pragma omp critical { count += value The code contained within the #pragma omp critical directive is treated as a critical section and performed atomically. 27 Silberschatz, Galvin and Gagne 2013

6.10.3 Functional Programming Languages Functional programming languages offer a different paradigm than procedural languages in that they do not maintain state. Variables are treated as immutable and cannot change state once they have been assigned a value. There is increasing interest in functional languages such as Erlang and Scala for their approach in handling data races. 28 Silberschatz, Galvin and Gagne 2013

End of Chapter 6 Operating System Concepts 9 th Edit9on Silberschatz, Galvin and Gagne 2013