CSE 451: Operating Systems Section 5: Synchronization.

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 Part III: Process Management (Process Synchronization)
CAS3SH3 Midterm Review. The midterm 50 min, Friday, Feb 27 th Materials through CPU scheduling closed book, closed note Types of questions: True & False,
Chapter 6: Process Synchronization
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
CSE 451: Operating Systems Section 6 Project 2b; Midterm Review.
CS444/CS544 Operating Systems Synchronization 2/16/2007 Prof. Searleman
1 CSE451 – Section 4. 2 Reminders Project 2 parts 1,2,3 due next Thursday Threads, synchronization Today: Project 2 continued (parts 2,3) Synchronization.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Reminders Homework 3 due next Monday
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS533 Concepts of Operating Systems Class 3 Monitors.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Jonathan Walpole Computer Science Portland State University
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Reminders Homework 3 due Monday, Oct. 25 Synchronization Project 2 parts 1,2,3 due Tuesday, Oct. 26 Threads, synchronization Today: Project 2 continued.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
CS 153 Design of Operating Systems Spring 2015 Midterm Review.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSE 451: Operating Systems Section 5 Midterm review.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSE 451: Operating Systems Section 5 Synchronization.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Lecture 6 Page 1 CS 111 Summer 2013 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CSE 451 Section 4. 2 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, semaphor, condition var.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
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.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Section 5 Project 1 Recap Synchronization primitives
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Critical section problem
Implementing Mutual Exclusion
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSE 451 Section 1/27/2000.
CSE 153 Design of Operating Systems Winter 2019
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CSE 451: Operating Systems Section 5: Synchronization

Project 2a is due on Wednesday  Review the sthread code that we give you  If a function is there, then there is probably a good reason why  Remember to write more test cases  test-burgers : your shell may limit the number of threads you can run  ulimit –u  Has anybody found bugs in the 64-bit code yet? 4/12/2012 2

Synchronization 4/12/2012 3

Synchronization support  Processor level:  Disable / enable interrupts  Atomic instructions  Operating system / library level:  Special variables: mutexes, semaphores, condition variables  Programming language level:  Monitors, Java synchronized methods 4/12/2012 4

Disabling / enabling interrupts  Prevents context-switches during execution of critical sections  Sometimes necessary  Many pitfalls 4/12/ Thread A: disable_irq() critical_section( ) enable_irq() Thread B: disable_irq() critical_section( ) enable_irq()

Processor support  Atomic instructions:  test-and-set  compare-exchange (x86)  Use these to implement higher-level primitives  E.g. test-and-set on x86 (given to you for part 5) is written using compare-exchange  You’ll use test-and-set when implementing mutexes and condition variables (part 5) 4/12/2012 6

Processor support  Test-and-set using compare-exchange: compare_exchange(lock_t *x, int y, int z): if(*x == y) *x = z; return y; else return *x; } test_and_set(lock_t *lock) { ??? } 4/12/2012 7

Processor support  Test-and-set using compare-exchange: compare_exchange(lock_t *x, int y, int z): if(*x == y) *x = z; return y; else return *x; } test_and_set(lock_t *lock) { compare_exchange(lock, 0, 1); } 4/12/2012 8

Preemption  You will need to use test-and-set and interrupt disabling in part 5 of project 2 (preemption)  You can start thinking about this while completing the code for the first 2 parts: where are the critical sections where I shouldn’t be interrupted? 4/12/2012 9

Semaphores  Semaphore = a special variable  Manipulated atomically via two operations  P (wait): tries to decrement semaphore  V (signal): increments semaphore  Has a queue of waiting threads  If execute wait() and semaphore is available, continue  If not, block on the waiting queue  signal() unblocks a thread on queue 4/12/

Mutexes  What is a mutex?  A binary semaphore (semaphore initialized with value 1)  Why use a mutex rather than a low-level lock?  Threads wait on a mutex by blocking, rather than spinning 4/12/

How not to implement mutexes  Definitely not like this: void sthread_user_mutex_lock( sthread_mutex_t lock) { while (lock->held) { ; } lock->held = true; }  And also not like this: void sthread_user_mutex_lock( sthread_mutex_t lock) { while (lock->held) { yield(); } lock->held = true; } 4/12/

