Chapter 4: Synchronization

Similar documents
Last time: Scheduling. Goals today. Recap: Hardware support for synchronization. Disabling interrupts. Disabling interrupts

Example: Linux o(1) scheduler. Networks and Operating Systems ( ) Chapter 4: Synchronization. Problems with UNIX Scheduling

Networks and Opera/ng Systems Chapter 14: Synchroniza/on

Signal types (some of them) Networks and Operating Systems ( ) Chapter 5: Memory Management. Where do signals come from?

But this will not be complete (no book covers 100%) So consider it a rough approximation Last lecture OSPP Sections 3.1 and 4.1

Chapter 5: Memory Management

Networks and Operating Systems ( ) Chapter 3: Scheduling

Administrivia. Networks and Operating Systems ( ) Chapter 3: Scheduling. A Small Quiz. Last time. Scheduling.

Operating Systems 2010/2011

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

ECE 650 Systems Programming & Engineering. Spring 2018

Signals. Joseph Cordina

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Lecture 2 Process Management

Embedded Systems Programming

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Mid Term from Feb-2005 to Nov 2012 CS604- Operating System

Linux Signals and Daemons

System Programming. Signals I

CS 33. Signals Part 1. CS33 Intro to Computer Systems XXII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Chapter 4 Multithreaded Programming

Operating Systems and Networks Assignment 9

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

SMD149 - Operating Systems

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

COSC243 Part 2: Operating Systems

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

(MCQZ-CS604 Operating Systems)

Multiprogramming. Evolution of OS. Today. Comp 104: Operating Systems Concepts 28/01/2013. Processes Management Scheduling & Resource Allocation

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 6

CS 326: Operating Systems. CPU Scheduling. Lecture 6

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Signals. CSC209: Software Tools and Systems Programming. Furkan Alaca & Paul Vrbik

Design Overview of the FreeBSD Kernel CIS 657

* What are the different states for a task in an OS?

Killing Zombies, Working, Sleeping, and Spawning Children

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

Subject Name: OPERATING SYSTEMS. Subject Code: 10EC65. Prepared By: Kala H S and Remya R. Department: ECE. Date:

CS240: Programming in C

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Lecture 9: Midterm Review

MARUTHI SCHOOL OF BANKING (MSB)

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

Exam Guide COMPSCI 386

Main Points of the Computer Organization and System Software Module

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Operating Systemss and Multicore Programming (1DT089)

Networks and Opera/ng Systems Chapter 13: Scheduling

CSE 421: Introduction to Operating Systems

W4118 Operating Systems. Junfeng Yang

o Reality The CPU switches between each process rapidly (multiprogramming) Only one program is active at a given time

Operating Systems. Process scheduling. Thomas Ropars.

CHAPTER 2: PROCESS MANAGEMENT

Shell Execution of Programs. Process Groups, Session and Signals 1

Processes. Processes (cont d)

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Advanced Operating Systems (CS 202) Scheduling (1)

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

CS370 Operating Systems Midterm Review

Workshop on Inter Process Communication Solutions

Signals: Management and Implementation. Sanjiv K. Bhatia Univ. of Missouri St. Louis

System Calls & Signals. CS449 Spring 2016

Chapter 1 Introduction

Computer Systems Assignment 4: Scheduling and I/O

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

TDIU25: Operating Systems II. Processes, Threads and Scheduling

CS 153 Design of Operating Systems Winter 2016

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

Operating system concepts. Task scheduling

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

CPS 310 first midterm exam, 10/6/2014

Signal Example 1. Signal Example 2

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

UNIT:2. Process Management

OPERATING SYSTEMS CS3502 Spring Processor Scheduling. Chapter 5

Signals and Inter-Process Communica.on

CSE 506: Opera.ng Systems Signals and Inter-Process Communica.on

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

Lecture 4: Memory Management & The Programming Interface

Problem Set: Processes

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Operating Systemss and Multicore Programming (1DT089)

Implementing Lightweight Threads

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Operating System Study Notes Department of Computer science and Engineering Prepared by TKG, SM and MS

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

Concurrent Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

Transcription:

ADRIAN PERRIG & TORSTEN HOEFLER ( 252-0062-00 ) Networks and Operating Systems Chapter 4: Synchronization Source: xkcd

Real Time 2

Real-time scheduling Problem: giving real time-based guarantees to tasks Tasks can appear at any time Tasks can have deadlines Execution time is generally known Tasks can be periodic or aperiodic Must be possible to reject tasks which are unschedulable, or which would result in no feasible schedule 3

