Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
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.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.
Lecture 4 Thread Concepts. Thread Definition A thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Concurrency: Deadlock and Starvation Chapter 6.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
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.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
The Critical Section Problem
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Lecture 2 Foundations and Definitions Processes/Threads.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Java Thread and Memory Model
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
Homework-6 Questions : 2,10,15,22.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
CSE 120 Principles of Operating
Chapter 5: Process Synchronization – Part 3
Background on the need for Synchronization
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Midterm review: closed book multiple choice chapters 1 to 9
Lecture 2 Part 2 Process Synchronization
Critical section problem
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
Chapter 6: Synchronization Tools
Foundations and Definitions
Presentation transcript:

Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm

Concurrent Execution More than one thread exists in system at once Can execute independently or in cooperation Asynchronous execution Threads generally independent Must occasionally communicate or synchronize Complex and difficult to manage such interactions Operating Systems - Deitel & Deitel

lock( ) Statement In C#, the lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. int Withdraw(int amount) { // This condition will not occur unless lock statement is removed if (balance < 0) throw new Exception("Negative Balance"); } // Comment out the next line to see the effect of leaving out lock lock (this) if (balance >= amount) Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; else return 0; // transaction rejected http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx

Definition of the Problem We start with a specification of the structure of the critical section problem and the assumptions under which it must be solved: Each of N processes is executing in an infinite loop a sequence of statement that can be divided into two subsequences: the critical section and the non-critical section The correctness specifications required of any solution are:

Mutual Exclusion Enforcing mutual exclusion is the method for preventing more than one process or thread from accessing a shared memory space at any given time. In multi-processing and multi-threaded applications, this is needed to ensure that asynchronous operations do not produce inconsistent data. Modern programming languages provide mechanisms for enforcing mutual exclusion. For example C# includes the lock( ) method that can be used to prevent two or more threads from entering a designated critical section of code in which shared memory will be accessed/modified. Unfortunately these machine specific methods cannot be used in distributed applications since there is no way to guarantee that some remote system will support them. What is needed, is a software-only means of enforcing mutual exclusion...

Mutual Exclusion: Version 1

Mutual Exclusion: Version 2

Mutual Exclusion: Version 3

Mutual Exclusion: Version 4

Mutual Exclusion: Version 5

Mutual Exclusion: Version 6

N-Process Mutual Exclusion When we consider n processes sharing memory rather than just two, the problem of mutual exclusion becomes much more complex. An efficient software-only algorithm for enforcing mutual exclusion among n process was developed by L.Lamport in which each process must “take a ticket” or be placed in a queue to wait for access to shared memory. This method is called Lamport’s Bakery Algorithm and is particularly well suited to distributed processing. 1329 42?… number 42? @$#%&*!! Compare Lamport's Bakery Algorithm to Dekker's or Peterson's Algorithm. How are they different. What do we gain by using Lamport's Algorithm? What do we give up?

Using Mutex in C# using System; using System.Threading; class Mutex_Demo { private static Mutex mut = new Mutex(); //create a new Mutex private const int numIterations = 2; private const int numThreads = 5; static void Main() for (int i = 0; i < numThreads; i++) Thread myThread = new Thread(new ThreadStart(MyThreadProc)); myThread.Name = String.Format("Thread{0}", i + 1); myThread.Start(); } Console.ReadKey(); mut.Close(); private static void MyThreadProc() for (int i = 0; i < numIterations; i++) UseResource(); private static void UseResource() // protected resource mut.WaitOne(); // wait until it is safe to enter Console.WriteLine("{0} using resource", Thread.CurrentThread.Name); Thread.Sleep(500); // time in protected resource Console.WriteLine("{0} leaving resource \r\n", Thread.CurrentThread.Name); mut.ReleaseMutex(); // release the mutex

Airline Reservation System - Case Study Boeing 737-400 Project Outline Simulate concurrent access to available seating on a particular carrier for a specific flight. Multiple agents (4 to 6) will seat customers with preferences. Program will simulate customer time of response. Program will monitor and report data on wait time, and service rates. Seating Chart

Customer Preferences Class - First (rows 1-6) - Business Coach (rows 7- 26) Seat Preference - Aisle - Middle - Window Seat Location - Near Front - Over Wing - Near Rear Number of Seats - 1 (Individual seating) - 2 (assumed adjacent) - 3 (at least 2 adjacent) Seating Restrictions - Exit row OK (true or false)

Server Interaction with each Customer Customer States Preferences Server Tags Candidate Seat(s) available no yes Server Changes Candidate Seat(s) rejects decide Server Releases Seat(s) 0.1 accepts 0.9 Server Reserves Seat(s)

Seat Status Array a shared resource : A B C D E F 1 2 3 4 5 26 Possible Seat States available tagged reserved : A ticket agent thread may tag any available seat. A tagged seat my be release or reserved only by the thread that has tagged it.

Summary of Case Study Airline Reservation System for One Flight on One Carrier 4 to 6 ticketing agents running concurrently A queue of customers for each agent Threads use mutex and/or semaphores to enforce mutual exclusion Customers are simulated WRT to Preferrences and Time of Service Wait Times and Customer Service Times are Monitored, Analyzed and Reported