Principles of Software Development

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
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.
Barber Shop 1 Barber Customer Queue. 2 C1C2C3 Barber Before Opening Customers waiting in queue.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Classic Synchronization Problems
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Fundamentals of Python: From First Programs Through Data Structures
18/02/08Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
5.4 Which of the following scheduling algorithms could result in starvation? a. First-come, first-served b. Shortest job first c. Round robin d. Priority.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Internet Software Development More stuff on Threads Paul Krause.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Nachos Phase 1 Code -Hints and Comments
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
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...
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads.
CSCI-100 Introduction to Computing
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
1 Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread... amount = … invoke delegate transact.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
CS 3340 Windows Programming. Course Evaluation Course Outcomes Not about the instructor Do it Today! 2.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Shlomo Hershkop Basics overview. Shlomo Hershkop Basic Review - Overview Practice coding Practice coding finger guessing game finger guessing.
Principles of Software Development
Multithreading / Concurrency
Threaded Programming in Python
Lecture 14 Throwing Custom Exceptions
Announcement server: server.o common.o $(CC) -o server server.o common.o $(LIBS) -lsock -lpthread and change it to: server: server.o common.o threadpool.o.
Synchronization, part 3 Monitors, classical sync. problems
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
CS 3340 Windows Programming
System Programming Practical Session 7
Multithreading Chapter 23.
Lecture 14: Pthreads Mutex and Condition Variables
Principles of Software Development
Introduction to TouchDevelop
Multithreading.
Multithreaded Programming
Critical section problem
Tonga Institute of Higher Education
Barbershop Example We can simulate a barbershop using semaphores.
Component-Based Software Engineering
Lecture 14: Pthreads Mutex and Condition Variables
Threads and Multithreading
CS 144 Advanced C++ Programming May 7 Class Meeting
EECE.4810/EECE.5730 Operating Systems
Software Engineering and Architecture
CSE 542: Operating Systems
More concurrency issues
Presentation transcript:

Principles of Software Development Sleeping Barber CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

Outline Sleeping Barber USC CSCI 201L

Sleeping Barber Overview The Sleeping Barber problem contains one barber and a number of customers There are a certain number of waiting seats If a customer enters and there is a seat available, he will wait If a customer enters and there is no seat available, he will leave When the barber isn’t cutting someone’s hair, he sits in his barber chair and sleeps If the barber is sleeping when a customer enters, he must wake the barber up USC CSCI 201L

Program Write a solution to the Sleeping Barber problem. You will need to utilize synchronization and conditions. USC CSCI 201L

Program Steps – main Method The SleepingBarber will be the main class, so create the SleepingBarber class with a main method The main method will instantiate the SleepingBarber Then create a certain number of Customer threads that arrive at random times Have the program sleep for a random amount of time between customer arrivals Have the main method wait to finish executing until all of the Customer threads have finished Print out that there are no more customers, then wake up the barber if he is sleeping so he can go home for the day USC CSCI 201L

Program Steps – SleepingBarber The SleepingBarber constructor will initialize some member variables Total number of seats in the waiting room Total number of customers who will be coming into the barber shop A boolean variable indicating whether more customers will be coming into the barber shop An ArrayList of customers who are currently waiting to get their hair cut A lock on the barber with a condition to signify whether he is sleeping Start the SleepingBarber thread at the end of the constructor The run() method of the SleepingBarber will perform the hair cutting as follows Continue looping as long as more customers will be coming into the barber shop As long as there are any customers waiting Get the customer who has been waiting the longest and cut his hair for a certain amount of time Let the customer know when you have started and when you have finished cutting his hair If there are no customers waiting, go to sleep (i.e. get the lock and await() on the sleeping condition) Don’t forget to release the lock in a finally block Create a method to add a customer to the waiting list If the number of waiting customers equals the number of seats in the waiting room when a new customer arrives, have the customer leave Otherwise, add the customer to the waiting list Print out all of the customers currently waiting Make sure to synchronize this method so that customers cannot be added to the list while iterating through it Create a method customers can call to wake up the barber Acquire the lock, signal on the sleeping condition, then release the lock USC CSCI 201L

Program Steps – Customer The Customer constructor will initialize some member variables A variable representing the customer name An instance of the SleepingBarber A lock on the customer with a condition to signify whether he is getting a haircut Create a method that allows the barber to get the customer’s name Create a method the barber will call when he starts cutting the customer’s hair This method should just print out that the customer is getting his hair cut Create a method the barber will call when he is done cutting the customer’s hair This method will print out that the customer is done getting his hair cut It also should signal the condition signifying that he is getting his hair cut The run() method of the Customer will perform the following tasks Add the customer to the barber’s waiting list If there are no seats available in the waiting area, print out a message letting the user know the customer is leaving without getting his hair cut Wake up the barber if he is sleeping Note: There is nothing wrong with signaling a condition even if no thread is waiting on it Get the customer lock and wait on the haircut condition Even though the customer may not be getting a haircut yet, the barber will call the method above when he starts cutting his hair, then the barber will call the other method above when he is done Don’t forget to release the lock in the finally block Print out a message that the customer is leaving USC CSCI 201L

Program Steps – Util USC CSCI 201L The Util class will be used for printing Create a static method called printMessage that takes a String as a parameter This method will print out a formatted date/time followed by a dash then the message To format a date/time, use the java.util.Calendar class Get an instance of the Calendar, then get the data out of the Calendar so that it can be formatted as follows yyyy-MM-dd HH:mm:ss.SSS For example: 2016-10-31 11:30:21.534 This method should be called from the SleepingBarber and Customer classes whenever something needs to be printed USC CSCI 201L