Programming Project 3: Barbers and Gamblers

Similar documents
Notes to Instructors Concerning the BLITZ Projects

Programming Project 4: Kernel Resource Management

Solution to the. Gaming Parlor. Programming Project

CS510 Operating System Foundations. Jonathan Walpole

Interprocess Communication and Synchronization

Puzzle: Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.

PROCESS SYNCHRONIZATION

5 Classical IPC Problems

CS450 OPERATING SYSTEMS FINAL EXAM ANSWER KEY

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Dealing with Issues for Interprocess Communication

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Operating Systems and Networks Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Programming Project 1: Introduction to the BLITZ Tools

Introducing Shared-Memory Concurrency

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CS 105, Spring 2015 Ring Buffer

4.5 Cigarette smokers problem

EECS 482 Introduction to Operating Systems

Concurrency: a crash course

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Concurrency. Chapter 5

CS 153 Design of Operating Systems Winter 2016

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Programming Assignment 3

Discussion CSE 224. Week 4

Deadlocks: Detection & Avoidance

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

CPS 110 Midterm. Spring 2011

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CSE Traditional Operating Systems deal with typical system software designed to be:

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

IV. Process Synchronisation

CPS 310 second midterm exam, 11/6/2013

Project 4: Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Last Class: Monitors. Real-world Examples

Deadlock CS 241. March 19, University of Illinois

Process Management And Synchronization

For 100% Result Oriented IGNOU Coaching and Project Training Call CPD: ,

CS4411 Intro. to Operating Systems Exam 1 Fall points 9 pages

Process Synchronization. studykorner.org

Course Description: This course includes the basic concepts of operating system

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

COMP 346 WINTER Tutorial 5 MONITORS

CS 153 Design of Operating Systems Winter 2016

Threads Questions Important Questions

PROVING THINGS ABOUT PROGRAMS

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Concurrent Programming in C++ Venkat

CS 333 Introduction to Operating Systems. Class 6 Monitors and Message Passing. Jonathan Walpole Computer Science Portland State University

Problem Set: Concurrency

In class we examined the need for concurrent execution paths like a consumer and a producer to synchronize their access to a shared ring buffer.

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Object Oriented Programming and Design in Java. Session 18 Instructor: Bert Huang

Lab 12 - Concurrency 2

Midterm Exam #2 Solutions October 25, 2016 CS162 Operating Systems

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Processor speed. Concurrency Structure and Interpretation of Computer Programs. Multiple processors. Processor speed. Mike Phillips <mpp>

CSE 153 Design of Operating Systems

Midterm Exam. October 20th, Thursday NSC

Introduction to Operating Systems

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Lab 4: On Lists and Light Sabers

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

Concurrency. Glossary

CS153: Deadlock. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Explain briefly how starvation may occur in process scheduling. (2 marks)

What's wrong with Semaphores?

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Lecture 9: Midterm Review

Interprocess Communication By: Kaushik Vaghani

CPS 310 first midterm exam, 10/6/2014

Threading and Synchronization. Fahd Albinali

Name of chapter & details

CS-537: Midterm Exam (Spring 2001)

POSIX Threads: a first step toward parallel programming. George Bosilca

College of Computer & Information Science Spring 2010 Northeastern University 26 January 2010

The Deadlock Lecture

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

CS-537: Midterm Exam (Fall 2013) Professor McFlub

CSCI 4061: Inter-Process Communication

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

Laboratory Assignment #3. Extending scull, a char pseudo-device

THREADS VS. PROCESSES

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Transcription:

Programming Project 3: Barbers and Gamblers Due Date: Duration: One week Overview and Goal In this project you will gain additional familiarity writing programs involving concurrency control. You will implement a couple of classical IPC problems to become more comfortable with Semaphores, Mutexes, Condition Variables, and the Monitor approach. Download New Files Start by creating a directory for the files you ll need for this project: ~YourUserName/cs333/p3 Then download all the files from: http://www.cs.pdx.edu/~harry/blitz/osproject/p3/ Even though some of the files have the same names, be sure to get new copies for each project. In general some files may be modified. Please keep your old files from previous projects separate and don t modify them once you submit them. This is a good rule for all programming projects in all classes. If there is ever any question about whether code was completed on time, we can always go back and look at the Unix file modification date information. For this project, you should get the following files: makefile DISK System.h System.c Runtime.s Switch.s List.h List.c Page 1

Thread.h Thread.c Synch.h Synch.c Main.h Main.c The file Synch.c contains our solution for project 2. You may use either our version of this file, or the version of Synch.c you created. We have also included the file Proj2Sol.pdf which contains my solution code for Producer-Consumer and Dining Philosophers. If you had difficulty with it, you can take a look at the solution code. You will modify and submit the following files: Main.h Main.c Task 1: Implement the Sleeping Barber Problem An older edition of the Tanenbaum textbook describes the Sleeping Barber problem and gives a solution in C. (This material can also be found in a separate file called SleepingBarberProblem.pdf in the p3 directory.) Translate this into a working KPL program. You ll also need to create some code to print out what happens when you run the program. What will you do for the cut_hair and the get_haircut methods? You ll need at least one print statement in each routine, but be careful: they both should run at more-or-less the same time. One barber is okay, but how many customers will you model? Will you just throw a bunch of customers at the barbershop all at once? This might not test your code very well. How long will the haircut last? You can implement waiting with a busy loop, such as The goals are: for i = 1 to 100.. want to yield here?... endfor Page 2

