Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Similar documents
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

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

Classic Problems of Synchronization

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Process Synchronization

CS370 Operating Systems

Process Synchronization

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

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

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

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

Synchronization Principles

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

Chapter 7: Process Synchronization!

Lesson 6: Process Synchronization

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

CS370 Operating Systems

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Process Synchronization

CHAPTER 6: PROCESS SYNCHRONIZATION

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

CSC501 Operating Systems Principles. Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Interprocess Communication By: Kaushik Vaghani

Chapters 5 and 6 Concurrency

Process Synchronization

Lecture 6. Process Synchronization

CS3502 OPERATING SYSTEMS

Dept. of CSE, York Univ. 1

Process Management And Synchronization

Process Synchronization

CS370 Operating Systems

Chapter 5: Process Synchronization

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

Dealing with Issues for Interprocess Communication

Chapter 7: Process Synchronization. Background. Illustration

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

Concurrency: mutual exclusion and synchronization

PROCESS SYNCHRONIZATION

Concurrency pros and cons. Concurrent Programming Problems. Linked list example. Linked list example. Mutual Exclusion. Concurrency is good for users

Process Synchronization

Concurrency. Chapter 5

Process Coordination

Chapter 6: Process Synchronization

Process Synchronization

Process Synchronization

Chapter 7: Process Synchronization. Background

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Midterm Exam. October 20th, Thursday NSC

Process Synchronization

Concept of a process

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

Chapter 5: Process Synchronization

Process Synchronization. studykorner.org

IV. Process Synchronisation

Synchronization Principles II

Semaphores. A semaphore is an object that consists of a. methods (e.g., functions): signal and wait. semaphore. method signal. counter.

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

CSE 4/521 Introduction to Operating Systems

Introduction to Operating Systems

Concurrency: Deadlock and Starvation. Chapter 6

PESIT Bangalore South Campus

Achieving Synchronization or How to Build a Semaphore

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

CSE Traditional Operating Systems deal with typical system software designed to be:

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

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Concurrency: Mutual Exclusion and Synchronization. Concurrency

Lecture 3: Intro to Concurrent Processing using Semaphores

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

CS420: Operating Systems. Process Synchronization

Synchronization Basic Problem:

CS370 Operating Systems

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Synchronization I. Jo, Heeseung

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6 Synchronization

Semaphores (by Dijkstra)

Process Synchronization

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Module 6: Process Synchronization

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Process Synchronization Mechanisms

General Objectives: To understand the process management in operating system. Specific Objectives: At the end of the unit you should be able to:

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Chapter 5: Process Synchronization

OS Process Synchronization!

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

Silberschatz and Galvin Chapter 6

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

semsignal (s) & semwait (s):

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

CS3733: Operating Systems

CSE Opera,ng System Principles

Transcription:

Lecture Topics Today: Concurrency (Stallings, chapter 5.1-5.4, 5.7) Next: Exam #1 1 Announcements Self-Study Exercise #5 Project #3 (due 9/28) Project #4 (due 10/12) 2

Exam #1 Tuesday, 10/3 during lecture 80 minutes, 18% of course grade Topics: computer systems overview operating system overview processes and threads concurrency Study suggestions on course website 3 Problems with Concurrent Execution Concurrent tasks (processes or threads) often need to share data in memory or files Actions performed by concurrent tasks depend on the order in which their execution is interleaved, which typically is not deterministic Must control access to shared data (otherwise data may become corrupted) 4

Review: Critical Sections When a process executes code that manipulates shared data, we say that the process is in a critical section (CS) for that shared data (aka critical region) The execution of critical sections must be mutually exclusive: at any time, only one process is allowed to execute in its critical section (even with multiple CPUs) 5 Review: Critical Section Problem The critical section problem: design a protocol for tasks so that their actions will not depend on the order in which their execution is interleaved (possibly on many processors) Each task requests permission to enter its critical section (CS): entry section Each task signals that it has left its critical section (CS): exit section 6

Framework for Analysis of Solutions Generalized structure of every process: preceding section entry section critical section exit section following section 7 Framework (cont) More than one CPU may be present Hardware "serializes" accesses to memory Each process operates at non-zero speed, but no other assumptions about speed No assumptions about the order of interleaved execution 8

Valid Solution: Classic Requirements 1. Mutual Exclusion: at any moment, at most one process can be in its critical section (CS) 2. Progress: if no process is executing in its CS, a process that requests entry should be allowed to enter its CS immediately 3. Bounded Waiting: upper bound on how long any process is forced to wait for entry to CS 9 Valid Solution: Stallings 1. Mutual Exclusion must be enforced: at any moment, at most one process can be in its CS 2. A process which halts outside its CS must not interfere with other processes 3. No indefinite delay: deadlock and starvation cannot occur 10

Valid Solution: Stallings (cont) 4. When no process is executing in its CS, a process that requests entry should be allowed to enter its CS immediately 5. No assumptions about number (or relative speed) of processors 6. A process remains in its CS for a finite amount of time 11 Software Solutions Code for entry section and exit section uses loops and shared variables, does not depend on special hardware instructions or OS support Example: Peterson's Algorithm (1981) Drawback: uses busy waiting 12

