Synchronization and Scheduling

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Ch 7 B.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
1 Concurrency: Deadlock and Starvation Chapter 6.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
CS510 Concurrent Systems Introduction to Concurrency.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Thread Synchronization including Mutual Exclusion In Java synchronized keyword Monitor or Lock with conditions Semaphore.
CSE 451: Operating Systems Section 5 Synchronization.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
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.
1 COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Overview Dr. Xiao Qin Auburn University
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.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
CS703 - Advanced Operating Systems
CSE 120 Principles of Operating
Thread Synchronization
CPS110: Reader-writer locks
CS703 – Advanced Operating Systems
COT 4600 Operating Systems Fall 2009
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Process Synchronization
CS533 Concepts of Operating Systems Class 3
Semaphores and Condition Variables
Monitors, Condition Variables, and Readers-Writers
Operating Systems CMPSC 473
CSE 120 Principles of Operating
Monitors.
Section 5 Project 1 Recap Synchronization primitives
Chapter 5: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Monitors Chapter 7.
Thread Synchronization
Threading And Parallel Programming Constructs
Process Synchronization
CS703 - Advanced Operating Systems
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Critical section problem
Another Means Of Thread Synchronization
Monitors Chapter 7.
CS533 Concepts of Operating Systems Class 3
Monitors Chapter 7.
Thread Synchronization including Mutual Exclusion
Chapter 6: Synchronization Tools
Synchronization: Monitors
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Process/Thread Synchronization (Part 2)
Presentation transcript:

Synchronization and Scheduling Section 6 Synchronization and Scheduling February 24th, 2017 Taught by Joshua Don

Locks Synchronization primitive Provides mutual exclusion to a critical section/shared resource Only one thread may own the lock at a time. Trying to acquire a held lock causes a thread to block. Has a holder -> implies ownership Locking mechanism struct lock lock; lock_init(&lock); lock_acquire(&lock); // critical section lock_release(&lock);

Semaphores Synchronization primitive Prevents resource contention Generalized mutex Signaling mechanism Initialized with a value. Can be ‘downed’ as many times as ‘value’ before a thread must block. Can be ‘upped’ by any thread struct semaphore sema; sema_init(&sema, 1); sema_down(&sema); // restricted mutual access sema_up(&sema);

Semaphores Continued Should you ever try to read the current value of a semaphore directly? No; it creates a race condition, the value could change right after you read it

When to use: lock vs. sema(1)?

Lock vs Sema(1) Thread A Thread B A lock is a locking mechanism, while a semaphore is a signaling mechanism A semaphore can be ‘upped’ by any thread, whereas a lock can only be released by its holder struct semaphore event; sema_init(&event, 0); Thread A Thread B // wait for event sema_down(&event); handleEvent(); createEvent(); // signal that event has occurred sema_up(&event);

Lock vs sema(1) continued In Pintos:thread.c, this exact signaling behavior is used for the idle thread! Check out the use of struct semaphore idle_started;

Monitors NOT a synchronization primitive Composed of a lock and one more condition variables Condition variables: A list of threads waiting for an event to occur There is no such thing as a ‘struct monitor’ (unless you make one!) Must acquire the lock before checking the condition! A thread acquires the monitor lock Check if some condition is not met, if so call wait(&cond, &mutex) – atomically releases the lock and adds the thread to the condition variable Another thread acquires the monitor lock The thread changes the original condition that the threads on cond are waiting for. Can now either signal cond (wake up one waiter) or broadcast (wake up all)

Monitor example struct lock mutex; struct cond_t space_available; struct cond_t data_available; AddToQueue(item) { lock_acquire(&mutex); while (queue.isFull()) { cond_wait(&space_available, &mutex); } queue.push_back(item); cond_signal(&data_available); lock_release(&mutex); RemoveFromQueue() { lock_acquire(&mutex); while (queue.isEmpty()) { cond_wait(&data_available, &mutex); } queue.pop(); cond_signal(&space_available); lock_release(&mutex);