May 23, 2002Serguei A. Mokhov, 1 Synchronization COMP346/5461 - Operating Systems Tutorial 3 Revision 1.2 October 7, 2003.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
May 21, 2002Serguei A. Mokhov, 1 Some Theory Review COMP346/ Operating Systems Revision 1.2 September 30, 2003.
Ken Birman 1. Refresher: Dekers Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J]
CS542 Topics in Distributed Systems Diganta Goswami.
1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of synchronization method. A barrier for a group of threads or.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
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.
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
© 2004, D. J. Foreman 1 The Dining Philosophers Problem.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Intro to OS CUCS Mossé Processes and Threads What is a process? What is a thread? What types? A program has one or more locus of execution. Each execution.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
June 11, 2002Serguei A. Mokhov, 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Atomic Operations David Monismith cs550 Operating Systems.
Tutorial 5 Even More Synchronization! presented by: Antonio Maiorano Paul Di Marco.
Concurrency, Mutual Exclusion and Synchronization.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Mutual Exclusion Steve Ko Computer Sciences and Engineering University at Buffalo.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
CS425 /CSE424/ECE428 – Distributed Systems – Fall 2011 Material derived from slides by I. Gupta, M. Harandi, J. Hou, S. Mitra, K. Nahrstedt, N. Vaidya.
Internet Software Development Controlling Threads Paul J Krause.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
Lecture 12-1 Computer Science 425 Distributed Systems CS 425 / CSE 424 / ECE 428 Fall 2012 Indranil Gupta (Indy) October 4, 2012 Lecture 12 Mutual Exclusion.
June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
CSCD 330 Network Programming
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Process Synchronization
Monitors.
IS310 Hardware & Network Infrastructure Ronny L
Process Synchronization - I
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
143a discussion session week 3
Inter-Process Communication and Synchronization
Threading And Parallel Programming Constructs
Lecture 2 Part 2 Process Synchronization
Critical section problem
Concurrency: Mutual Exclusion and Process Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Process Synchronization
Process Synchronization
Presentation transcript:

May 23, 2002Serguei A. Mokhov, 1 Synchronization COMP346/ Operating Systems Tutorial 3 Revision 1.2 October 7, 2003

May 23, 2002Serguei A. Mokhov, 2 Topics Synchronization in Details Semaphores Introducing Semaphore.java

May 23, 2002Serguei A. Mokhov, 3 Synchronization What is it? An act of communication between unrelated processes to sync their activities to achieve some goals and solve some common problems of multiprogramming: –Mutual exclusion and critical sections –Specific execution order must be maintained Improper sync may cause a very big problem in all Operating Systems – a deadlock.

May 23, 2002Serguei A. Mokhov, 4 A Typical Example Shared account (between spouses John and Jane) Current account balance: $400 John and Jane happened to be at the ATM in different places, but at almost the same time. Let say John wants to withdraw $200, and Jane wants to withdraw $300. First scenario: both of them see the current balance of $400 and relatively at the same time perform request to withdraw. John goes first…

May 23, 2002Serguei A. Mokhov, 5 A Typical Example (2) A DB system takes $400, subtracts $200, and gets interrupted (e.g. network congestion), thus the remaining balance of $200 wasn’t recorded yet. Jane’s request goes through and the system writes down $100 balance remaining. Then John’s request finally goes through, and system updates the balance to $200. The bank is a victim in that case because John and Jane were able to withdraw $500 and have $200 remaining, when initially account’s balance was $400!