Example: multimedia scheduling 4

Rate-monotonic scheduling Schedule periodic tasks by always running task with shortest period first. Static (offline) scheduling algorithm Suppose: m tasks C i is the execution time of i th task P i is the period of i th task Then RMS will find a feasible schedule if: i= 1 m( 2 1 1) (Proof is beyond scope of this course) m Ci P i m 5

Earliest Deadline First Schedule task with earliest deadline first (duh..) Dynamic, online. Tasks don t actually have to be periodic More complex - O(n) for scheduling decisions EDF will find a feasible schedule if: m i= 1 C i P i 1 Which is very handy. Assuming zero context switch time 6

Guaranteeing processor rate E.g. you can use EDF to guarantee a rate of progress for a longrunning task Break task into periodic jobs, period p and time s. A task arrives at start of a period Deadline is the end of the period Provides a reservation scheduler which: Ensures task gets s seconds of time every p seconds Approximates weighted fair queuing Algorithm is regularly rediscovered 7

Multiprocessor Scheduling 8

Challenge 1: sequential programs on multiprocessors Queuing theory straightforward, although: More complex than uniprocessor scheduling Harder to analyze Task queue Core 0 Core 1 Core 2 Core 3 But 9

It s much harder Overhead of locking and sharing queue Classic case of scaling bottleneck in OS design Solution: per-processor scheduling queues Core 0 Core 1 Core 2 Core 3 In practice, each is more complex e.g. MFQ 10

It s much harder Threads allocated arbitrarily to cores tend to move between cores tend to move between caches really bad locality and hence performance Solution: affinity scheduling Keep each thread on a core most of the time Periodically rebalance across cores Note: this is non-work-conserving! Alternative: hierarchical scheduling (Linux) 11

Challenge 2: parallel applications Global barriers in parallel applications One slow thread has huge effect on performance Corollary of Amdahl s Law Multiple threads would benefit from cache sharing Different applications pollute each others caches Leads to concept of co-scheduling Try to schedule all threads of an application together Critically dependent on synchronization concepts 12

Multicore scheduling Multiprocessor scheduling is two-dimensional When to schedule a task? Where (which core) to schedule on? General problem is NP hard But it s worse than that: Don t want a process holding a lock to sleep Might be other running tasks spinning on it Not all cores are equal In general, this is a wide-open research problem 13

Little s Law Assume, in a train station: 100 people arrive per minute Each person spends 15 minutes in the station How big does the station have to be (house how many people) Little s law: The average number of active tasks in a system is equal to the average arrival rate multiplied by the average time a task spends in a system 14

Our Small Quiz True or false (raise hand) Throughput is an important goal for batch schedulers Response time is an important goal for batch schedulers Realtime schedulers schedule jobs faster than batch schedulers Realtime schedulers have higher throughput than batch schedulers The scheduler has to be invoked by an application FCFS scheduling has low average waiting times Starvation can occur in FCFS scheduling Starvation can occur in SJF scheduling Preemption can be used to improve interactivity Round Robin scheduling is fair Multilevel Feedback Queues in Linux prevent starvation Simple Unix scheduling fairly allocates the time to each user RMS scheduling achieves full CPU utilization Multiprocessor scheduling is NP hard 15

Last time: Scheduling Basics: Workloads, tradeoffs, definitions Batch-oriented scheduling FCFS, Convoys, SJF, Preemption: SRTF Interactive workloads RR, Priority, Multilevel Feedback Queues, Linux, Resource containers Realtime RMS, EDF Multiprocessors This time: OSPP Section 5 (not including IPC) 16

Goals today Overview of inter-process communication systems Hardware support With shared memory Without shared memory Upcalls Generally: very broad field Quite competitive especially with microkernels 17

Recap: Hardware support for synchronization 18

Disabling interrupts Critical section Disable all interrupts / traps State to be protected Enable interrupts 19

Disabling interrupts Nice and simple Can t be rescheduled inside critical section data can t be altered by anything else Except Another processor! Hmm. Very efficient if in kernel on a uniprocessor. 20

Test-And-Set instruction Atomically: Read the value of a memory location Set the location to 1 Available on some hardware (e.g., PA-RISC) (actually, more a RAC Read-And-Clear) 21

Compare-And-Swap (CAS) word cas(word *flag, word oldval, word newval) { atomically { if (*flag == oldval) { *flag = newval; return oldval; } else { return *flag; } } } Available on e.g., x86, IBM/370, SPARC, ARM, Theoretically, slightly more powerful than TAS Why? Other variants e.g., CAS2, etc. 22

