OS Process Synchronization!

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

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

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

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,

Process Synchronization

Interprocess Communication By: Kaushik Vaghani

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

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

Process Coordination

CS370 Operating Systems

CS3733: Operating Systems

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

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

CSE 4/521 Introduction to Operating Systems

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

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

Chapter 7: Process Synchronization. Background

Process Synchronization. studykorner.org

Process Management And Synchronization

Chapter 7: Process Synchronization!

Concept of a process

Process Synchronization(2)

Process Synchronization

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

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

Module 6: Process Synchronization

Lesson 6: Process Synchronization

Process Synchronization

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Process Co-ordination OPERATING SYSTEMS

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

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

Module 6: Process Synchronization

Process Synchronization(2)

1. Motivation (Race Condition)

Process Synchronization

Synchronization Basic Problem:

Process Synchronization(2)

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Synchronization Principles

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

CS3502 OPERATING SYSTEMS

Introduction to Operating Systems

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CS370 Operating Systems

Chapter 5: Process Synchronization

Process Synchronization

Chapter 5: Process Synchronization

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

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

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

Chapter 6 Synchronization

CS370 Operating Systems

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

High-level Synchronization

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Dept. of CSE, York Univ. 1

Chapter 6: Process Synchronization

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Chapter 6: Process Synchronization

Chapter 2 Processes and Threads

Chapter 6: Process Synchronization

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

Process Synchronization

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Semaphores (by Dijkstra)

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Process Synchronization

Silberschatz and Galvin Chapter 6

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

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

CSE Opera,ng System Principles

Dealing with Issues for Interprocess Communication

Midterm Exam. October 20th, Thursday NSC

Classic Problems of Synchronization

Chapter 5: Process Synchronization

Concurrency. Chapter 5

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

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

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Synchronization Classic Problems

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

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

Process/Thread Synchronization

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

Synchronization COMPSCI 386

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

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

UNIT II PROCESS MANAGEMENT 9

CS370 Operating Systems

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

Synchronization Principles II

Transcription:

OS Process Synchronization! Race Conditions! The Critical Section Problem! Synchronization Hardware! Semaphores! Classical Problems of Synchronization! Synchronization HW Assignment! 3.1!

Concurrent Access to Shared Data! Suppose that two processes A and B have access to a shared variable Balance. PROCESS A: PROCESS B: Balance = Balance - 100 Balance = Balance - 200 Further, assume that Process A and Process B are executing concurrently in a time-shared, multiprogrammed system. 3.2!

Concurrent Access to Shared Data! The statement Balance = Balance 100 is implemented by several machine level instructions such as: A1. LOAD R1, BALANCE // load Balance from memory into Register 1 (R1) A2. SUB R1, 100 // Subtract 100 from R1 A3. STORE BALANCE, R1 // Store R1 s contents back to the memory location of Balance. Similarly, Balance = Balance 200 can be implemented by the following:! B1. LOAD R1, BALANCE B2. SUB R1, 200 B3. STORE BALANCE, R1 3.3!

Race Conditions! Observe: In a time-shared system the exact instruction execution order cannot be predicted!! Scenario 1:! A1. LOAD R1, BALANCE A2. SUB R1, 100 A3. STORE BALANCE, R1 Context Switch! B1. LOAD R1, BALANCE B2. SUB R1, 200 B3. STORE BALANCE, R1 Balance is effectively decreased by 300! Scenario 2:! A1. LOAD R1, BALANCE A2. SUB R1, 100 Context Switch! B1. LOAD R1, BALANCE B2. SUB R1, 200 B3. STORE BALANCE, R1 Context Switch! A3. STORE BALANCE, R1 Balance is effectively decreased by 100! 3.4!

Race Conditions! Situations like this, where multiple processes are writing or reading some shared data and the final result depends on who runs precisely when, are called race conditions. A serious problem for any concurrent system using shared data basically ALL operating systems and most user applications We must make sure that some high-level code sections are executed atomically (without interruption)! Atomic operation means that it completes in its entirety without interruption by any other potentially conflictcausing process. 3.5!

The Critical-Section Problem! n processes all competing to use some shared data! Each process has a code segment, called critical section (critical region), in which the shared data is accessed.! Problem ensure that when one process is executing in its critical section, no other process is allowed to execute in that critical section.! The execution of the critical sections by the processes must be mutually exclusive in time.! Lack of understanding of parallelism and concurrent access leads to both omission bugs and oversynchronization bugs! 3.6!