Condition variables  Let threads block until a certain event or condition occurs (rather than polling)  Associated with some logical condition in a program: sthread_mutex_lock(lock); while (x <= y) { sthread_cond_wait(cond, lock); } sthread_mutex_unlock(lock); 4/12/

Condition variables  Operations:  wait : sleep on wait queue until event happens  signal : wake up one thread on wait queue  broadcast : wake up all threads on wait queue  signal or broadcast is called explicitly by the application when the event / condition occurs 4/12/

Condition variables sthread_user_cond_wait(sthread_cond_t cond, sthread_mutex_t lock)  Should do the following atomically:  Release the lock (to allow someone else to get in)  Add current thread to the waiters for cond  Block thread until awoken (by signal/broadcast)  So, application must acquire lock before calling wait() !  Read man page for pthread_cond_[wait|signal|broadcast] 4/12/

Example synchronization problem  Late-Night Pizza  A group of students study for CSE 451 exam  Can only study while eating pizza  If a student finds pizza is gone, the student goes to sleep until another pizza arrives  First student to discover pizza is gone orders a new one  Each pizza has S slices 4/12/

Late-night pizza  Each student thread executes the following: while (must_study) { pick up a piece of pizza; study while eating the pizza; } 4/12/

Late-night pizza  Need to synchronize student threads and pizza delivery thread  Avoid deadlock  When out of pizza, order it exactly once  No piece of pizza may be consumed by more than one student 4/12/

Semaphore / mutex solution  Shared data: semaphore_t pizza;//Number of //available pizza //resources; //init to 0 semaphore_t deliver;//init to 1 int num_slices = 0; mutex_t mutex;//protects accesses //to num_slices 4/12/

student_thread { while (must_study) { wait(pizza); acquire(mutex); num_slices--; if (num_slices==0) signal(deliver); release(mutex); study(); } delivery_guy_thread { while (employed) { wait(deliver); make_pizza(); acquire(mutex); num_slices=S; release(mutex); for (i=0;i<S;i++) signal(pizza); } 20

Condition variable solution  Shared data: int slices=0; bool has_been_ordered; Condition order;//an order has //been placed Condition deliver;//a delivery has //been made Lock mutex;//protects //“slices”; //associated with //both Condition //variables 4/12/

Student() { while(diligent) { mutex.lock(); if (slices > 0) { slices--; } else { if(!has_been_ordered){ order.signal(mutex); has_been_ordered = true; } while (slices <= 0) { deliver.wait(mutex); } slices--; } mutex.unlock(); Study(); } DeliveryGuy() { while(employed) { mutex.lock(); order.wait(mutex); makePizza(); slices = S; has_been_ordered = false; mutex.unlock(); deliver.broadcast(); } 22

Monitors  An object that allows one thread inside at a time  Contain a lock and some condition variables  Condition variables used to allow other threads to access the monitor while one thread waits for an event to occur 4/12/

Monitors 10/28/10 24 shared data f() { … } g() { … } h() { … } Entry set: queue of threads trying to enter the monitor CV operations (procedures) At most one thread in monitor at a time CV Wait sets

Midterm! 11/4/10 25

The kernel  Kernel mode vs user mode  How these modes differ conceptually and from the CPU's point of view  How we switch between the two  Interrupts 11/4/10 26

System calls  What they are  What they do  How they do it  What hardware is involved  Who uses them and when 11/4/10 27

Processes and threads  Kernel processes, kernel threads, and user threads  How these differ from one another  Context switching  Process and thread states  fork, exec, wait 11/4/10 28

Synchronization  Critical sections  Locks and atomic instructions  Mutexes, semaphores, and condition variables  Monitors and how they are implemented  Ways to detect / avoid deadlock 11/4/10 29

Scheduling  Different scheduling algorithms and their tradeoffs  Average response time, various “laws”  Starvation  Cooperative vs. preemptive scheduling 11/4/10 30

Tips  Focus on lecture slides  Review textbook, section slides and project writeups to emphasize key concepts and fill in gaps  On Monday, when taking the exam:  Arrive early  Focus on key points  Work quickly; finish easy problems first 11/4/10 31

4/12/