Concurrent Programming Issues & Readers/Writers

Similar documents
Locks and Condition Variables Recap. Introducing Monitors. Hoare Monitors: Semantics. Coke Machine Example. Locks. Condition variables

Semaphores and Monitors: High-level Synchronization Constructs

Lecture #10: Synchronization wrap up

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

r ~ c.. Q.) 0\ 7 < - \1") ::J - ::r 3 ::J,... ::J Q.) 0!:t. !:t. ::J ::J (/') C

9/17/12! Ion Stoica CS162 UCB Fall 2012!

February 23 rd, 2015 Prof. John Kubiatowicz

CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

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

5 Dining Philosophers. Template for Philosopher. Naive Solution. while (food available) /*pick up forks*/ eat; /*put down forks*/ think awhile;

Synchroniza+on. Today: Implementa+on issues

CS162 Operating Systems and Systems Programming Midterm Review"

2 Threads vs. Processes

Synchronization COMPSCI 386

Outline for Today. 5 Dining Philosophers. Template for Philosopher. Naive Solution. Objective: Administrative details:

What's wrong with Semaphores?

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Midterm 1, CSE 451, Winter 2001 (Prof. Steve Gribble)

Concept of a process

EECS 482 Introduction to Operating Systems

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

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

Synchronization for Concurrent Tasks

Introduction to Operating Systems

Today: Synchronization. Recap: Synchronization

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

Synchronization Classic Problems

Operating Systems ECE344

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Opera&ng Systems ECE344

CS 318 Principles of Operating Systems

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Lecture #7: Implementing Mutual Exclusion

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002

Last Class: Synchronization

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSE 153 Design of Operating Systems

Synchronization (Part 2) 1/40

Outline Part 2. Semaphores. Semaphore Usage. Objectives: Administrative details:

Dealing with Issues for Interprocess Communication

Topic 4: Synchronization with Semaphores

CS5460: Operating Systems

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

CSE Traditional Operating Systems deal with typical system software designed to be:

Chapter 6: Process Synchronization

Critical Section Problem: Example

SFDV3006 Concurrent Programming

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

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

CS Advanced Operating Systems Structures and Implementation Lecture 8. Synchronization Continued. Goals for Today. Synchronization Scheduling

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

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

Critical Section Problem: Example

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

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

CS 153 Design of Operating Systems Winter 2016

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

5. Synchronization. Operating System Concepts with Java 8th Edition Silberschatz, Galvin and Gagn

Last Class: Deadlocks. Today

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Concurrency: Signaling and Condition Variables

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Synchronization I. Jo, Heeseung

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

Part II Process Management Chapter 6: Process Synchronization

Lecture 9: Midterm Review

Midterm I February 28 th, 2019 CS162: Operating Systems and Systems Programming

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

CS3733: Operating Systems

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Process Synchronization

CS533 Concepts of Operating Systems. Jonathan Walpole

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

Concurrency and Synchronisation. Leonid Ryzhyk

THE AUSTRALIAN NATIONAL UNIVERSITY Mid-semester Quiz, Second Semester COMP2310 (Concurrent and Distributed Systems)

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Interprocess Communication By: Kaushik Vaghani

Models of concurrency & synchronization algorithms

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

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

Page 1. Recap: ATM Bank Server" Recap: Challenge of Threads"

Concurrency: a crash course

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

EECS 262a Advanced Topics in Computer Systems Lecture 5. Mesa/Transactions September 17 th, 2014

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

Thread Synchronization: Too Much Milk

Synchronization Spinlocks - Semaphores

Enforcing Mutual Exclusion Using Monitors

Lecture 8: September 30

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

CSCI 350 Ch. 5 Synchronization. Mark Redekopp Michael Shindler & Ramesh Govindan

Synchronization via Transactions

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Transcription:

Concurrent Programming Issues & Readers/Writers 1

Summary of Our Discussions! Developing and debugging concurrent programs is hard Ø Non-deterministic interleaving of instructions! Safety: isolation and atomicity! Scheduling: busy-waiting and blocking! Synchronization constructs Ø Locks: mutual exclusion Ø Condition variables: wait while holding a lock Ø Semaphores: Mutual exclusion (binary) and condition synchronization (counting)! How can you use these constructs effectively? Ø Develop and follow strict programming style/strategy 2

Programming Strategy! Decompose the problem into objects! Object-oriented style of programming Ø Identify shared chunk of state Ø Encapsulate shared state and synchronization variables inside objects! Don t manipulate shared variables or synchronization variables along with the logic associated with a thread! Programs with race conditions always fail. Ø A. True, B. False 3