Mutual Exclusion! 3.7!

Solving Critical-Section Problem! Any solution to the problem must satisfy four conditions.! Mutual Exclusion:! No two processes may be simultaneously inside the same critical section.! Bounded Waiting:! Progress:!!No process should have to wait forever to enter a critical section.! No process executing a code segment unrelated!! to a given critical section can block another!!!process trying to enter the same critical section.! Arbitrary Speed:! No assumption can be made about the relative speed!of different processes (though all processes have a!non-zero speed) many programmers ignore this!! 3.8!

General Structure of a Typical Process!!!do {!.!!!!entry section"!!!!critical section!!!!exit section!!!!!remainder section!!!} while (1);! We will assume this structure when evaluating possible solutions to Critical Section Problem.! In the entry section, the process requests permission.! We consider single-processor systems now, multiprocessor systems later! 3.9!

Getting Help from the Hardware! One solution supported by hardware may be to use interrupt capability.! do {! DISABLE INTERRUPTS!! critical section;" ENABLE INTERRUPTS!! remainder section"!!} while (1);! Are we done? is this a good solution?! 3.10!

Synchronization Hardware! Many machines provide special hardware instructions that help to achieve mutual exclusion! The TestAndSet (TAS) instruction tests and modifies the content of a memory word atomically TAS R1,LOCK reads the contents of the memory word LOCK into register R1, and stores a nonzero value (e.g. 1) at the memory word LOCK (again, atomically!)! Assume LOCK = 0" calling TAS R1, LOCK will set R1 to 0, and set LOCK to 1.! Assume LOCK = 1! calling TAS R1, LOCK will set R1 to 1, and set LOCK to 1." 3.11!

Mutual Exclusion with Test-and-Set! Initially, shared memory word LOCK = 0. Process P i!!do {! entry_section:! TAS R1, LOCK! CMP R1, #0 /* was LOCK 0? */! JNE entry_section /* if not equal, jump to entry */!!!!critical section"! MOVE LOCK, #0 /* exit section */!!!remainder section"!!} while(1);! 3.12!

Busy Waiting! This approach is based on busy waiting: if the critical section is being used, waiting processes loop continuously at the entry point.! Disadvantages?! 3.13!

Semaphores! Introduced by E.W. Dijkstra Motivation: Avoid busy waiting by blocking a process execution until some condition is satisfied. Two operations are defined on a semaphore variable s: wait(s) (also called P(s) or down(s)) signal(s) (also called V(s) or up(s)) We will assume that these are the only user-visible operations on a semaphore. 3.14!

Semaphore Operations! Conceptually a semaphore has an integer value. This value is greater than or equal to 0. wait(s):: wait/block until s.value > 0; s.value-- ; /* Executed atomically! */ A process executing the wait operation on a semaphore with value 0 is blocked until the semaphore s value becomes greater than 0. No busy waiting signal(s):: s.value++; /* Executed atomically! */ 3.15!

Semaphore Operations (cont.)! If multiple processes are blocked on the same semaphore s, only one of them will be awakened when another process performs signal(s) operation.! Who will have priority?! Carefully study Section 6.5.2 of the textbook to better understand how the semaphores are implemented at the kernel level " 3.16!

Attacking Critical Section Problem with Semaphores! Shared data:!! semaphore mutex; /* initially mutex = 1 */ Process P i : do { wait(mutex); critical section"! signal(mutex); remainder section } while (1);! 3.17!

Re-visiting the Simultaneous Balance Update Problem! Shared data: int Balance;! semaphore mutex; // initially mutex = 1! Process A:!.! wait (mutex);! Balance = Balance 100;! signal (mutex);!! Process B:!.! wait (mutex);! Balance = Balance 200;! signal (mutex);!! 3.18!

Semaphore as a General Synchronization Tool! Semaphores provide a general process synchronization mechanism beyond the critical section problem.! Example problem: Execute B in P j only after A executed in P i " Use semaphore flag initialized to 0! Code:! " "P i : " P j :"!!!!!!A!wait(flag)!!!signal(flag)!B" 3.19!

Classical Problems of Synchronization! Producer-Consumer Problem! (HW problem) Readers-Writers Problem Dining-Philosophers Problem! The solutions will use only semaphores as synchronization tools, and busy waiting is to be avoided.! 3.20!

Producer-Consumer Problem! The bounded-buffer producer-consumer problem assumes that there is a buffer of size n.! The producer process puts items to the buffer area! The consumer process consumes items from the buffer! The producer and the consumer execute concurrently" see funny illustration of this at " http://www.youtube.com/watch?v=4wp3m1vg06q"........! producer! consumer! 3.21!

Producer-Consumer Problem (Cont.)! Make sure that:! 1.The producer and the consumer do not access the buffer area and related variables at the same time.! 2.No item is made available to the consumer if all the buffer slots are empty.! 3.No slot in the buffer is made available to the producer if all the buffer slots are full.! 3.22!

Producer-Consumer Problem! Shared data semaphore full, empty, mutex; Initially: full = 0 /* The number of full buffers */ empty = n /* The number of empty buffers */! mutex = 1 /* Semaphore controlling the access!to the buffer pool */! 3.23!

Producer Process!!!do {!!!!!!!!!produce an item in nextp"!!!!!!!!wait(empty);!!!!wait(mutex);!!!!!!!!!add nextp to buffer"!!!!!!!!signal(mutex);!!!!signal(full);!!!} while (1);! 3.24!

Consumer Process!!!do {!!!!wait(full);!!!!wait(mutex);!!!!!!!!!remove an item from buffer to nextc"!!!!!!!!signal(mutex);!!!!signal(empty);!!!!!!!!!consume the item in nextc"!!!!!!!} while (1);! 3.25!

Readers-Writers Problem! A data object (e.g. a file) is to be shared among several concurrent processes.! A writer process must have exclusive access to the data object.! Multiple reader processes may access the shared data simultaneously without a problem! Shared data semaphore mutex, wrt;! int readcount; Initially mutex = 1, readcount = 0, wrt = 1;!!!! 3.26!

Readers-Writers Problem: Writer Process! " "wait(wrt);!!!!!!!!!writing is performed!!!!!!!!signal(wrt);! 3.27!

Readers-Writers Problem: Reader Process!!!wait(mutex);!!!readcount++;!!!!if (readcount == 1)!!!!!wait(wrt);!!!signal(mutex);!!!!!!!!!reading is performed"!!!!!!!wait(mutex);! Is this solution o.k.?!!!readcount--;!!!if (readcount == 0)!!!!signal(wrt);!!!signal(mutex);! 3.28!

Dining-Philosophers Problem! Shared data!!!semaphore chopstick[5];" Initially all semaphore values are 1! Five philosophers share a common circular table. There are five chopsticks and a bowl of rice (in the middle). When a philosopher gets hungry, he tries to pick up the closest chopsticks.! A philosopher may pick up only one chopstick at a time, and cannot pick up a chopstick already in use. When done, he puts down both of his chopsticks, one after the other.! 3.29!

Dining-Philosophers Problem! Philosopher i:!!!do {!!!!wait(chopstick[i]);!!!!wait(chopstick[(i+1) % 5]);!!!!!!!!!!eat"!!!!!!!!signal(chopstick[i]);!!!!signal(chopstick[(i+1) % 5]);!!!!!!!!!!think" Is this solution o.k.?!!!!!!!!!} while (1);! 3.30!

Synchronization HW Assignment! Multiple parts! Part 1: Single-Producer, Single-Consumer! Write a C program with a producer thread (P), a bounded buffer (B), and a consumer thread (C)! P writes integers sequentially from 1 to n into B! C reads and displays each integer in order as they are received! Show and explain what happens when the threads are NOT synchronized! Part 2: Multiple Producer, Single Consumer! Write a C program with two producer threads Po (writes odd numbers) and Pe (writes even numbers) alternately into bounded buffer B, and one consumer thread C that reads and displays the integers in order as they are received! Show and explain what happens when the threads are NOT synchronized! 3.31!

Synchronization HW Assignment! Part 1:! P produces: 1, 2, 3, 4,, n into B! C prints contents of B: 1, 2, 3, 4,, n! show improper C ordering if not synchronized! Part 2:! Po produces: 1, 3, 5, 7, into B alternating with! Pe produces: 2, 4, 6, 8, into B! C prints contents of B: 1, 2, 3, 4,! show improper C ordering if not synchronized! Due Oct 13, will review requirements in class Sept 29; see program guide at! http://cs.gmu.edu/~hfoxwell/cs571/! 3.32!