May 23, 2002Serguei A. Mokhov, 6 A Typical Example (4) class John extends Thread { run() { balance = ATM.getBalance(); if(balance >= $200) ATM.withdraw($200); } } class Jane extends Thread { run() { balance = ATM.getBalance(); if(balance >= $300) ATM.withdraw($300); } } class ATM{ … int withdraw(amount){ if(amount <= balance) { balance = balance – amount; return amount; } A trouble may occur at the points marked with the red arrows. The code MUST NOT be interrupted at those places.

May 23, 2002Serguei A. Mokhov, 7 Solution: Use Semaphores Obvious: make the critical section part atomic. One way of doing it: Semaphores Semaphores are system-wide OS objects (also resources) used to –Protect critical section (mutexes – for Mutual Exclusion), –Coordinate other process’ activities. Semaphores are NOT shared memory segments! But they both are often used together.

May 23, 2002Serguei A. Mokhov, 8 Semaphores for CS There are two main operations on semaphores – Wait() and Signal(). A process wishing to enter the critical section tries to acquire the semaphore (a lock in a human world) by calling Wait(sem) (a.k.a. P() ). –If the lock isn’t there (i.e. “in use”), the execution of the process calling Wait() is suspended (put asleep). –Otherwise, it acquires the semaphore and does the CS stuff. When a process is over with the CS, it notifies the rest of the processes waiting on the same semaphore that they can go in by calling Signal(sem) (a.k.a. V() ). The awakened process goes back to the ready queue to compete again to enter the CS.

May 23, 2002Serguei A. Mokhov, 9 A Typical Example Solution class John extends Thread { run() { mutex.Wait(); balance = ATM.getBalance(); if(balance >= $200) ATM.withdraw($200); mutex.Signal(); } } class Jane extends Thread { run() { mutex.Wait(); balance = ATM.getBalance(); if(balance >= $300) ATM.withdraw($300); mutex.Signal(); } } class ATM{ … Semaphore mutex = 1; int withdraw(amount) { if(amount <= balance) { balance = balance – amount; return amount; } A trouble may occur at the points marked with red. The code MUST NOT be interrupted at those places.

May 23, 2002Serguei A. Mokhov, 10 Semaphores for Barrier Sync Take a look at the typical problem: –all processes must finish their phase I before any of them starts phase II –processes must proceed to their phase II in a specific order, for example: 1, 2, 3… This is called barrier synchronization. semaphore s1 = 0, s2 = 0; process P1 process P2 V (s1) V (s2) P (s2) P (s1)

May 23, 2002Serguei A. Mokhov, 11 Barrier Sync for Three Processes A possible copy-cat attempt: Why it doesn’t work? –Scenario: 1-6, process 2 even hasn’t started! –None of the requirements are met semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P V (s1) V (s2) 2 V (s3) 5 P (s3) P (s1) P (s2) 6

May 23, 2002Serguei A. Mokhov, 12 Barrier Sync for Three Processes (2) Another attempt: What’s wrong now? –Scenario: 1-10, so far so good, but after… –The second requirement isn’t met semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P P (s1) 5 V (s1) 2 V (s1) 9 P (s1) 6 P (s2) 3 P (s3) 10 V (s2) V (s3)

May 23, 2002Serguei A. Mokhov, 13 Barrier Sync for Three Processes (3) Last attempt: A bit “optimized”: semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 P (s1) V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3) semaphore s1 = -1, s2 = 0, s3 = 0; process P1 process P2 process P3 V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3)

May 23, 2002Serguei A. Mokhov, 14 Barrier Sync: Need for the General Solution Problem with the proposed solution: # of semaphores == # of processes. Semaphores as any other resource are limited and take space => overhead Imagine you need to sync 10 processes in the same manner? 100? 1000? –Complete mess and a high possibility of a deadlock!

May 23, 2002Serguei A. Mokhov, 15 Barrier Sync: Need for the General Solution (2) Attempt for the first requirement: The second requirement is left as an exercise to the curious student :-) semaphore s1 = -n + 2, s2 = 0; process P1 process P2... process Pn... P (s1) V (s1)... V (s1) V (s2) P (s2)... P (s2) V (s2)... V (s2)...

May 23, 2002Serguei A. Mokhov, 16 Introducing the Semaphore Class NOTE: Operations Signal and Wait are guaranteed to be atomic! class Semaphore { private int value; public Semaphore(int value) { this.value = value; } public Semaphore() { this(0); }...

May 23, 2002Serguei A. Mokhov, 17 Introducing the Semaphore Class (2)... public synchronized void Wait() { while(this.value <= 0) { try { wait(); } catch(InterruptedException e) { System.out.println ( "Semaphore::Wait() …” + e.getMessage() ); e.printStackTrace(); } this.value--; }...

May 23, 2002Serguei A. Mokhov, 18 Introducing the Semaphore Class (3)... public synchronized void Signal() { ++this.value; notify(); } public synchronized void P() { this.Wait(); } public synchronized void V() { this.Signal(); }

May 23, 2002Serguei A. Mokhov, 19 Next Tutorial Deadlock

May 23, 2002Serguei A. Mokhov, 20 References