Hardware Solutions Code for entry section and exit section uses special hardware instructions which execute atomically Examples: SPARC SWAP, Intel XCHG Drawbacks: uses busy waiting, more complex entry section and exit section needed to ensure bounded waiting 13 Summary: Software solutions to Critical Section Problem using normal instructions are possible, but use busy waiting and are somewhat complex when extended to more than two processes Hardware solutions using special "atomic" instructions are possible, but use busy waiting and are somewhat complex when extended to ensure bounded waiting 14

Review: Semaphores Synchronization mechanism that blocks (and unblocks) processes to avoid busy waiting Three basic operations on semaphore S: init( S, value ) wait( S ) block process, if necessary signal( S ) unblock process, if any waiting Operations must be executed without interruption (semaphore is shared resource) 15 Semaphore Operations (Atomic) Semaphore implemented as a record with two fields: count -- integer queue -- list of blocked processes struct semaphore { }; int count; queuetype queue; 16

void wait( semaphore s ) { s.count--; if (s.count < 0) // put process in s.queue, block it }; void signal( semaphore s ) { s.count++; if (s.count <= 0) // get some process from s.queue }; 17 Semaphores for Critical Sections Initialize S to 1 preceding section wait(s) critical section signal(s) following section 18

Semaphores for Synchronization We have two processes: P1 and P2 Statement S1 in P1 needs to be performed before statement S2 in P2 Initialize semaphore Synch to 0 P1: S1 P2: wait(synch) signal(synch) S2 19 The Producer/Consumer Problem A producer process produces information that is consumed by a consumer process Example: a program produces characters that are consumed by a printer We need a buffer to hold items that are produced and eventually consumed A common paradigm for cooperating processes 20

Bounded Buffer (circular buffer of size k) Finite number of slots in the buffer (circular) Producer(s) and consumer(s) update the buffer (since in and out pointers are modified) Can consume only when there is at least one item (number N of items is at least one) Can produce only when there is at least one empty slot (number E of empty spaces is at least one) 21 Bounded Buffer (circular buffer of size k) 22

General Pattern Producer: produce item buffer[in] = item in = (in+1) mod K Consumer: item = buffer[out] out = (out+1) mod K consume item 23 Solution using Semaphores Semaphore S to enforce mutual exclusion when accessing the buffer Semaphore N to synchronize producer and consumer (number of consumable items) Semaphore E to synchronize producer and consumer (number of empty slots) 24

Solution using Semaphores Initialization: in = 0; out = 0; // Next location to put item // Next location to get item S.count = 1; N.count = 0; E.count = K; // CS is free initially // 0 slots in use // K slots empty 25 Solution using Semaphores Producer: produce item wait(e) wait(s) buffer[in] = item in = (in+1) mod K signal(s) signal(n) Consumer: wait(n) wait(s) item = buffer[out] out = (out+1) mod K signal(s) signal(e) consume item 26

Problems with Semaphores Semaphores: powerful tool for enforcing mutual exclusion and coordinating processes Uses of wait(s) and signal(s) are scattered among several processes: difficult to understand their effects Usage must be correct in all the processes: one bad (or malicious) process can cause the entire collection of processes to fail 27 Readers/Writers Problem A data area is shared among processes: some processes only read the data area (readers) and some only write to the data area (writers) Conditions that must be satisfied: any number of readers may simultaneously read from the data only one writer at a time may write to the data area if a writer is writing to the data area, no reader may read from the data area 28

Readers/Writers Problem Two possible strategies: readers have priority over writers writers have priority over readers 29 R/W: readers have priority Semaphore wsem used to enforce mutual exclusion on the critical sections in both readers and writers Integer rc used to track the number of readers who are currently reading the data Updating rc is therefore a critical section; semaphore x used to enforce mutual exclusion on the critical sections in readers 30

Reader: wait(x) rc++ if (rc==1) wait(wsem) signal(x) READUNIT() wait(x) rc-- if (rc==0) signal(wsem) signal(x) Writer: wait(wsem) WRITEUNIT() signal(wsem) 31 R/W: writers have priority More complex to give writers priority Integers readcount and writecount Semaphores wsem, rsem, x, y and z 32

33 The Dining Philosophers Problem Five philosophers who only eat and think Each needs to use two forks to eat There are only 5 forks Illustrates the difficulty of allocating resources among process without deadlock and starvation 34

The Dining Philosophers Problem One process for each philosopher Array of semaphores (one per fork); each initialized to 1 Problem? Starvation if each philosopher picks up left fork Process Pi: think wait(fork[i]) wait(fork[i+1 mod 5]) eat signal(fork[i+1 mod 5]) signal(fork[i]) 35 The Dining Philosophers Problem One solution: only allow four philosophers to sit at the table Guarantees that one will be able to eat (even if other three are holding left forks) Use semaphore T to control access to table (initialize to 4) Process Pi: think wait(t) wait(fork[i]) wait(fork[i+1 mod 5]) eat signal(fork[i+1 mod 5]) signal(fork[i]) signal(t) 36