Load-Link, Store-Conditional Factors CAS, etc. into two instructions: 1. LL: load from a location and mark as owned 2. SC: Atomically: 1. Store only if already marked by this processor 2. Clear any marks set by other processors 3. Return whether it worked. Available on PPC, Alpha, MIPS, etc 23

Back to TAS Spin forever waiting? old = TAS(flag) if (old == True) Critical section flag False 24

Spinning On a uniprocessor: Not much point in spinning at all. What s going to happen? Possibly an interrupt On a multiprocessor: Can t spin forever Another spin is always cheap Blocking thread and rescheduling is expensive Spinning only works if lock holder is running on another core 25

Competitive spinning How long to spin for? Competitive spinning : Within a factor of 2 of optimal, offline (i.e., impossible!) algorithm Good approach: spin for the context switch time Best case: avoid context switch entirely Worst case: twice as bad as simply rescheduling 26

IPC with shared memory 27

Techniques you already know Semaphores P, V operations Mutexes Acquire, Release Condition Variables Wait, Signal (Notify), Broadcast (NotifyAll) Monitors Enter, Exit 28

Focus here: interaction with scheduling Most OSes provide some form of these Key issue not yet covered: interaction between scheduling and synchronization Example: Priority inversion Assuming a priority scheduler, e.g., Unix, Windows 29

Priority Inversion High priority Low priority Acquire lock Time 30

Priority Inversion Preemption High priority Low priority Acquire lock Time 31

Priority Inversion Preemption Wait for lock High priority Low priority Acquire lock Time 32

Priority Inversion Preemption Wait for lock High priority Preemption Med. priority Low priority Acquire lock Time 33

Priority Inversion Preemption Wait for lock High priority Preemption Inverted priority Med. priority Low priority Acquire lock Time 34

Anyone recognize this? 35

Priority Inheritance Process holding lock inherits priority of highest priority process that is waiting for the lock. Releasing lock priority returns to previous value Ensures forward progress Alternative: Priority Ceiling Process holding lock acquires priority of highest-priority process that can ever hold lock Requires static analysis, used in embedded RT systems 36

Priority Inheritance Preemption Wait for lock High priority Med. priority Acquire lock Low priority Time 37

Priority Inheritance Preemption Wait for lock High priority Med. priority Low priority Acquire lock This process acquires high priority Time 38

Priority Inheritance High priority Preemption Wait for lock Acquire lock Med. priority Acquire lock This process acquires high priority Release lock Low priority Time 39

IPC without shared memory 40

Asynchronous (buffered) IPC Process 1 RECV Process 2 SEND Time 41

Asynchronous (buffered) IPC Process 1 RECV Receiver blocks wai@ng for msg Process 2 SEND Time 42

Asynchronous (buffered) IPC Process 1 RECV Receiver blocks wai@ng for msg Sender does not block Process 2 SEND Time 43

Synchronous (unbuffered) IPC Process 1 RECV Process 2 SEND Time 44

Synchronous (unbuffered) IPC Process 1 RECV Sender blocks un@l receiver ready Process 2 SEND Time 45

Duality of messages and shared-memory Famous claim by Lauer and Needham (1978): Any shared-memory system (e.g., one based on monitors and condition variables) is equivalent to a non-shared-memory system (based on messages) Exercise: pick your favourite example of one, and show how to build the dual. 46

Unix Pipes Basic (first) Unix IPC mechanism Unidirectional, buffered communication channel between two processes Creation: int pipe(int pipefd[2]) Q. How to set up pipe between two processes? A. Don t! Create the pipe first, then fork 47

Pipe idiom (man 2 pipe) Create a pipe 48

Pipe idiom (man 2 pipe) Fork 49

Pipe idiom (man 2 pipe) In child: close write end 50

Pipe idiom (man 2 pipe) Read from pipe and write to standard output until EOF 51

Pipe idiom (man 2 pipe) In parent: close read end and write argv[1] to pipe 52

Unix shell pipes E.g.: curl --silent http://spcl.inf.ethz.ch/teaching/2014-osnet/ sed 's/[^a-za-z]/\n/g' sort -fu egrep -v '^\s*$' wc -l Shell forks each element of the pipeline Each process connected via pipes Stdout of process n stdin of process n+1 Each process then exec s the appropriate command Exercise: write it! (hint: man dup2 ) 53

