CMSC421: Principles of Operating Systems

Similar documents
CMSC421: Principles of Operating Systems

Synchronising Threads

Locks. Dongkun Shin, SKKU

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

Concurrency, Thread. Dongkun Shin, SKKU

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Synchronization I. Jo, Heeseung

Today: Synchronization. Recap: Synchronization

Introduction to PThreads and Basic Synchronization

Solving the Producer Consumer Problem with PThreads

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

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

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

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

Lecture 2: February 6

CSE 4/521 Introduction to Operating Systems

Lecture 5: Synchronization w/locks

Programming in Parallel COMP755

Concurrency: Mutual Exclusion (Locks)

Implementing Locks. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

Synchronization 1. Synchronization

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Mutual Exclusion and Synchronization

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

Dealing with Issues for Interprocess Communication

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

Chapter 6 Process Synchronization

Lecture #7: Implementing Mutual Exclusion

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 6: Process Synchronization

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

Last Class: Synchronization

Synchronization Principles

Systèmes d Exploitation Avancés

Lesson 6: Process Synchronization

CS533 Concepts of Operating Systems. Jonathan Walpole

C09: Process Synchronization

More Shared Memory Programming

CHAPTER 6: PROCESS SYNCHRONIZATION

Concurrency: a crash course

CS 318 Principles of Operating Systems

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

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

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

EECS 482 Introduction to Operating Systems

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

IV. Process Synchronisation

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

POSIX Threads: a first step toward parallel programming. George Bosilca

Synchronization. Dr. Yingwu Zhu

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

CS420: Operating Systems. Process Synchronization

Timers 1 / 46. Jiffies. Potent and Evil Magic

Lecture 9: Midterm Review

Synchronization 1. Synchronization

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Thread and Synchronization

CS370 Operating Systems

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Achieving Synchronization or How to Build a Semaphore

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Lecture 6. Process Synchronization

CSE 153 Design of Operating Systems

Problem Set 2. CS347: Operating Systems

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

Concurrency Race Conditions and Deadlocks

Operating Systems, Assignment 2 Threads and Synchronization

Threading and Synchronization. Fahd Albinali

Operating Systems. Synchronization

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Synchronization COMPSCI 386

CS-537: Midterm Exam (Spring 2001)

ECE 574 Cluster Computing Lecture 8

Synchronization Spinlocks - Semaphores

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

EEE3052: Introduction to Operating Systems. Fall Project #3

Last Class: Deadlocks. Today

CMSC 330: Organization of Programming Languages

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

Computer Core Practice1: Operating System Week10. locking Protocol & Atomic Operation in Linux

Administrivia. Review: Thread package API. Program B. Program A. Program C. Correct answers. Please ask questions on Google group

CS 537 Lecture 11 Locks

Chapter 6: Process Synchronization

Introduction to parallel computing

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

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

Experience with Processes and Monitors in Mesa. Arvind Krishnamurthy

More on Synchronization and Deadlock

TCSS 422: OPERATING SYSTEMS

Threads and concurrency

Threads and concurrency

Synchronization for Concurrent Tasks

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

[537] Locks. Tyler Harter

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

Transcription:

CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof. Mark Corner and Prof. Emery Berger s OS course at Umass Amherst 1

Announcements Readings from Silberchatz [4 th and 6 th chapter] 2

Scheduling Many-one (Green Threads) One-one (linux) Many-many (Window-NT) 3

How does it work in Linux Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) struct task_struct points to process data structures (shared or unique) 4

When to use Threads and When to use processes Processes or Threads Performance? Flexibility/Ease-of-use Robustness Simple Scheduling OS has a list of Threads and schedules them similar to Processes. In Linux, threads/processes treated similarly Chooses one of the threads and loads them to be run Runs them for a while, runs another. 5

Flexibility/Ease of use? Process are more flexible Easy to spawn processes, I can ssh into a server and spawn a process Can communicate over sockets= distributes across machines Threads Communicate using memory must be on the same machine Requires thread-safe code 6

Robustness Process are more robust Processes are separate or sandboxed from other processes If one process dies, no effect on other processes Threads If one thread crashes, whole process terminates Since there is sharing, there are problems with using the stack efficiently 7

Context Switch cost Threads much cheaper Stash registers, PC ( IP ), stack pointer Processes above plus Process context TLB shootdown (later in VM lecture) Process switches more expensive, or require long quanta 8

Synchronization and Concurrency Probably the MOST important concept in OS Lets look at a demonstration 9

What really happened? Count = count + 1 load X R1 add Y Z R1 store Y in Mem Thread 1 executes the first two statements and is preempted Thread 2 executes all the three statements Thread 1 returns and executes the third statement 10

Concurrency Terminology Mutual exclusion ( mutex ) prevents multiple threads from entering Critical section code only one thread can execute at a time Lock mechanism for mutual exclusion Lock entering critical section, accessing shared data Unlock when complete Wait if locked Invariant Something that must be true 11

Why do we need concurrency? Synchronization serves two purposes: Ensure safety for shared updates Avoid race conditions Coordinate actions of threads Parallel computation Event notification ALL interleavings must be correct there are lots of interleavings of events also constrain as little as possible 12

Locks/mutexes Provide mutual exclusion to shared data Two routines Rules: acquire wait for lock, then take it release unlock, wake up waiters Acquire lock before accessing shared data Release lock afterwards Lock initially released 13

Locks and Queueing Acquire: if unlocked, go in; otherwise wait in line Release: Unlock & leave 14

Synchronization and Concurrency Demonstration on using pthread locks/mutexes 15

Implementing Locks Requires hardware support (in general) Can build on atomic operations: Load/Store Disable interrupts Uniprocessors only Test & Set, Compare & Swap 16

Disabling Interrupts int value; Queue q; FREE = 0, BUSY = 1 value = 0; q = empty; void acquire () { disable interrupts; if (value == BUSY) { add this thread to q; enable interrupts; sleep(); } else { value = BUSY; } enable interrupts; } } void release () { disable interrupts; if (q not empty) { thread t = q.pop(); put t on ready queue; } value = FREE; enable interrupts; 17

Lock Variants Implementing Locks with Test and Set Blocking Locks Spin locks Hybrids 18

Atomic Operation But: locks are also variables, updated concurrently by multiple threads Who locks the lock? Answer: use hardware-level atomic operations Test-and-set Compare-and-swap 19

Test and Set semantics What s the effect of testandset(value) when: value = 0? ( unlocked ) value = 1? ( locked ) int testandset (int* v) { int old = *v; *v = 1; return old; } 20

Blocking Locks Suspend thread immediately Lets scheduler execute another thread Go to back of run queue or wait to be woken Minimizes time spent waiting But: always causes context switch void blockinglock (Lock& l) { while (testandset(l.v) == 1) { sched_yield(); } } 21

Spin Locks Instead of blocking, loop until released void spinlock (Lock& l) { while (testandset(l.v) == 1) { ; } } 22

Other variants Spin-then-yield: Spin for some time, then yield Fixed spin time Exponential backoff 23

Safety Locks can enforce mutual exclusion, but notorious source of errors Failure to unlock Double locking Deadlock (its own lecture) Priority inversion not an error per se pthread_mutex_t l; void square (void) { pthread_mutex_lock (&l); // acquires lock // do stuff if (x == 0) { return; } else { x = x * x; } pthread_mutex_unlock (&l); } 24

An in-class discussion 25