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

Similar documents
Chapter 4: Synchronization

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

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

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

Chapter 5: Memory Management

ECE 650 Systems Programming & Engineering. Spring 2018

Chapter 4 Multithreaded Programming

System Programming. Signals I

Signals. Joseph Cordina

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

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

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

COSC243 Part 2: Operating Systems

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

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

Operating Systems 2010/2011

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

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

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

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

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

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

Chapter 1 Introduction

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

System Calls & Signals. CS449 Spring 2016

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

Processes. Processes (cont d)

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

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

Design Overview of the FreeBSD Kernel CIS 657

Signals and Inter-Process Communica.on

CS240: Programming in C

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

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Process. Signal #8. Signals are software interrupts from unexpected events. a power failure. an alarm clock. the death of a child process

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

Lecture 4 Threads. (chapter 4)

Killing Zombies, Working, Sleeping, and Spawning Children

Operating Systemss and Multicore Programming (1DT089)

Processes. CS439: Principles of Computer Systems January 24, 2018

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

(MCQZ-CS604 Operating Systems)

SMD149 - Operating Systems

Systems Programming/ C and UNIX

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

Implementing Lightweight Threads

CSE 421: Introduction to Operating Systems

Signal Example 1. Signal Example 2

Interprocess Communication

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

Processes. CS439: Principles of Computer Systems January 30, 2019

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

Operating Systemss and Multicore Programming (1DT089)

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

Lecture 2 Process Management

Acontecimentos assíncronos (POSIX signals) Sincronização com múltiplos acontecimentos

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

CS 326: Operating Systems. Process Execution. Lecture 5

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

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals

Lecture 9: Midterm Review

Workshop on Inter Process Communication Solutions

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Embedded Systems Programming

CPS 310 first midterm exam, 10/6/2014

Kernel and processes

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

Midterm Exam CPS 210: Operating Systems Spring 2013

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

Princeton University. Computer Science 217: Introduction to Programming Systems. Signals

CS 134: Operating Systems

Exam Guide COMPSCI 386

Signals and Session Management. Signals. Mechanism to notify processes of system events

Lab 4. Out: Friday, February 25th, 2005

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Linux Signals and Daemons

Lecture Summary Operating Systems on 8/3/2015. Table of Contents 1 Introduction Processes Scheduling Synchronization...

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

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Operating Systems. Review ENCE 360

CSI Module 2: Processes

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

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

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

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

MARUTHI SCHOOL OF BANKING (MSB)

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

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

COMP 2400 UNIX Tools

Course Syllabus. Operating Systems, Spring 2016, Meni Adler, Danny Hendler & Amnon Meisels

Unix Processes. What is a Process?

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

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

Transcription:

Last time: Scheduling Networks and Operating Systems Chapter 14: Synchronization (252-0062-00) Donald Kossmann & Torsten Hoefler Frühjahrssemester 2012 Basics: Workloads, tradeoffs, definitions Batch-oriented scheduling FCFS, Convoys, SJF, Preemption: SRTF Interactive workloads RR, Priority, Multilevel Feedback Queues, Linux, Resource containers Realtime RM, EDF Multiprocessors Systems Group Department of Computer Science ETH Zürich Goals today Overview of er-process communication systems Hardware support With shared memory Without shared memory Upcalls Generally: very broad field Quite competitive especially with microkernels Recap: Hardware support for synchronization Disabling errupts Disabling errupts Critical section Disable all errupts / traps Enable errupts State to be protected 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. 1

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) 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. Load-Locked, Store-Conditional Back to TAS Factors CAS, etc. o two instructions: 1. LL: load from a location and mark a 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. Critical section old = TAS(flag) if (old == True) Flag False Spin forever waiting? Available on Alpha, MIPS, etc Spinning On a uniprocessor: Not much po in spinning at all. What s going to happen? Possibly an errupt 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 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 2