Messaging systems A good textbook will examine options: End-points may or may not know each others names Messages might need to be sent to more than one destination Multiple arriving messages might need to be demultiplexed Can t wait forever for one particular message BUT: you ll see most of this somewhere else! In networking Many parallels between message-passing operating systems and networks 54

Example The concept of a port allows: Naming of different end-points within a process Demultiplexing of messages Waiting selectively for different kinds of messages Analogous to socket and TCP port in IPv4 In Unix, Unix domain sockets do exactly this. int s = socket(af_unix, type, 0); 55

Naming pipes Pipes so far are only named by their descriptors Namespace is local to the process Copied on fork() How to put a pipe in the global namespace? Make it a named pipe Special file of type pipe (also known as a FIFO) 56

Named pipes 57

Local Remote Procedure Call Can use RPC locally: Define procedural interface in an IDL Compile / link stubs Transparent procedure calls over messages Naïve implementation is slow Lots of things (like copying) don t matter with a network, but do matter between local processes Can be made very fast: more in the AOS course 58

Unix signals Asynchronous notification from the kernel Receiver doesn t wait: signal just happens Interrupt process, and: Kill it Stop (freeze) it Do something else (see later) 59

Signal types (some of them) Name Description / meaning Default action SIGHUP Hangup / death of controlling process Terminate process SIGINT Interrupt character typed (CTRL-C) Hanging Terminate up the process phone (terminal) SIGQUIT Quit character typed (CTRL-\) Core dump SIGKILL kill -9 <process id> Terminate process Can t be SIGSEGV Segfault (invalid memory reference) Core dump disabled! SIGPIPE Write on pipe with no reader Terminate process SIGALRM alarm() goes off E.g., after other side Terminate of process pipe has closed it SIGCHLD Child process stopped or terminated Ignored SIGSTOP Stop process Stop SIGCONT Continue process Used by debuggers (e.g., Continue gdb) and shell (CTRL-Z) SIGUSR1,2 User-defined signals Terminate process Etc. see man 7 signal for the full list 60

Where do signals come from? Memory management subsystem: SIGSEGV, etc. IPC system SIGPIPE Other user processes SIGUSR1,2, SIGKILL, SIGSTOP, SIGCONT Kernel trap handlers SIGFPE The TTY Subsystem SIGINT, SIGQUIT, SIGHUP 61

Sending a signal to a process From the Unix shell: $ kill HUP 4234 From C: #include <signal.h> int kill(pid_t pid, int signo); Kill is a rather unfortunate name 62

Unix signal handlers Change what happens when a signal is delivered: Default action Ignore signal Call a user-defined function in the process the signal handler Allows signals to be used like user-space traps 63

Oldskool: signal() Test your C parsing skills: #include <signal.h> void (*signal(int sig, void (*handler)(int))) (int); What does this mean? 64

Oldskool: signal() void (*signal(int sig, void (*handler)(int))) (int); Unpacking this: A handler looks like void my_handler(int); Signal takes two arguments An integer (the signal type, e.g. SIGPIPE) A pointer to a handler function and returns a pointer to a handler function The previous handler, Special handler arguments: SIG_IGN (ignore), SIG_DFL (default), SIG_ERR (error code) 65

Unix signal handlers Signal handler can be called at any time! Executes on the current user stack If process is in kernel, may need to retry current system call Can also be set to run on a different (alternate) stack User process is in undefined state when signal delivered 66

Implications There is very little you can safely do in a signal handler! Can t safely access program global or static variables Some system calls are re-entrant, and can be called Many C library calls cannot (including _r variants!) Can sometimes execute a longjmp if you are careful With signal, cannot safely change signal handlers What happens if another signal arrives? 67

Multiple signals If multiple signals of the same type are to be delivered, Unix will discard all but one. If signals of different types are to be delivered, Unix will deliver them in any order. Serious concurrency problem: How to make sense of this? 68

A better signal()posix sigaction() #include <signal.h> int sigaction(int signo, const struct sigaction *act, struct sigaction *oldact); struct sigaction { void (*sa_handler)(int); sigset_t sa_mask; }; New action for signal signo Signal handler Signals to be blocked in this handler (cf., fd_set) int sa_flags; void (*sa_sigaction)(int, siginfo_t *, void *); More sophisticated signal handler (depending on flags) Previous action is returned 69

Signals as upcalls Particularly specialized (and complex) form of Upcall Kernel RPC to user process Other OSes use upcalls much more heavily Including Barrelfish Scheduler Activations : dispatch every process using an upcall instead of return Very important structuring concept for systems! 70