1 Solution to the Gaming Parlor Programming Project.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
CSCC69: Operating Systems
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
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.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Solution to the Gaming Parlor Programming Project.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
Jonathan Walpole Computer Science Portland State University
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
Jonathan Walpole Computer Science Portland State University
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
CS 333 Introduction to Operating Systems Class 13 - Virtual Memory (3) Jonathan Walpole Computer Science Portland State University.
Solve Multi-step Equations (var. both sides) Students will solve multi-step equations with variables on both sides using distributive property, combining.
CS Introduction to Operating Systems
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
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.
CS510 Concurrent Systems Introduction to Concurrency.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
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.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CSE 451: Operating Systems Section 5 Synchronization.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Lecture Notes 1/20/05 Pseudocode.  Pseudocode standard which we will follow in this class: - Statements are written in simple English; - Each instruction.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
Chapter 5: Process Synchronization – Part 3
PARALLEL PROGRAM CHALLENGES
Chapter 5: Process Synchronization – Part II
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
Linked Lists Damian Gordon.
Semaphores and Condition Variables
Jonathan Walpole Computer Science Portland State University
CS510 Operating System Foundations
CS510 Operating System Foundations
Thread Implementations; MUTEX
CS510 Operating System Foundations
Synchronization Hank Levy 1.
CS510 Operating System Foundations
Another Means Of Thread Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Slides by Prof. Landon Cox and Vamsi Thummala
Thread Synchronization including Mutual Exclusion
Thread Implementations; MUTEX
Synchronization Hank Levy 1.
CS510 Operating System Foundations
CSE 451 Section 1/27/2000.
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

1 Solution to the Gaming Parlor Programming Project

2 The Gaming Parlor - Solution Scenario: Front desk with dice (resource units) Groups request (e.g., 5) dice (They request resources) Groups must wait, if none available Dice are returned (resources are released) A list of waiting groups... A “condition” variable The condition is signalled The group checks and finds it needs to wait some more The group (thread) waits...and goes to the end of the line Problem?

3 The Gaming Parlor - Solution Scenario: Front desk with dice (resource units) Groups request (e.g., 5) dice (They request resources) Groups must wait, if none available Dice are returned (resources are released) A list of waiting groups... A “condition” variable The condition is signalled The group checks and finds it needs to wait some more The group (thread) waits...and goes to the end of the line Problem? Starvation!

4 The Gaming Parlor - Solution Approach: Serve every group “first-come-first-served”. Implementation: Keep the thread at the front of the line separate “Leader” - the thread that is at the front of the line Use 2 condition variables. “Leader” will have at most one waiting thread “RestOfLine” will have all other waiting threads

5 The Threads function Group (numDice: int) var i: int for i = 1 to 5 gameParlor.Acquire (numDice) currentThread.Yield () gameParlor.Release (numDice) currentThread.Yield () endFor endFunction thA.Init (“A”) thA.Fork (Group, 4)...

6 The Monitor class GameParlor superclass Object fields monitorLock: Mutex leader: Condition restOfLine: Condition numberDiceAvail: int numberOfWaitingGroups: int methods Init () Acquire (numNeeded: int) Release (numReturned: int) Print (str: String, count: int) endClass

7 The Release Method method Release (numReturned: int) monitorLock.Lock () -- Return the dice numberDiceAvail = numberDiceAvail + numReturned -- Print self.Print ("releases and adds back", numReturned) -- Wakeup the first group in line (if any) leader.Signal (&monitorLock) monitorLock.Unlock () endMethod

8 The Acquire Method method Acquire (numNeeded: int) monitorLock.Lock () -- Print self.Print ("requests", numNeeded) -- Indicate that we are waiting for dice. numberOfWaitingGroups = numberOfWaitingGroups If there is a line, then get into it. if numberOfWaitingGroups > 1 restOfLine.Wait (&monitorLock) endIf -- Now we're at the head of the line. Wait until there are enough dice. while numberDiceAvail < numNeeded leader.Wait (&monitorLock) endWhile...

9 The Acquire Method Take our dice. numberDiceAvail = numberDiceAvail - numNeeded -- Now we are no longer waiting; wakeup some other group and leave. numberOfWaitingGroups = numberOfWaitingGroups - 1 restOfLine.Signal (&monitorLock) -- Print self.Print ("proceeds with", numNeeded) monitorLock.Unlock () endMethod