Auburn University http://www.eng.auburn.edu/~xqin COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice Dr. Xiao Qin.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
Threads Cannot be Implemented As a Library Andrew Hobbs.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
Classical Problems of Concurrency
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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.
Process Synchronization
Chapter 2.3 : Interprocess Communication
CS444/CS544 Operating Systems Classic Synchronization Problems 3/5/2007 Prof. Searleman
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Process Synchronization I Nov 27, 2007 CPE Operating Systems
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Java Thread and Memory Model
Today’s Agenda  HW #2 Out  Replay Semaphore and Lock  Advanced Locking Advanced Topics in Software Engineering 1.
Lab assignment 1: Synchronization
Process Synchronization Monitors Simulation of Monitors Checkpoint and Recovery Monitors Simulation of Monitors Checkpoint and Recovery Topics:
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Practice Chapter Five.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Chapter 71 Monitor for Reader/Writers  Synchronizes access if all processes follow.  If some don’t go through monitor and database is accessed directly,
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 3: Adding System Calls to OS/161 Dr. Xiao Qin Auburn University.
1 COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Overview Dr. Xiao Qin Auburn University
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
COMP7330/7336 Advanced Parallel and Distributed Computing OpenMP: Programming Model Dr. Xiao Qin Auburn University
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
COMP7330/7336 Advanced Parallel and Distributed Computing Pthread RW Locks – An Application Dr. Xiao Qin Auburn University
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
C++ Lesson 1.
Deadlock and Starvation
Chapter 1.2 Introduction to C++ Programming
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 5: Process Synchronization – Part II
Deadlock and Starvation
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Pthread Mutex Dr. Xiao Qin Auburn University.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
Chapter 6-7: Process Synchronization
CSE451 Basic Synchronization Spring 2001
                                                                                                                                                                 
Chapter 5: Process Synchronization (Con’t)
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Lecture 22 Syed Mansoor Sarwar
Synchronization Hank Levy 1.
Thread Synchronization
Realtime System Fundamentals
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Realtime System Fundamentals
Monitor Giving credit where it is due:
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Synchronization Hank Levy 1.
Chapter 7: Synchronization Examples
N-Process Barrier (error 1)
Variables and Constants
Presentation transcript:

Auburn University http://www.eng.auburn.edu/~xqin COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu

Synchronizations Among Cats and Mice Your task is to synchronize the eating habits of cats and mice, such that: No mouse ever gets eaten, and Neither the cats or the mice starve.

Specifications Two cat food dishes 6 cats and 2 mice Only 1 mouse or cat may eat at a given dish at any one time If a cat is eating at either dish, a mouse attempting to eat from the other dish will be seen and therefore eaten When cats aren't eating, they will not see mice eating.

Understand the Problem Dish 1 Dish 2 If a cat “eats” dish 1 or dish 2 Then no mice If a mouse “eats” dish 1 or dish 2 Then no cat

How many possible cases? Case 1: Dish 1: No Cat/Mouse Dish2: No Cat/Mouse Case 2:Dish1: Cat i Dish2: No Cat Case 3: Dish1: Cat i Dish2: Cat j Case 4: Dish1: Cat i Dish2: Cat j Cat k waits Case 5: Dish1: Mouse 1 Dish2: No Mouse Case 6: Dish1: Mouse 1 Dish2: Mouse 2 Case 7: Dish1: Mouse 1 Dish2: Mouse 2 Cat i waits

How many semaphores? volatile bool all_dishes_available = true; semaphore done = 0; semaphore mutex = 1; semaphore dish_mutex = 1; semaphore cats_queue = 0; volatile int cats_wait_count = 0; volatile bool no_cat_eat = true; /*first cat*/ semaphore mice_queue = 0; volatile int mice_wait_count = 0; volatile bool no_mouse_eat = true; The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times. C does not support bool #define true 1 #define false 0 typedef char bool;

First Cat and No Mouse wait(mutex); If (all_dishes_available) { all_dishes_availalbe = flase; signal(cats_queue); /* let first cat in */ } cats_wait_count++; signal(mutex); wait(cats_queue); /*first cat in, other wait*/ If (no_cat_eat) { no_cat_eat = false; first_cat_eat = true; Else first_cat_eat = false; The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times. C does not support bool #define true 1 #define false 0 typedef char bool;

The first cat controls two dishes If (first_cat_eat) { wait(mutex); if (cat_wait_count > 1) { another_cat_each = true; signal(cats_queue); /*let another cat in*/ } signal(mutex); kprintf(“Cat in the kitch \n”); /* cat name */ The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times. C does not support bool #define true 1 #define false 0 typedef char bool;

All cats in the kitchen wait(dish_mutex); If (dish1_busy == false) { dish1_busy = true; mydish = 1; } Else { assert(dish2_busy == false); dish2_busy = true; mydish = 2; signal(dish_mutex); The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times. C does not support bool #define true 1 #define false 0 typedef char bool;