CENG334 Introduction to Operating Systems

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

CS 153 Design of Operating Systems Winter 2016

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

CS Operating Systems

CS Operating Systems

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Concurrency and Synchronisation. Leonid Ryzhyk

Interprocess Communication and Synchronization

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

CS 153 Design of Operating Systems Winter 2016

Dealing with Issues for Interprocess Communication

IV. Process Synchronisation

Synchronization: semaphores and some more stuff. Operating Systems, Spring 2018, I. Dinur, D. Hendler and R. Iakobashvili

Operating Systems ECE344

518 Lecture Notes Week 7

Introduction to Operating Systems

Last Class: Synchronization

Operating Systems. User OS. Kernel & Device Drivers. Interface Programs. Interprocess Communication (IPC)

Opera&ng Systems ECE344

Concurrency and Synchronisation

Lecture 8: September 30

Concurrency and Synchronisation

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

7: Interprocess Communication

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

What's wrong with Semaphores?

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

Chapter 6: Process Synchronization

Lecture 6 (cont.): Semaphores and Monitors

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Process Synchronization Mechanisms

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

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

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

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

Process Synchronization(2)

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

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Lecture #7: Implementing Mutual Exclusion

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem

Process Synchronization(2)

Operating Systems. Synchronization

EECS 482 Introduction to Operating Systems

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1

CS 318 Principles of Operating Systems

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

Synchronization COMPSCI 386

Process Management And Synchronization

CSE 120 Principles of Operating Systems Spring 2016

Semaphores and Monitors: High-level Synchronization Constructs

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

Process Synchronization and Cooperation

CS3502 OPERATING SYSTEMS

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

CS 318 Principles of Operating Systems

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

The University of Texas at Arlington

Chapter 6: Process Synchronization

CSE 120 Principles of Operating Systems Spring 2016

Concurrency: a crash course

OS Process Synchronization!

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

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

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

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Today: Synchronization. Recap: Synchronization

CS162 Operating Systems and Systems Programming Lecture 7. Mutual Exclusion, Semaphores, Monitors, and Condition Variables

Concurrency. Chapter 5

Process Synchronization(2)

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CS370 Operating Systems

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

Inter- process Communications

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

Critical Section Problem: Example

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

Process Synchronization

Synchronization for Concurrent Tasks

Synchronization I. Jo, Heeseung

Interprocess Communication

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Interprocess Communication By: Kaushik Vaghani

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

The University of Texas at Arlington

Semaphores (by Dijkstra)

Lesson 6: Process Synchronization

Announcements. Class feedback for mid-course evaluations Receive about survey to fill out until this Friday

There are 8 total numbered pages, 6 Questions. You have 60 minutes. Budget your time carefully!

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

C09: Process Synchronization

Operating Systems. Synchronization, part 3 Monitors, classical sync. problems

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

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

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

Transcription:

CENG334 Introduction to Operating Systems Semaphores Topics: Need for higher-level synchronization primitives Semaphores and their implementation The Producer/Consumer problem and its solution with semaphores The Reader/Writer problem and its solution with semaphores Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL: http://kovan.ceng.metu.edu.tr/ceng334 Week 4 13/03/07 1 Higher-level synchronization primitives We have looked at one synchronization primitive: locks Locks are useful for many things, but sometimes programs have different requirements. Examples? Say we had a shared variable where we wanted any number of threads to read the variable, but only one thread to write it. How would you do this with locks? Reader() { lock.acquire(); mycopy = shared_var; lock.release(); return mycopy; Writer() { lock.acquire(); shared_var = NEW_VALUE; lock.release(); What's wrong with this code? 2 1

Semaphores Higher-level synchronization construct Designed by Edsger Dijkstra in the 1960's, part of the THE operating system (classic stuff!) Semaphore Semaphore is a shared counter Two operations on semaphores: P() or wait() or down() From Dutch proeberen, meaning test Atomic action: Wait for semaphore value to become > 0, then decrement it V() or signal() or up() From Dutch verhogen, meaning increment Atomic action: Increments semaphore value by 1. 3 Semaphore Example Semaphores can be used to implement locks: Semaphore my_semaphore = 1; // Initialize to nonzero int withdraw(account, amount) { down(my_semaphore); balance = get_balance(account); balance -= amount; put_balance(account, balance); up(my_semaphore); return balance; critical section A semaphore where the counter value is only 0 or 1 is called a binary semaphore. 4 2

