1 Program5 Due Friday, March 27. 2 Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...

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.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
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.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Multithreading The objectives of this chapter are:
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
1 Concurrency: Deadlock and Starvation Chapter 6.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
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,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS 3340 Windows Programming Prog4 Test Program5 Due Friday, March 27.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
1 Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread... amount = … invoke delegate transact.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
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.
Homework-6 Questions : 2,10,15,22.
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.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Multithreading / Concurrency
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Monitors Chapter 7.
Multithreading.
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Multithreading.
Lecture 2 Part 2 Process Synchronization
Another Means Of Thread Synchronization
Monitors Chapter 7.
Concurrency: Mutual Exclusion and Process Synchronization
Monitors Chapter 7.
CSE 153 Design of Operating Systems Winter 19
Process/Thread Synchronization (Part 2)
Presentation transcript:

1 Program5 Due Friday, March 27

2 Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread... amount = … invoke delegate transact (amount)... user_thread... amount = … invoke delegate transact (amount)... Total

3 Data Shared by Multiple Threads user_thread... amount = … Total += amount... user_thread... amount = … Total += amount... user_thread... amount = … Total += amount... Total

4 Time Sharing threadOne threadTwo Time line The threads don’t know when the time out will occur.

5 Data Shared by Multiple Threads user_thread... amount = … Total += amount // In assembly // Load Total // Add amount // Save Total... Total user_thread... amount = … Total += amount // In assembly // Load Total // Add amount // Save Total...

6 Mutual Exclusion user_thread Start running amount = 50 Total += 50 // In assembly // Load Total Total: 1000 Time Out Continue running Total: 1000 // Add amount // Save Total Total: Total: user_thread Start running amount = -100 Total += -100 // In assembly // Load Total Total: 1000 // Add amount // Save Total Total:

Critical Section When accessing shared data. Should be executed as a single statement. Total += amount // Load Total // Add amount // Save Total 7

Two Classes for Mutual Exclusion MutEx Monitor 8

9 Class MutEx (Semaphore) A synchronization primitive that can also be used for inter- process synchronization. When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. MutEx is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a MutEx, the second thread that wants to acquire that MutEx is suspended until the first thread releases the MutEx.

10 Class Monitor Provides a mechanism that synchronizes access to objects. The Monitor class controls access to objects by granting a lock for an object to a single thread. Object locks provide the ability to restrict access to a block of code, commonly called a critical section. While a thread owns the lock for an object, no other thread can acquire that lock.

11 Class Monitor Features It is associated with an object on demand. It is unbound, which means it can be accessed directly from any context. An instance of the Monitor class cannot be created. Not inheritable Public NotInheritable Class Monitor Shared methods

12 Class Monitor The following information is maintained for each synchronized object: A reference to the thread that currently holds the lock. A reference to a ready queue, which contains the threads that are ready to obtain the lock. A reference to a waiting queue, which contains the threads that are waiting for notification of a change in the state of the locked object.

13 Monitor Methods Enter Exit TryEnter Wait Pulse PulseAll...

14 Using Class Monitor Private Total As Integer ‘ Cannot use Monitor on integers Private TotalObj As New Object user_thread... amount = 50 Monitor.Enter(TotalObj) (CriticalSection) Total += 50 // In assembly // Load Total // Add amount // Save Total Monitor.Exit(TotalObj)...

15 Using Class Monitor user_thread Start running amount = 50 Monitor.Enter(TotalObj) (CriticalSection) Total += 50 // In assembly // Load Total Total: 1000 Time Out Continue running Total: 1000 // Add amount // Save Total Total: 1050 Monitor.Exit(TotalObj)... TotalObj Total: user_thread Start running amount = -100 Monitor.Enter(TotalObj) (wait here) (continue) (CriticalSection) Total += -100 // In assembly // Load Total Total: 1050 // Add amount // Save Total Total: 950 Monitor.Exit(TotalObj)...

ManualResetEvent &AutoResetEvent Class ManualResetEvent –Set: change the state to signaled (Green light) –WaitOne: wait for signal –Reset: change the state to non-signaled (Red Light) –Like a traffic light Class AutoResetEvent –Set: change the state to signaled (Green light) –WaitOne: wait for signal –State becomes non-signaled after a waiting thread released –Like a Stop sign (combined with traffic light): one at a time –(Reset): change the state to non-signaled (Red Light) 16

17 Queue Class Represents a first-in, first-out collection of objects. This class implements a queue as a circular array. Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

18 Queue Constructor Queue: Initializes a new instance of the Queue class that is empty, has the default initial capacity, and uses the default growth factor. Queue(Int32, Single): Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the specified growth factor....

Queue Methods Private myQueue As Queue = New Queue ' Puts obj at the end of the queue ' Any obj, could be a thread object myQueue.Enqueue(obj) ' Takes the first obj from the queue obj = myQueue.Dequeue ' Checks if the queue is empty If myQueue.Count > 0 Then ' Points to the first obj of the queue obj = myQueue.Peek 19

Multiple Threads Accessing One Same Queue Any instance members are not guaranteed to be thread safe. Private myQueue As Queue = New Queue ‘ To guarantee at most one thread accessing the queue Monitor.Enter(myQueue) myQueue.Enqueue(obj) ' Could do other things ' Exits the queue waking up next waiting thread if any Monitor.Exit(myQueue) 20

Multiple Threads Accessing One Same Queue Monitor.Enter(myQueue) ' Checks if the queue is empty If myQueue.Count > 0 Then ' Points to the first obj of the queue obj = myQueue.Peek If... Then obj = myQueue.Dequeue... End If Monitor.Exit(myQueue) 21

22 Prog5: Readers and Writers There is a data area shared among a number of threads File, Memory, Registers,... Variable Total of Integer Readers: Read data only Writers: Modify data Requirements: Any number of Readers may simultaneously read the data. A Writer requires exclusive access to the data. First In First Out (FIFO) rule.

23 ReaderWriterLock Starvation Not a FIFO solution We don’t use the lock!

24 FIFO Solution DatabaseClass –Total –ReaderCount (RC) Number of readers reading the data –WriterCount (WC) Number of writers writing the data Could be a Boolean –DataObj To use Monitor on RC and/or WC Not thread Safe!

25 FIFO Solution ReaderWriter Class –FIFO Queue –ReaderWriterEvent: AutoResetEvent To wait in the FIFO queue –EndProgram: AutoResetEvent To terminate the program –ID –Type –Sub Run

26 FIFO Solution Reader class and Writer Class –Sub-class of ReaderWriter –Overrides ID –Overrides Type –Overrides Run Mutual exclusion! FIFO! No Deadlock!

Pseudo code for the Run method of Reader 27 (For one reader and no loop inside: no spindown) If the queue is not empty or the database status is writing Add itself to the queue Wait for _ReaderWriterEvent Wakeup next reader before accessing the database Increase reader count by one Reading the value Decrease reader count by one If the database status is empty Wakeup next one in the queue

Pseudo code for the Run method of Writer 28 (For one writer and no loop inside: no spindown) If the queue is not empty or the database status is not empty Add itself to the queue Wait for _ReaderWriterEvent (Wakeup next reader before accessing the database) Increase writer count by one Update the value Decrease writer count by one (If the database status is empty) Wakeup next one in the queue