© 2004, D. J. Foreman 1 Basic Synchronization. © 2004, D. J. Foreman 2 The Problem  Given: "i" is global  i++; expands into: LDAi ADAi,1 STAi  What.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: 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.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Secure Operating Systems Lesson 5: Shared Objects.
© 2004, D. J. Foreman 1 Basic Synchronization Semaphores.
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
Midterm Review. Agenda Part 1 – quick review of resources, I/O, kernel, interrupts Part 2 – processes, threads, synchronization, concurrency Part 3 –
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Synchronization Principles Gordon College Stephen Brinton.
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. 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.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
Chapter 2.3 : Interprocess Communication
CS533 - Concepts of Operating Systems 1 CS533 Concepts of Operating Systems Class 8 Synchronization on Multiprocessors.
Jonathan Walpole Computer Science Portland State University
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Chapter 1 Computer System Overview Sections 1.1 to 1.6 Instruction exe cution Interrupt Memory hierarchy Cache memory Locality: spatial and temporal Problem.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
© 2004, D. J. Foreman 1 Basic Synchronization Semaphores.
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Exam Review Andy Wang Operating Systems COP 4610 / CGS 5765.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 5: Process Synchronization.
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.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Sarah Diesburg Operating Systems COP 4610
CS703 – Advanced Operating Systems
Process Synchronization
Chapter 5: Process Synchronization
Background on the need for Synchronization
Synchronization.
Chapter 5: Process Synchronization
Thread Implementations; MUTEX
COMS Prelim 1 Review Session
Module 7a: Classic Synchronization
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Thread Implementations; MUTEX
Basic Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Andy Wang Operating Systems COP 4610 / CGS 5765
CSE 542: Operating Systems
Synchronization CSE 2431: Introduction to Operating Systems
Sarah Diesburg Operating Systems CS 3430
Presentation transcript:

© 2004, D. J. Foreman 1 Basic Synchronization

© 2004, D. J. Foreman 2 The Problem  Given: "i" is global  i++; expands into: LDAi ADAi,1 STAi  What if interrupt occurs DURING 1 or 2?  This is a “Critical Section”  Incorrect values of "i" can result  How do we prevent such errors

© 2004, D. J. Foreman 3 Solution Proposals 1. Turn interrupts off (not MP safe) 2. Flag in kernel to disallow context switch (works for kernel routines too) (MP safe) 3. Semaphores (MP safe) ■ Implemented as h/w inst ■ 80x86 has XCHG (not MP safe) Swaps RAM & register in 1 inst cycle not 1 bus cycle 4. Busy wait or "spin lock"

© 2004, D. J. Foreman 4 Clever S/W-Only Solution Shared memory int flag [0..1]; int turn Process 0 code flag[0] := T; turn := 1; while (flag [1] and turn =1) do no-op; critical section flag[0] := F;

© 2004, D. J. Foreman 5 Semaphores  Sometimes called an “indivisible test and set.” In 80x86 - XCHG, swaps 1 RAM location and a register in one instruction cycle (but not one bus cycle – so not useful in multi-processor environments – unless enhanced.) Flag – global – initially 0 WAITMOVAX,1 XCHGFLAG,AX CMPAX,1 JEWAIT ---- critical section MOVFLAG,0

© 2004, D. J. Foreman 6 Spin Locks  While (flag); // loops until flag=0  Wastes CPU time  Makes user seem “bad”

© 2004, D. J. Foreman 7 Another Critical Section Problem Loadr1,bal Loadr2,amount Addr1,r2 Storer1,bal Loadr1,bal Loadr2,amount Subr1,r2 Storer1,bal p1, p2 are in a race bal=bal + amount bal=bal - amount interrupt context-switch

© 2004, D. J. Foreman 8 Locking a Critical Section While(lock) wait; lock=true Loadr1,bal Loadr2,amount Addr1,r2 Storer1,bal lock=false While(lock) wait; lock=true; Loadr1,bal Loadr2,amount Subr1,r2 Storer1,bal lock=false bal=bal + amountbal=bal - amount

© 2004, D. J. Foreman 9 Wait & Signal  Classical definitions ■ Wait – Originally was P(s) DO WHILE (s<=0) wait;// The wait is interruptible s=s-1; ■ Signal - Originally was V(s) s=s+1;  Remember: these must appear as ATOMIC operations to the application  "s" is global

© 2004, D. J. Foreman 10 Strategies 1. User-only mode software 2. Disabling interrupts 3. H/W & O/S support

© 2004, D. J. Foreman 11 Acceptable Solutions 1. One process at a time in CritSec 2. Entry decision made by entrants 3. No indefinite wait allowed 4. Limit to predecessors

© 2004, D. J. Foreman 12 Additional Problems  Semaphores only protect the CritSec  Signaling adds need for more semaphores  Classical problems: ■ Bounded Buffer also known as Producer-Consumer ■ Readers & Writers Readers have precedence Writers have precedence