Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

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.
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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
Synchronization Principles Gordon College Stephen Brinton.
Concurrency.
The Critical-Section Problem
Critical Sections with lots of Threads. Announcements CS 414 Homework due today.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
OS Spring’04 Concurrency Operating Systems Spring 2004.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
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 Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrency, Mutual Exclusion and Synchronization.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
3.1. Concurrency, Critical Sections, Semaphores
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
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.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
OS Winter’03 Concurrency. OS Winter’03 Bakery algorithm of Lamport  Critical section algorithm for any n>1  Each time a process is requesting an entry.
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Topic 6 (Textbook - Chapter 5) Process Synchronization
The Critical-Section Problem
Lecture 19 Syed Mansoor Sarwar
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Chapter 6: Process Synchronization
CSE 153 Design of Operating Systems Winter 19
Synchronization.
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Presentation transcript:

Synchronization (other solutions …)

Announcements Assignment 2 is graded Project 1 is due today

Critical Section Problem Problem: Design a protocol for processes to cooperate, such that only one process is in its critical section –How to make multiple instructions seem like one? Processes progress with non-zero speed, no assumption on clock speed Used extensively in operating systems: Queues, shared variables, interrupt handlers, etc. Process 1 Process 2 CS 1 Time  CS 2

Solution Structure Shared vars: Initialization: Process:... Entry Section Critical Section Exit Section Added to solve the CS problem

Solution Requirements Mutual Exclusion –Only one process can be in the critical section at any time Progress –Decision on who enters CS cannot be indefinitely postponed No deadlock Bounded Waiting –Bound on #times others can enter CS, while I am waiting No livelock Also efficient (no extra resources), fair, simple, …

Solution 1 (Two Processes) Process 0 turn = 0; while(turn != 0); Critical Section turn = 1; Shared: int turn; Initialize: turn = 0; Problem: Does not satisfy progress! Assumes strict alternation Process 1 turn = 1; while(turn != 1); Critical Section turn = 0;

Solution 1 (Two Processes) Process 0 flag[0] = true; while(flag[1]); Critical Section flag[0] = false; Shared: bool flag[2]; Initialize: flag[0] = false; flag[1] = false; Problem: Does not satisfy progress! Timing dependent What if you swap statement in the entry section? Process 1 flag[1] = true; while(flag[0]); Critical Section flag[1] = false;

Peterson’s Algorithm Process i flag[i] = true; turn = j; while(flag[j] && turn == j); Critical Section flag[i] = false; Shared: bool flag[2]; int turn; Initialize: flag[0] = false; flag[1] = false; turn = 0; Works for atomic load/stores! Special problem for multiprocessors

Solution 2: Disable Interrupts Disable interrupts while process is in critical section 2 approaches: –Scheduler checks if program counter is in critical section Do not interrupt if it is Pro: Fast Con: needs compiler support –Process disables interrupts before entering critical section Enables on exit Pro: Easy Con: when infinite loop in CS Cons –Response time –Multiprocessors  Critical section should be small! Process Disable interrupts Critical Section Enable interrupts Entry Section Exit Section

Solution 3: Hardware Primitives Modern hardware provides better atomic instructions: –test_and_set –swap –compare_and_swap –load_linked/store_conditional –fetch_and_add Entire function is atomic –Possible by locking bus for both load and store bool test_and_set(bool *target) { bool temp = *target; *target = TRUE; return temp; } void swap(bool *a, bool *b) { bool temp = *a; *a = *b; *b = temp; }

Solution 3: Hardware Primitives Process i While(test_and_set(&lock)); Critical Section lock = false; Share: int lock; Initialize: lock = false; Problem: Does not satisfy bounded waiting (see book for correct solution)

Hardware Primitives Pros: –Simple primitive –Easy to program critical sections Cons –Busy waiting for entire duration of critical section! Also called spinlocks

Implementing Semaphores P() and V() should be executed atomically –Disable interrupts, or –Busy waiting solutions Move busy wait from entry of CS to P() and V() P() and V() code is small

Semaphores Atomicity typedef struct semaphore { int lock; int value: ProcessList L; } Semaphore; void P(Semaphore *S) { while(test_and_set(&S->lock) == 1) /* do nothing */; S->value = S->value - 1; if (S.value < 0) { add this process to S.L; atomic_clear_and block(&S->lock); } else atomicclear(&S->lock); } void V(S) { while(test_and_set(&S->lock) == 1) /* do nothing */; S->value = S->value + 1; if (S->value <= 0) { remove a process P from S.L; wakeup P } atomicclear(&S->lock); }

A Real Race Condition Customer to sales executive: “This is the second time I have written to you, and I don't blame you for not answering me, because I sounded crazy, but it is a fact that as a tradition in our family we have ice-cream for dessert after dinner each night. …”