Process Synchronization I CPE 261403 - Operating Systems

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
Operating Systems Part III: Process Management (Process Synchronization)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Secure Operating Systems Lesson 5: Shared Objects.
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Synchronization Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization I Nov 27, 2007 CPE Operating Systems
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
3.1. Concurrency, Critical Sections, Semaphores
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CSE Operating System Principles Synchronization.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Q: 請以實際例子說明 critical section 之意 ? 何謂 race condition? while (true) { /*
Homework-6 Questions : 2,10,15,22.
Big Picture Lab 4 Operating Systems C Andras Moritz
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Lecture 22 Syed Mansoor Sarwar
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Chapter 6: Process Synchronization
Lecture 21 Syed Mansoor Sarwar
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Process Synchronization I CPE Operating Systems

Why do we need to Synchronize?

Problem Example A simple C primitive: Count++ Actual machine code: Register1 = count Increase register1 Count = register1

Problem Example Register1 = count Increase register1 Count = register1 Register2 = count Increase register2 Count = register2 Register1 = count Register2 = count Increase register2 Count = register2 Increase register1 Count = register1

Critical Section Code where multiple processes (threads) can write to a shared location or acquire a shared resource. Need some form of Synchronization!

Software Solution? Assuming only two processes turn = p1; While (turn == p2) {/* busy wait */}; turn = p2; //Critical Section Count++; turn = p2; While (turn == p1) {/* busy wait */}; turn = p1; //Critical Section Count++; P1P2 (There are still problematic cases)

Software Solution? Peterson’s Solution turn = p2; p1_needs_to_work = true; While (turn == p2 && p2_needs_to_work) {/* busy wait */}; p1_needs_to_work = false; //Critical Section Count++; turn = p1; p2_needs_to_work = true; While (turn == p1 && p1_needs_to_work) {/* busy wait */}; p2_needs_to_work = false; //Critical Section Count++; P1P2

Synchronization Hardware Special atomic hardware instructions

Test-and-Set Instruction Equivalent to: boolean testAndSet(boolean *Lock) { boolean originalVal = *Lock; *Lock = true; return originalVal; }

Test-And-Set Applies to any N processes While (testAndSet(&Lock)) { /* busy wait */ }; Lock = false; //Critical Section Count++; While (testAndSet(&Lock)) { /* busy wait */ }; Lock = false; //Critical Section Count++; P1P2

Requirements for solving the Critical Section Problem Mutual Exclusion Progress Bounded Waiting

Semaphores

Semaphore as a Signaling Tool

Semaphore as a Communication Tool

ROGER

Nuclear Disarmamenthttp:// Semaphore in Pop Culture

Types of Semaphores Critical section tool (Mutex Lock) Counting Semaphore

Mutex Lock Wait(S); Signal(S); //Critical Section Count++; Wait(S); Signal(S); //Critical Section Count++; P1P2 Semaphore S;

Counting Semaphore Wait(S); Signal(S); // Access Shared // resources P1 Semaphore S = 3; Database P1

Semaphore Implementation Wait (S) { S.value--; if (S.value < 0) { sleep(); } Signal (S){ S.value++; if (S.value <= 0) { wakeupSleepingProcesses(); }

Semaphore’s Application Critical Section Tool Resource Usage Control (limiting usage) Synchronizing threads

Semaphore Example I: Resource Usage Control Database Manager P1 Limit 2 connections P2 PnPn …

Wait(Database); // make DB connection // and get the data Signal(Database); Database Manager P1 P2 PnPn … P Semaphore Database = 2

Semaphore Example II: Process Synchronization P3: Database Manager P1: Client P2: Server Request DB Connection DB DataResponse P2 limits 10 connection P3 limits 2 connections

P3: Database Manager P1: Client P2: Server RequestDB Connection DB DataResponse Semaphore Client = 0, Server = 10, Database = 2, DBData = 0 Wait(server); Signal(Client); Wait(DBData); // receives the data Signal(server); While (true) { Wait(Client); Wait(Database); // make DB connection // and get the data Signal(Database); Signal(DBData); } ClientServer

The Dim sum Restaurant

Steamers Waiter Restaurant Waiter

Semaphore Seats=6; Customer=0; Waiter=2; Steamer=3; Food=1; CustomerWaiter

While (true) { Wait (Customer) Signal (Waiter) // gets the order Wait (Steamer) // food is cooked Signal (Steamer) Signal (Food) } Wait(Seats) Signal(Customer) Wait(Waiter) /// Places order Wait(Food) /// Eat the food Signal(Seats) Semaphore Seats=6; Customer=0; Waiter=1; Steamer=3; Food=0; CustomerWaiter