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

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

CS370 Operating Systems

Chapter 5: Process Synchronization

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

CS370 Operating Systems

Chapter 5: Process Synchronization

Chapter 7: Process Synchronization!

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Process Synchronization

Chapter 7: Process Synchronization. Background

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 6: Process Synchronization

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

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

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

Module 6: Process Synchronization

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

Chapter 6 Synchronization

Chapter 6: Process Synchronization

Process Synchronization

Process Synchronization

Process Co-ordination OPERATING SYSTEMS

Chapter 5: Process Synchronization

Module 6: Process Synchronization

Chapter 6: Synchronization

Introduction to Operating Systems

Lesson 6: Process Synchronization

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Synchronization Principles II

Chapter 6: Process Synchronization

High-level Synchronization

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

Process Synchronization

Synchronization Principles

Chapter 7 Process Synchronization

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

Semaphores (by Dijkstra)

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

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

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!

Process Synchronization(2)

Interprocess Communication By: Kaushik Vaghani

Real-Time Operating Systems M. 5. Process Synchronization

Process Synchronization(2)

CS370: Operating Systems [Spring 2017] Dept. Of Computer Science, Colorado State University

Prof. Hui Jiang Dept of Computer Science and Engineering York University

Chapter 6: Process Synchronization

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

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Process Synchronization(2)

Process Synchronization

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Process Synchronization

CSE 4/521 Introduction to Operating Systems

CSE Opera,ng System Principles

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

CS370 Operating Systems

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

Lecture 3: Synchronization & Deadlocks

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

Frequently asked questions from the previous class survey

PESIT Bangalore South Campus

Chapter 6 Process Synchronization

Process Coordination

CS370: Operating Systems [Spring 2016] Dept. Of Computer Science, Colorado State University

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

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

Process Management And Synchronization

Process Synchronization

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

Lecture 5: Inter-process Communication and Synchronization

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

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

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Process Synchronization

Process Synchronization. studykorner.org

Classic Problems of Synchronization

CS3502 OPERATING SYSTEMS

Frequently asked questions from the previous class survey

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization Classic Problems

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

CS370: Operating Systems [Spring 2016] Dept. Of Computer Science, Colorado State University

Unit 1: Introduction

Fall 2015 COMP Operating Systems. Lab 06

1. Motivation (Race Condition)

OS Process Synchronization!

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

Silberschatz and Galvin Chapter 6

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

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Transcription:

Frequently asked questions from the previous class survey CS 370: SYSTEM ARCHITECTURE & SOFTWARE [PROCESS SYNCHRONIZATION] Shrideep Pallickara Computer Science Colorado State University Semaphores From Ancient Greek σῆμα (sêma), sign, and φωρος (phoros), bearing, bearer Where are they implemented? How are the operations made atomic? Why use spinlocks? Critical sections Why use mutex instead of a simple boolean variable? Are these recognized by the OS? What happens if a process is context-switched in the middle of the critical section? Is bounded-wait a good thing? What does priority inversion do? Midterms: 64/80? L13.1 L13.2 Topics covered in the lecture Classical process synchronization problems Readers Writers Dining philosopher s problem THE READERS-WRITERS PROBLEM Instructor: SHRIDEEP PALLICKARA L13.3 L13.4 The Readers-Writers problem A database is shared among several concurrent processes Two types of processes Readers Writers Readers-Writers: Potential for adverse effects If two readers access shared data simultaneously? No problems If a writer and some other reader (or writer) access shared data simultaneously? Chaos L13.5 L13.6 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.1