(1) Practice creating a KPL class from scratch (2) Practice testing your code (3) Gain experience with semaphores, mutex locks, and thread synchronization Task 2: The Gaming Parlor Problem Here is the problem scenario: groups of customers come in to a gaming parlor to play games. They go to the front desk to obtain one or more dice, which are used by the group while they are playing their game, and then returned to the front desk. The front desk is in charge of lending out the dice and collecting them after each game is finished. The gaming parlor owns only 8 dice, which are available at the front desk before the first group comes in. The customers can play the following games. Listed after each game in parentheses is the number of dice required to play that game. Backgammon (4) Risk (5) Monopoly (2) Pictionary (1) You should model the front desk as a monitor with the following entry methods: Request (numberofdice: int) Return (numberofdice: int) Model each group of customers as a thread. When a group is ready to play, it must obtain the necessary number of dice. If the required number of dice is not available, then the group (i.e., the thread) must wait. You might use a condition variable that more dice have become available. You should model the following eight different groups. Each group plays one game, as shown below, but each group plays its game 5 times. Each group must return their dice after each game and then reacquire the dice before playing again. A Backgammon B Backgammon C Risk D Risk E Monopoly F Monopoly G Pictionary H Pictionary Page 3

To simulate playing the game, simply call the Yield method. This problem is a generalization of the problem of resource allocation where (1) there are a number of resources (dice) but each is identical; (2) every requesting process needs a one or more units of the resource, (3) each requesting thread knows how many units it will need before requesting any units and that info is included in the request, (4) all units are returned before any further requests are made. In a monitor, each method is either an entry method or a local method. A monitor will always have one Mutex lock, which is associated with entering the monitor. Every monitor will also have zero or more condition variables, as required by the particular application. An entry method must always begin by locking the monitor s mutex. Before it returns, it must always unlock the mutex. Within the entry method, the code may Signal some of the condition variables and may Wait on condition variables. (In some applications, it may make sense to Broadcast to a condition variable.) However, once inside of an entry method, the code should never touch the monitor s mutex lock, or try to look inside the condition variables. You should model the front desk as a monitor and you should use condition variables instead of semaphores. If deadlock occurs, the program will freeze up and some requests for dice will go unsatisfied forever. This is a disaster! Regardless of the order in which the groups make their requests, your solution should be structured such that deadlock can never occur. Your solution must not be subject to any race conditions. In other words, regardless of the order in which the groups make their requests and return their dice, each die must never be allocated to more than one group at a time. It should never be the case that groups are allowed to proceed when there are too few dice. Likewise, if a group has returned its dice, other groups which are waiting must be allowed to proceed once enough dice have become available. Starvation can occur if it is possible that one thread can be delayed infinitely by other threads requests. If the game parlor problem is extended to assume that each group will continue to play game after game forever, then starvation might be possible, if you are not careful in your solution. If some group X comes in requesting a lot of dice, it will be made to wait until enough dice are available. If it is possible that other groups can come in, request a small number of dice, and have their requests granted, then group X might get delayed forever, since there are never enough dice at the front desk at once to satisfy group X s needs. In other words, it might be possible for starvation of X to occur. In your solution starvation should not be possible. (Of course, with each group limited to playing only 5 games, all the outstanding dice will always get returned eventually and starvation can never occur, but your solution should also avoid starvation in the presence of a never-ending sequence of Request and Return operations.) To verify that your code is working, please insert print statements to produce output like this... Page 4

Initializing Thread Scheduler... A requests 4 A proceeds with 4 ------------------------------Number of dice now avail = 4 B requests 4 ------------------------------Number of dice now avail = 4 B proceeds with 4 ------------------------------Number of dice now avail = 0 D requests 5 ------------------------------Number of dice now avail = 0 E requests 2 ------------------------------Number of dice now avail = 0 A releases and adds back 4 ------------------------------Number of dice now avail = 4 B releases and adds back 4 C requests 5 H requests 1 B requests 4 D proceeds with 5 ------------------------------Number of dice now avail = 3...etc... This output makes it fairly easy to see what the program is doing and verify that it is correct. Below is a method you should use to produce your output. Feel free to copy this method straight into your program. behavior GameParlor... Other methods... method Print (str: String, count: int) -- -- This method prints the current thread's name and the arguments. -- It also prints the current number of dice available. -- print (currentthread.name) print (" ") print (str) print (" ") printint (count) nl () print ("------------------------------Number of dice now avail = ") printint (numberdiceavail) nl () endmethod Page 5

endbehavior This method would be called in several places... At the beginning of the Request method: self.print ("requests", numneeded) At the end of the Request method: self.print ("proceeds with", numneeded) In the Return method: self.print ("releases and adds back", numreturned) What to Hand In Please submit hardcopy of the following files: Main.h Main.c Also hand in hardcopy showing your output for both tasks. For the Sleeping Barber problem, I am not providing any testing material; you ll have to devise some tests on your own. Please hand in something showing how you have tested your code. In LARGE BLOCK LETTERS, write your full name on the first page. PLEASE STAPLE ALL PAGES! Basis for Grading In this course, the code you submit will be graded on both correctness and style. Correctness means that the code must work right and not contain bugs. Style means that the code must be neat, well commented, well organized, and easy to read. In style, your code should match the code we are distributing to you. The indentation should be the same and the degree of commenting should be the same. Page 6