Operating Systems Review for Chap.6 & 7 Hung Q. Ngo KyungHee University Spring 2009

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems Part III: Process Management (Process Synchronization)
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
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: 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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
What we will cover… Process Synchronization Basic Concepts
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
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.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
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.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
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.
COSC 3407: Operating Systems Lecture 7: Implementing Mutual Exclusion.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Homework Assignment #2 J. H. Wang Oct. 17, 2007.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
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 ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
Process Synchronization: Semaphores
Background on the need for Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Module 7a: Classic Synchronization
Critical section problem
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Monitor Giving credit where it is due:
Chapter 6: Synchronization Tools
Presentation transcript:

Operating Systems Review for Chap.6 & 7 Hung Q. Ngo KyungHee University Spring

2 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.1

3 Operating SystemsHung Q. Ngo, KHU Spring’09 Solution to Critical-Section Problem 1.Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2.Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3.Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes

4 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.4 Mutual exclusion using Swap( ) with bounded waiting swap (&address, register) { /* x86 */ temp = M[address]; M[address] = register; register = temp; }

5 Operating SystemsHung Q. Ngo, KHU Spring’09 Lock solution using Swap Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key. Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section lock = FALSE; // remainder section } while ( TRUE);

6 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.4 Solution: similar to fig using TestAndSet( )

7 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.7 Implement semaphore in multiprocessor environments using TestAndSet( ) with minimal busy waiting Hint: Similar to “better lock” but here “value” is more than Free/Busy Use a guard shared variable for acquiring critical section to modify the semaphore value Put waiting threads to sleep

8 Operating SystemsHung Q. Ngo, KHU Spring’09 Better Locks using test&set Can we build test&set locks without busy-waiting? –Can’t entirely, but can minimize! –Idea: only busy-wait to atomically check lock value Note: sleep has to be sure to reset the guard variable –Why can’t we do it just before or just after the sleep? Release() { // Short busy-wait time while (test&set(guard)); if anyone on wait queue { take thread off wait queue Place on ready queue; } else { value = FREE; } guard = 0; int guard = 0; int value = FREE; Acquire() { // Short busy-wait time while (test&set(guard)); if (value == BUSY) { put thread on wait queue; go to sleep() & guard = 0; } else { value = BUSY; guard = 0; } }

9 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.8 The Sleeping-Barber Problem: A barbershop consists of awaiting room with n chairs and a barber room with one 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.

10 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.8 Semaphore mutex = 1; Semaphore customers = 0; Semaphore haircut = 0; int waiting = 0 void customer() //khách đ ế n c ắ t tóc { wait( mutex ); if( waiting == N ) //n ế u s ố khách đ ợ i = N thì r ờ i kh ỏ i ti ệ m { signal( mutex ); return ; } waiting ++; //tăng s ố khách đ ợ i signal( mutex ); signal(customers); //đánh th ứ c barber n ế u đang ng ủ wait(haircut); //đang c ắ t nh ư ng ch ư a xong (ch ờ trên gh ế c ắ t tóc) }

11 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.8 Semaphore mutex = 1; Semaphore customers = 0; Semaphore haircut = 0; int waiting = 0 void barber() //th ợ c ắ t tóc { while( 1 ) //c ắ t liên t ụ c, h ế t khách này đ ế n khách khác { wait( customers ); //n ế u không có khách, barber s ẽ ng ủ wait( mutex ); waiting --; //gi ả m 1 khách đ ợ i signal(mutex); cut_hair(); signal(haircut); //cho khách đã c ắ t tóc xong r ờ i kh ỏ i ti ệ m } }

12 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.9 & 6.10 Write a bounded-buffer monitor in which the buffers (portions) are embedded within the monitor itself. Problem with previous solution? Modify the solution to improve.

13 Operating SystemsHung Q. Ngo, KHU Spring’09 monitor bounded_buffer { int items[MAX_ITEMS]; int numItems = 0; condition full, empty; void produce(int v) { while (numItems == MAX_ITEMS) full.wait(); items[numItems++] = v; empty.signal(); } int consume() { int retVal; while (numItems == 0) empty.wait(); retVal = items[--numItems]; full.signal(); return retVal; } }

14 Operating SystemsHung Q. Ngo, KHU Spring’09 Producing Water! MakeH() { while (true) Make-Hydro(); // t ạ o 1 nguyên t ử H } MakeO() { while (true) Make-Oxy(); //t ạ o 1 nguyên t ử O } /* Ti ế n trình MakeWater ho ạ t đ ộ ng đ ồ ng hành v ớ i các ti ế n trình MakeH, MakeO, ch ờ có đ ủ 2 H và 1 O đ ể t ạ o H2O */ MakeWater() { while (True) Make-Water(); //T ạ o 1 phân t ử H2O }

15 Operating SystemsHung Q. Ngo, KHU Spring’09 Producing Water! Semaphore s1=0, s2=0; MakeH() { while (true) { Make-Hydro(); signal(s1);} } MakeO() { while (true) { Make-Oxy(); signal(s2);} } /* Ti ế n trình MakeWater ho ạ t đ ộ ng đ ồ ng hành v ớ i các ti ế n trình MakeH, MakeO, ch ờ có đ ủ 2 H và 1 O đ ể t ạ o H2O */ MakeWater() { while (True) { wait(s1); wait(s1); wait(s2); Make-Water(); } }

16 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 7.10 A single-lane bridge connects the two Vermont villages of North Tunbridge and South Tunbridge. Farmers in the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Vermont farmers are stubborn and are unable to back up.) Using semaphores, design an algorithm that prevents deadlock. Hint: Initially, do not be concerned about starvation (the situation in which northbound farmers prevent southbound farmers from using the bridge, or vice versa).

17 Operating SystemsHung Q. Ngo, KHU Spring’09 Simple solution semaphore ok_to_cross = 1; void enter bridge() { ok to cross.wait(); } void exit bridge() { ok to cross.signal(); }

18 Operating SystemsHung Q. Ngo, KHU Spring’09 Solution without deadlock & starvation

19 Operating SystemsHung Q. Ngo, KHU Spring’09 Want More? :D