Writers must have exclusive access to shared database while writing FIRST readers-writers problem: No reader should wait for other readers to finish; simply because a writer is waiting n Writers may starve SECOND readers-writers problem: If a writer is ready it performs its write ASAP n Readers may starve Solution to the FIRST readers-writers problem Variable int readcount Tracks how many readers are reading object Semaphore mutex {1} Ensure mutual exclusion when readcount is accessed Semaphore wrt {1} 1 Mutual exclusion for the writers 2 First (last) reader that enters (exits) critical section n Not used by readers, when other readers are in their critical section L13.7 L13.8 The Writer: When a writer signals either a waiting writer or the readers resume do { wait(wrt); writing is performed signal(wrt); When: writer in critical section and if n readers waiting 1 reader is queued on wrt (n-1) readers queued on mutex The Reader process if (readcount ==0) { signal(wrt); } } while (TRUE); signal(mutex); } while (TRUE); L13.9 do { wait(mutex); readcount++; if (readcount ==1) { wait(wrt); } signal(mutex); reading is performed wait(mutex); readcount--; mutex for mutual exclusion to readcount When: writer in critical section and if n readers waiting 1 is queued on wrt (n-1) queued on mutex L13.10 The situation THE DINING PHILOSOPHERS PROBLEM L13.11 L13.12 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.2

The Problem 1 Philosopher tries to pick up two closest {LR} chopsticks 2 Pick up only 1 chopstick at a time Cannot pick up a chopstick being used 3 Eat only when you have both chopsticks Why is the problem important? Represents allocation of several resources AMONG several processes Can this be done so that it is: Deadlock free Starvation free 4 When done; put down both the chopsticks L13.13 L13.14 Dining philosophers: Simple solution Each chopstick is a semaphore Grab by executing wait() Release by executing signal() Shared data semaphore chopstick[5]; All elements are initialized to 1 What if all philosophers get hungry and grab the same {L/R} chopstick? do { wait(chopstick[i]); wait(chopstick[(i+1)%5]); //eat signal(chopstick[i]); signal(chopstick[(i+1)%5]); //think } while (TRUE); Deadlock: If all processes access chopstick with same hand We will look at solution with monitors L13.15 L13.16 Overview of the semaphore solution Processes share a semaphore mutex Initialized to 1 Each process MUST execute wait before entering critical section signal after exiting critical section MONITORS L13.17 L13.18 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.3

Incorrect use of semaphores can lead to timing errors Incorrect use of semaphores: [1] Interchange order of wait and signal Hard to detect Reveal themselves only during specific execution sequences If correct sequence is not observed 2 processes may be in critical section simultaneously Problems even if only one process is not well behaved do { signal(mutex); critical section wait(mutex); remainder section } while (TRUE); Problem: Several processes simultaneously active in critical section NB: Not always reproducible L13.19 L13.20 Incorrect use of semaphores: [2] Replace signal with wait Incorrect use of semaphores: [3] What if you omit signal AND/OR wait? do { wait(mutex); critical section wait(mutex); remainder section } while (TRUE); Problem: Deadlock do { wait(mutex); critical section signal(mutex); remainder section } while (TRUE); Omission: Mutual exclusion violated Omission: Deadlock L13.21 L13.22 When programmers use semaphores incorrectly problems arise We need a higher-level synchronization construct Monitor Before we move ahead: Abstract Data Types Encapsulates private data with n Public methods to operate on them A monitor is an abstract data type Mutual exclusion provided within the monitor Contains: Declaration of variables n Defining the instance s state Functions that operate on these variables L13.23 L13.24 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.4

Monitor construct ensures that only one process at a time is active within monitor Programmer does not code synchronization constraint explicitly monitor monitor name { //shared variable declarations function F1(..) {...} function F2(..) {...} function Fn(..) {...} initialization code(..) {...} } shared data operations initialization code Entry Queue L13.25 L13.26 Basic monitor scheme not sufficiently powerful Provides an easy way to achieve mutual exclusion But we also need a way for processes to block when they cannot proceed This blocking capability is provided by the condition construct The condition construct condition x, y; Operations on a condition variable wait: e.g. x.wait() n Process invoking this is suspended UNTIL signal: e.g. x.signal() n Resumes exactly-one suspended process n If no process waiting; NO EFFECT on state of x L13.27 L13.28 Semantics of wait and signal x.signal() invoked by process P Q is the suspended process waiting on x Signal and wait: P waits for Q to leave monitor Signal and continue: Q waits till P leaves monitor PASCAL: When thread P calls signal P leaves immediately Q immediately resumed Difference between the signal() in semaphores and monitors Monitors {condition variables}: Not persistent If a signal is performed and no waiting threads? n Signal is simply ignored During subsequent wait operations n Thread blocks Semaphores Signal increments semaphore value even if there are no waiting threads n Future wait operations would immediately succeed L13.29 L13.30 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.5

Dining-Philosophers Using Monitors Deadlock-free enum {THINKING,HUNGRY,EATING} state[5]; state[i] = EATING only if state[(i+4)%5] = EATING && state[(i+1)%5] = EATING DINING PHILOSOPHERS USING MONITORS condition self[5] Delay self when HUNGRY but unable to get chopsticks L13.31 L13.32 Sequence of actions Before eating, must invoke pickup() May result in suspension of philosopher process After completion of operation, philosopher may eat DiningPhilosophers.pickup(i);... eat... DiningPhilosophers.putdown(i); The pickup() and putdown() operations pickup(int i) { state[i] = HUNGRY; test(i); if (state[i] = EATING) { Suspend self if unable self[i].wait(); to acquire chopstick } } putdown(int i) { state[i] = THINKING; test( (i+4)%5 ); test( (i+1)%5 ); } Check to see if person on left or right can use the chopstick L13.33 L13.34 test() to see if philosopher can eat Eat only if HUNGRY and Person on Left AND Right are not eating test(int i) { if (state[(i+4)%5] = EATING && state[i] == HUNGRY && state[(i+1)%5 = EATING] ) { state[i] = EATING; self[i].signal(); } } Signal a process that was suspended while trying to eat Possibility of starvation Philosopher i can starve if eating periods of philosophers on left and right overlap Possible solution Introduce new state: STARVING Chopsticks can be picked up if no neighbor is starving n Effectively wait for neighbor s neighbor to stop eating n REDUCES concurrency L13.35 L13.36 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.6

Implementing a monitor using semaphores For each monitor Semaphore mutex initialized to 1 IMPLEMENTING A MONITOR USING SEMAPHORES Process must execute wait(mutex) : Before entering the monitor signal(mutex): Before leaving the monitor L13.37 L13.38 Semantics of the signaling process Signaling process must wait until the resumed process leaves or waits Additional semaphore next is introduced So signaling process needs to suspend itself Semaphore next initialized to 0 n Signaling processes use next to suspend themselves Integer variable next_count n Counts number of processes suspended on next Implementing a function F in the monitor wait(mutex);... body of function F... if (next_count > 0) { signal(next); } else { signal(mutex); } L13.39 L13.40 Implementing condition variables: Resuming processes within a monitor x_count++; if (next_count > 0) { signal(next); } else { signal(mutex); } wait(x_sem); x_count--; x.wait() Operation if (x_count > 0) { next_count++; signal(x_sem); wait(next); next_count--; } x.signal() Operation {C1} Several processes suspended on condition x {C2} x.signal() executed by some process Which suspended process should be resumed next? Simple solution: FCFS ordering n Process waiting the longest is resumed first For each condition x we have: semaphore xsem and integer variable x_count Instructor: SHRIDEEP PALLICKARA Both initialized to 0 L13.41 Instructor: SHRIDEEP PALLICKARA L13.42 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.7

Process resumption: conditional wait Monitor to allocate a single resource Monitor ResourceAllocator { x.wait(c) boolean busy; condition x; c is an integer expression; evaluated when wait() is void acquire(int time) { executed if (busy) { x.wait(time); } Value of c is the priority number busy = TRUE; Stored with the name of process that is suspended } When x.signal() is executed void release() { Process with smallest priority number resumed next busy = FALSE; x.signal(); } initialization() {busy = FALSE;} } L13.43 L13.44 An example of conditional waits Specify maximum time resource will be used R.acquire(t); Monitor allocates resource... based on shortest duration access the resource;... R.release(); Avoiding time dependent errors and ensuring that scheduling algorithm is not defeated User processes must make their calls on the monitor in correct sequence Ensure that uncooperative processes do not ignore the mutual exclusion gateway Should not access resource directly Monitor cannot guarantee that the access sequence will be observed L13.45 L13.46 The contents of this slide set are based on the following references Avi Silberschatz, Peter Galvin, Greg Gagne. Operating Systems Concepts, 9 th edition. John Wiley & Sons, Inc. ISBN-13: 978-1118063330. [Chapter 5] Andrew S Tanenbaum. Modern Operating Systems. 4 th Edition, 2014. Prentice Hall. ISBN: 013359162X/ 978-0133591620. [Chapter 2] Instructor: SHRIDEEP PALLICKARA L13.47 SLIDES CREATED BY: SHRIDEEP PALLICKARA L13.8