General Programming Strategy! Two step process! Threads: Ø Identify units of concurrency these are your threads Ø Identify chunks of shared state make each shared thing an object; identify methods for these objects (how will the thread access the objects?) Ø Write down the main loop for the thread! Shared objects: Ø Identify synchronization constructs Mutual exclusion vs. conditional synchronization Ø Create a lock/condition variable for each constraint Ø Develop the methods using locks and condition variables for coordination 4

Coding Style and Standards! Always do things the same way! Always use locks and condition variables! Always hold locks while operating on condition variables! Always acquire lock at the beginning of a procedure and release it at the end Ø If it does not make sense to do this à split your procedures further! Always use while to check conditions, not if while (predicate on state variable) { conditionvariableà wait(&lock); ;! (Almost) never sleep(), yield(), or islocked() in your code Ø Use condition variables to synchronize! Note that printf() internally uses locks, and may hide race conditions 5

Readers/Writers: A Complete Example! Motivation Ø Shared databases accesses Examples: bank accounts, airline seats,! Two types of users Ø Readers: Never modify data Ø Writers: read and modify data! Problem constraints Ø Using a single lock is too restrictive Allow multiple readers at the same time but only one writer at any time Ø Specific constraints Readers can access database when there are no writers Writers can access database when there are no readers/writers Only one thread can manipulate shared variables at any time 6

Readers/Writer: Solution Structure! Basic structure: two methods Database::Read() { Wait until no writers; Block any writers; Access database; Let in one writer or reader; Database::Write() { Wait until no readers/writers; Write database; Let all readers/writers in; 7

Solution Details Lock dblock; Condition dbavail; int reader = 0; bool writer = false; Public Database::Read() { dblock.lock(); while(writer) { dbavail.wait(); reader++; dblock.unlock(); Read database; dblock.lock(); reader--; if(reader == 0) { dbavail.singal(); dblock.unlock(); Public Database::Write() { dblock.lock(); while(reader > 0 writer){ dbavail.wait(); writer = true; dblock.unlock(); Write database; dblock.lock(); writer = false; dbavail.signalall(); dblock.unlock(); This solution favors 1. Readers 2. Writers 3. Neither, it is fair 8

Self- criticism can lead to self- understanding! Our solution works, but it favors readers over writers. Ø Any reader blocks all writers Ø All readers must finish before a writer can start Ø Last reader will wake any writer, but a writer will wake readers and writers (statistically which is more likely?) Ø If a writer exits and a reader goes next, then all readers that are waiting will get through! Are threads guaranteed to make progress? Ø A. Yes B. No 9

Readers/Writer: Using Monitors! Basic structure: two methods Database::Read() { Wait until no writers; Access database; Wake up waiting writers;! State variables Database::Write() { Wait until no readers/writers; Access database; Wake up waiting readers/writers; Class RWFairLock { AR = 0; // # of active readers AW = false; // is there an active writer public bool iread; Condition oktoread; Condition oktowrite; LinkedList<RWFairLock> q; Lock lock; 10

Solution Details: Readers Class RWFairLock { AR = 0; // # of active readers AW = false; // is there an active writer public bool iread; Condition oktoread; Condition oktowrite; LinkedList<RWFairLock> q; Lock lock; Private Database::StartRead() { lock.acquire(); iread = true; q.add(this); while (AW!q.peek().iRead) { oktoread.wait(&lock); AR++; lock.release(); Public Database::Read() { StartRead(); Access database; DoneRead(); Private Database::DoneRead() { lock.acquire(); AR--; q.remove(this); if(q.size() > 0) { if (q.peek().iread == false) { oktowrite.notify(); lock.release(); 11

Solution Details: Writers Class RWFairLock { AR = 0; // # of active readers AW = false; // is there an active writer public bool iread; Condition oktoread; Condition oktowrite; LinkedList<RWFairLock> q; Lock lock; Database::Write() { StartWrite(); Access database; DoneWrite(); Private Database::StartWrite() { lock.acquire(); iread = false; q.add(this); while (AW AR > 0 q.peek().isread) { oktowrite.wait(&lock); AW = true; lock.release(); Private Database::DoneWrite() { lock.acquire(); AW = false; q.remove(this); if(q.size() > 0) { if (q.peek().isread) { oktoread.notifyall(); else { oktowrite.notify(); lock.release(); 12

Summary! Allowing concurrent reader execution is a common concurrent programming pattern! Naïve implementations can starve writers! Bookkeeping to ensure fair queuing is tricky, but not impossible Ø A lot of effort to reason about all possible interleavings of operations 13