Techniques you already know IPC with shared memory Semaphores P, V operations Mutexes Acquire, Release Condition Variables Wait, Signal (Notify), Broadcast (NotifyAll) Monitors Enter, Exit Focus here: eraction with scheduling Priority Inversion Preemption Wait for lock Most OSes provide some form of these High priority Key issue not yet covered: eraction between scheduling and synchronization Med. priority Preemption Inverted priority Acquire lock Example: Priority inversion Assuming a priority scheduler, e.g. Unix, Windows Low priority Time Anyone recognize this? 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 highestpriority process that can ever hold lock Requires static analysis, used in embedded RT systems 3

Priority Inheritance Preemption Wait for lock Acquire lock High priority IPC without shared memory Med. priority Acquire lock This process acquires high priority Release lock Low priority Time Asynchronous (buffered) IPC Synchronous (unbuffered) IPC Process 1 RECV Process 1 RECV Receiver blocks waiting for msg Sender does not block Sender blocks until receiver ready Process 2 SEND Process 2 SEND Time Time 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 nonshared-memory system (based on messages) Exercise: pick your favourite example of one, and show how to build the dual. Unix Pipes Basic (first) Unix IPC mechanism Unidirectional, buffered communication channel between two processes Creation: pipe( pipefd[2]) Q. How to set up pipe between two processes? A. Don t! Create the pipe first, then fork 4

Pipe idiom (man 2 pipe) Unix shell pipes Create a pipe E.g.: Fork In child: close write end Read from pipe and write to standard output until EOF In parent: close read end and write argv[1] to pipe curl --silent http://people.inf.ethz.ch/troscoe/mothy-bio.txt 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! (h: man dup2 ) Messaging systems A good textbook will examine options: End-pos 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 ve seen most of this before! In networking Many parallels between message-passing operating systems, and networks Example The concept of a port allows: Naming of different end-pos 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. s = socket(af_unix, type, 0); Naming pipes Named 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) 5

Local Remote Procedure Call Can use RPC locally: Define procedural erface 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 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) Signal types (some of them) Name Description / meaning Default action SIGHUP Hangup / death of controlling process Terminate process SIGINT Interrupt character typed (CTRL-C) Terminate process SIGQUIT Quit character typed (CTRL-\) Hanging up Core the dump phone (terminal) SIGKILL kill -9 <process id> Terminate process SIGSEGV Segfault (invalid memory reference) Core dump Can t be disabled! SIGPIPE Write on pipe with no reader Terminate process SIGALRM alarm() goes off E.g. after other side of Terminate process SIGCHLD pipe has closed it Child process stopped or terminated Ignored SIGSTOP Stop process Stop SIGCONT Continue process Used by debuggers Continue (e.g. gdb) SIGUSR1,2 User-defined signals and shell (CTRL-Z) Terminate process Etc. see man 7 signal for the full list 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 Sending a signal to a process From the Unix shell: $ kill HUP 4234 From C: kill(pid_t pid, signo); Kill is a rather unfortunate name 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 6

Oldskool: signal() Test your C parsing skills: void (*signal( sig, void (*handler)())) (); What does this mean? Oldskool: signal() void (*signal( sig, void (*handler)())) (); Unpacking this: A handler looks like void my_handler(); Signal takes two arguments An eger (the signal type, e.g. SIGPIPE) A poer to a handler function and returns a poer to a handler function The previous handler, Special handler arguments: SIG_IGN (ignore), SIG_DFL (default), SIG_ERR (error code) 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 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? 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? New action for signal signo void (*sa_handler)(); sigset_t sa_mask; 7

Previous action is returned void (*sa_handler)(); sigset_t sa_mask; Signal handler void (*sa_handler)(); sigset_t sa_mask; void (*sa_handler)(); Signals to be blocked in sigset_t sa_mask; this handler (c.f. fd_set) void (*sa_handler)(); sigset_t sa_mask; More sophisticated signal handler (depending on flags) 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! 8