Simple Semaphore Implementation struct semaphore { int val; threadlist L; // List of threads waiting for semaphore down(semaphore S): // Wait until > 0 then decrement while (S.val <= 0) { Why is this a while loop add this thread to S.L; and not just an if statement? block(this thread); S.val = S.val -1; return; up(semaphore S): // Increment value and wake up next thread S.val = S.val + 1; if (S.L is nonempty) { remove a thread T from S.L; wakeup(t); What's wrong with this picture??? 5 Simple Semaphore Implementation struct semaphore { int val; threadlist L; // List of threads waiting for semaphore down(semaphore S): // Wait until > 0 then decrement while (S.val <= 0) { add this thread to S.L; block(this thread); S.val = S.val -1; return; up(semaphore S): // Increment value and wake up next thread S.val = S.val + 1; if (S.L is nonempty) { remove a thread T from S.L; wakeup(t); down() and up() must be atomic actions! 6 3

Semaphore Implementation How do we ensure that the semaphore implementation is atomic? 7 Semaphore Implementation How do we ensure that the semaphore implementation is atomic? One approach: Make them system calls, and ensure only one down() or up() operation can be executed by any process at a time. This effectively puts a lock around the down() and up() operations themselves! Easy to do by disabling interrupts in the down() and up() calls. Another approach: Use hardware support Say your CPU had atomic down and up instructions That would be sweet. 8 4

OK, but why are semaphores useful? A binary semaphore (counter is always 0 or 1) is basically a lock. The real value of semaphores becomes apparent when the counter can be initialized to a value other than 0 or 1. Say we initialize a semaphore's counter to 50. What does this mean about down() and up() operations? 9 The Producer/Consumer Problem Also called the Bounded Buffer problem. Mmmm... donuts Producer Consumer Producer pushes items into the buffer. Consumer pulls items from the buffer. Producer needs to wait when buffer is full. Consumer needs to wait when the buffer is empty. 10 5

The Producer/Consumer Problem Also called the Bounded Buffer problem. zzzzz... Producer Consumer Producer pushes items into the buffer. Consumer pulls items from the buffer. Producer needs to wait when buffer is full. Consumer needs to wait when the buffer is empty. 11 One implementation... Producer Consumer int count = 0; Producer() { int item; while (TRUE) { item = bake(); if (count == N) sleep(); insert_item(item); count = count + 1; if (count == 1) wakeup(consumer); What's wrong with this code? Consumer() { int item; while (TRUE) { if (count == 0) sleep(); item = remove_item(); count = count 1; if (count == N-1) wakeup(producer); eat(item); What if we context switch right here?? 12 6

A fix using semaphores Producer Consumer Semaphore mutex = 1; Semaphore empty = N; Semaphore full = 0; Producer() { int item; while (TRUE) { item = bake(); down(empty); down(mutex); insert_item(item); up(mutex); up(full); Consumer() { int item; while (TRUE) { down(full); down(mutex); item = remove_item(); up(mutex); up(empty); eat(item); 13 Reader/Writers Let's go back to the problem at the beginning of lecture. Single shared object Want to allow any number of threads to read simultaneously But, only one thread should be able to write to the object at a time (And, not interfere with any readers...) Why the test Semaphore mutex = 1; Semaphore wrt = 1; int readcount = 0; Reader() { here?? Writer() { down(wrt); do_write(); up(wrt); A Reader should only wait for a Writer to complete its do_write(). A Reader should not wait for other Readers to complete their do_read(). The Writer should wait for the other Writers to complete their do_write(). The Writer should wait for all the Readers to complete their do_read(). down(mutex); readcount++; if (readcount == 1) { down(wrt); up(mutex); do_read(); down(mutex); readcount--; if (readcount == 0) { up(wrt); up(mutex); 14 7

Issues with Semaphores Much of the power of semaphores derives from calls to down() and up() that are unmatched See previous example! Unlike locks, acquire() and release() are not always paired. This means it is a lot easier to get into trouble with semaphores. More rope Would be nice if we had some clean, well-defined language support for synchronization... Java does! 15 8