Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

Similar documents
Monitors; Software Transactional Memory

What's wrong with Semaphores?

Monitors; Software Transactional Memory

Lecture 8: September 30

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Synchronization Mechanisms

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

W4118 Operating Systems. Instructor: Junfeng Yang

Lecture 6 (cont.): Semaphores and Monitors

CSE 153 Design of Operating Systems

Concurrency and Synchronisation

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Concurrency and Synchronisation

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Synchroniza+on II COMS W4118

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

CSE 120 Principles of Operating Systems Spring 2016

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Reminder from last time

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

CHAPTER 6: PROCESS SYNCHRONIZATION

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Resource management. Real-Time Systems. Resource management. Resource management

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Transactional Memory: Architectural Support for Lock-Free Data Structures Maurice Herlihy and J. Eliot B. Moss ISCA 93

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Lecture 7: CVs & Scheduling

CSE 120 Principles of Operating Systems Spring 2016

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Concurrency. Chapter 5

Reminder from last time

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Lecture 7: Transactional Memory Intro. Topics: introduction to transactional memory, lazy implementation

1 Process Coordination

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

IT 540 Operating Systems ECE519 Advanced Operating Systems

Operating Systems. Lecture 8

CS 318 Principles of Operating Systems

Concurrent & Distributed Systems Supervision Exercises

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Chapter 5: Process Synchronization

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Reminder from last time

Dekker s algorithms for Semaphores Implementation. ALI TARIQ AL-KHAYYAT ( ) Submitted to : Prof. Dr. Huseyin Balik

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

CS 318 Principles of Operating Systems

Lecture 6. Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Classic Problems of Synchronization

CS Operating Systems

CS Operating Systems

Process Management And Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Introduction to OS Synchronization MOS 2.3

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Last Class: Synchronization

Multi-core Architecture and Programming

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Operating Systems ECE344

Dealing with Issues for Interprocess Communication

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Outline. Monitors. Barrier synchronization The sleeping barber problem Readers and Writers One-way tunnel. o Monitors in Java

Semaphores (by Dijkstra)

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Synchronization Classic Problems

Lecture 21: Transactional Memory. Topics: Hardware TM basics, different implementations

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS3733: Operating Systems

Opera&ng Systems ECE344

CS533 Concepts of Operating Systems. Jonathan Walpole

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

CMSC421: Principles of Operating Systems

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

CS 318 Principles of Operating Systems

C09: Process Synchronization

OpenMP and more Deadlock 2/16/18

Concurrency and Synchronisation. Leonid Ryzhyk

Lecture 6: Lazy Transactional Memory. Topics: TM semantics and implementation details of lazy TM

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Systèmes d Exploitation Avancés

Transcription:

Synchronization CSCI 5103 Operating Systems Monitors Other Approaches Instructor: Abhishek Chandra 2 3 Semaphore Usage wait(s) Critical section signal(s) Each process must call wait and signal in correct order What if we: Replace wait with signal? Replace signal with wait? Switch the order of wait and signal? Must also initialize the semaphore correctly 4 Bounded-Buffer Problem With Semaphores Semaphore mutex=1; item buffer[n]; Producer: while(1) produce(item); wait(slots); wait(mutex); insert(item, buffer); signal(mutex); signal(items); Semaphore items=0, slots=n; Consumer: while(1) wait(items); wait(mutex); remove(item, buffer); signal(mutex); signal(slots); consume(item); What if we switched wait(slots) and wait(mutex)? 1

Problems with Semaphores Programming complexity Difficult to use correctly Programmer has to be skilled in their usage, reason about the code Debugging difficulty Errors may not be reproducible Depends on order of execution Would like: high-level synchronization construct Don t have to worry about explicitly programming in synchronization Monitors High-level synchronization constructs Provided at language level Actual mechanisms hidden from programmer Easier to focus on application-level semantics 5 6 Monitor: Definition Monitor Syntax 7 Abstract data type (ADT) Encapsulates private data Public methods provided to operate on data Monitor is an ADT: Encapsulates shared variables Ensures mutual exclusion among its procedures Only one process can be active inside the monitor at a time 8 Monitor monitor_name //shared variable declarations procedure P1() procedure P2() initialization() 2

Monitor Execution At most one process executing inside the monitor Monitor has an entry queue of processes Processes waiting to enter the monitor When executing process exits: A process is selected from the entry queue Producer-Consumer Problem With Monitors Monitor ProducerConsumer; Producer: while(1) produce(item); ProducerConsumer.insert (item); Consumer: while(1) ProducerConsumer.remove (item); consume(item); 9 10 Producer-Consumer Problem With Monitors: Attempt 1 Monitor ProducerConsumer Buffer_type buffer; integer count; procedure insert(item) insert_item(item, buffer); count++; procedure remove(item) item=remove_item(buffer); count--; initialization() count=0; Problem We can achieve mutual exclusion But how to wait for a specific condition? E.g.: wait on full or empty buffer? Need another mechanism 11 12 3

Condition Variables Provide a way to wait and signal a condition Operations: wait and signal Each cond. var. has a wait queue wait: Process is suspended and immediately leaves the monitor signal: One suspended process is woken up No-op if no process waiting on cond. var. signalall: Wakes up all processes waiting on cond. var. Broadcast signal Bounded Buffer Problem With Monitors and Condition Variables integer count; Buffer_type buffer; procedure insert(item) if (count==n) slot_empty.wait(); insert_item(item, buffer); count++; if (count==1) item_avail.signal(); condition item_avail, slot_empty; procedure remove(item) if (count==0) item_avail.wait(); item=remove_item(buffer); count--; if (count==n-1) slot_empty.signal(); 13 14 Condition Variable Semantics When a process P signals and Q is woken up: Who gets to run in the monitor? Signal and wait (Hoare style): P blocks until Q leaves the monitor Signal and continue (Mesa style): Q blocks until P leaves the monitor What about the condition Q was waiting on? Signal and return: P exits monitor (returns from procedure) immediately after signal Monitor Implementation Language-provided construct Compiler has to enforce mutual exclusion Can be implemented using semaphores Some languages provide monitors: Concurrent Pascal, C#, Java Monitors in Java: Synchronized methods Wait and notify methods 15 16 4

Other Synchronization Approaches Transactional Memory Language annotations (OpenMP) Functional programming languages Transactional Memory Applies idea of transactions to memory reads/writes A set of operations is carried out as a transaction Committed: if no conflicts during execution Aborted: if read/write conflicts atomic read (x); write (y); 17 18 Transactional Memory: Usage Transactions specified using language or library support Compiler/runtime system/hardware implements the underlying concurrency/synchronization Lock-free shared data Each thread reads/writes data concurrently Values may have to be rolled back if conflict on memory locations Transactional Memory: Benefits Makes programming easier Avoids common problems such as deadlocks, priority inversion Better performance than locks (often): Optimistic concurrency control Fine-grained synchronization (memory address/word-level) 19 20 5

Transactional Memory: Implementation Software Tran. Mem. (STM): Implemented in software Requires compiler/runtime support Support in Java, C, C++ Hardware Tran. Mem. (HTM): Implemented in the hardware Extended cache coherence protocols OpenMP Supports language annotations to specify synchronization constructs such as critical sections E.g.: #pragma omp critical Compiler generates code for mutual exclusion However: Programmer still responsible for identifying critical sections in the code 21 22 Functional Programming Languages Do not have mutable state Variables cannot be overwritten (or modified in place) Every operation depends only on arguments, has no side effects E.g.: add(list, node) will create and return a new list (not modify list in place) Do not suffer from synchronization problems E.g.: Lisp, Scheme, Erlang, Scala 23 6