18/02/08Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:

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.
Barber Shop 1 Barber Customer Queue. 2 C1C2C3 Barber Before Opening Customers waiting in queue.
Ch 7 B.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Classic Synchronization Problems
矩陣乘法 實作矩陣乘法 利用 threads 來加速運算速度 – Matrix1 row x Matrix2 column = Ans (x,y) Matrix 1Matrix 2Answer.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.
1 Thursday, June 22, 2006 "I think I've got the hang of it now.... :w :q :wq :wq! ^d X exit ^X^C ~. ^[x X Q :quitbye CtrlAltDel ~~q :~q logout save/quit.
1 CS 333 Introduction to Operating Systems Class 5 – Semaphores and Classical Synchronization Problems Jonathan Walpole Computer Science Portland State.
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
Monitors CSCI 444/544 Operating Systems Fall 2008.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Jonathan Walpole Computer Science Portland State University
CS Introduction to Operating Systems
1 Sleeping Barber Problem There is one barber, and n chairs for waiting customers If there are no customers, then the barber sits in his chair and sleeps.
CY2003 Computer Systems Lecture 11 Review Lecture.
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 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Operating Systems Review for Chap.6 & 7 Hung Q. Ngo KyungHee University Spring 2009
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Homework Assignment #2 J. H. Wang Nov. 1, 2013.
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.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Homework Assignment #2 J. H. Wang Oct. 17, 2007.
Synchronization, part 3 Monitors, classical sync. problems
CENG334 Introduction to Operating Systems
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION3 OPERATING SYSTEMS.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CSE 451 Section Thursday, October 30. Questions from Lecture?
CPS110: Threads review and wrap up Landon Cox February 11, 2009.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Homework Assignment #2 J. H. Wang Nov. 1, Homework #2 Chap.5: 5.8, 5.19, 5.22 Chap.6: 6.11, 6.12, *6.35 (Optional: End-of-chapter project for Chap.
B. B. Karki, LSU 0.1 CSC 4103: Operating Systems CSC 4103: Operating Systems Spring 2013 Bijaya B Karki and Coretta Douglas Wednesday 5:00 PM to 7:50 PM.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002.
CS 3340 Windows Programming. Course Evaluation Course Outcomes Not about the instructor Do it Today! 2.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
CENG334 Introduction to Operating Systems
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.
Dining Philosophers Five philosophers sit around a table
CPS110: Threads review and wrap up
PARALLEL PROGRAM CHALLENGES
Synchronization, part 3 Monitors, classical sync. problems
CS 3340 Windows Programming
2.4 Classic IPC Problems Dining philosophers Readers and writers
Chapter 5: Process Synchronization
Principles of Software Development
Synchronization, part 3 Monitors, classical sync. problems
CS399 New Beginnings Jonathan Walpole.
Critical section problem
CSE 451: Operating Systems Winter 2004 Module 7+ Monitor Supplement
Barbershop Example We can simulate a barbershop using semaphores.
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CS510 Operating System Foundations
February 5, 2004 Adrienne Noble
More IPC B.Ramamurthy 4/15/2019.
CMPE 135: Object-Oriented Analysis and Design April 30 Class Meeting
CENG334 Introduction to Operating Systems
CS 144 Advanced C++ Programming May 7 Class Meeting
Synchronization, part 3 Monitors, classical sync. problems
Presentation transcript:

18/02/08Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL: Review Topics

2 The barbershop problem A barbershop consists of a waiting room with n chairs, and the barber room containing the barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy, but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers. The Little book of Semaphores

3 The barbershop problem A barbershop consists of a waiting room with n chairs, and the barber room containing the barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy, but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers. Customer threads should invoke a function named getHairCut. If a customer thread arrives when the shop is full, it can invoke balk, which does not return. Barber threads should invoke cutHair. When the barber invokes cutHair there should be exactly one thread invoking getHairCut concurrently.

4 Hint customers counts the number of customers in the shop; it is protected by mutex. The barber waits on customer until a customer enters the shop, then the customer waits on barber until the barber signals him to take a seat. customers = 0; mutex = Semaphore(1); customer = Semaphore(0); barber = Semaphore(0); n =..; // number of seats

5 Barber customers = 0; mutex = Semaphore(1); customer = Semaphore(0); barber = Semaphore(0); n =..; // number of seats barberFunction{ while(1){ customer.down(); barber.up(); cutHair(); } Each time a customer signals, the barber wakes, signals barber, and gives one hair cut. If another customer arrives while the barber is busy, then on the next iteration the barber will pass the customer semaphore without sleeping. customer.down() means “wait for a customer”.

6 Customer Every notifyAll() will cause all threads to wake up and re-check the empty or full condition. Could be inefficient if a lot of threads are involved... customers = 0; mutex = Semaphore(1); customer = Semaphore(0); barber = Semaphore(0); n =..; // number of seats customerFunction(){ mutex.down(); if (customers == n+1){ mutex.up(); balk(); } customers++; mutex.up(); customer.up(); barber.down(); getHairCut(); mutex.down(); customers--; mutex.up(); } If there are n customers in the waiting room and one in the barber chair, then the shop is full and any customers that arrive immediately invoke balk. Otherwise each customer signals customer and waits on barber. barberFunction{ while(1){ customer.down(); barber.up(); cutHair(); }