Signals and Inter-Process Communica.on

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

Basic OS Progamming Abstrac2ons

Basic OS Progamming Abstrac7ons

Basic OS Programming Abstractions (and Lab 1 Overview)

ECE 650 Systems Programming & Engineering. Spring 2018

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)

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

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

Na.ve POSIX Threading Library (NPTL)

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

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

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

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

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

W4118 Operating Systems. Junfeng Yang

Lecture 24: Multitasking and Signals

Design Overview of the FreeBSD Kernel CIS 657

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

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

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

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

Process Control. Philipp Koehn. 23 April 2018

Chapter 4 Multithreaded Programming

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

System Programming. Signals I

CS 4410, Fall 2017 Project 1: My First Shell Assigned: August 27, 2017 Due: Monday, September 11:59PM

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

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

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

CSE 421: Introduction to Operating Systems

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Advanced Unix Concepts. Satyajit Rai

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

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

Signals. Joseph Cordina

SMD149 - Operating Systems

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

Interrupts, Fork, I/O Basics

Most of the work is done in the context of the process rather than handled separately by the kernel

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

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

System Calls & Signals. CS449 Spring 2016

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

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CS240: Programming in C

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

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

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

Interprocess Communication

The Kernel. wants to be your friend

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Computer Science & Engineering Department I. I. T. Kharagpur. Operating System: CS rd Year CSE: 5th Semester (Autumn ) Lecture VII

VEOS high level design. Revision 2.1 NEC

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

Unix System Programming - Chapter 8

Programs. Program: Set of commands stored in a file Stored on disk Starting a program creates a process static Process: Program loaded in RAM dynamic

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Operating Systems 2010/2011

CS 326: Operating Systems. Process Execution. Lecture 5

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

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

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

CSCI Computer Systems Fundamentals Shell Lab: Writing Your Own Unix Shell

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

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

Unix Processes. What is a Process?

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

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

CSC374, Spring 2010 Lab Assignment 1: Writing Your Own Unix Shell

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

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

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

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

Process. Program Vs. process. During execution, the process may be in one of the following states

Process Address Spaces and Binary Formats

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

Process Management forks, bombs, zombies, and daemons! Lecture 5, Hands-On Unix System Administration DeCal

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

What is a Process? Processes and Process Management Details for running a program

Operating Systems 9/6/ SIGINT Terminate Interrupt from keyboard (ctl-c) 9 SIGKILL Terminate Kill program (cannot override or ignore)

CS 213, Fall 2002 Lab Assignment L5: Writing Your Own Unix Shell Assigned: Oct. 24, Due: Thu., Oct. 31, 11:59PM

CSE506: Operating Systems CSE 506: Operating Systems

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Implementing Lightweight Threads

Overview. Over the next four weeks, we will look at these topics: Building Blocks. Advanced Authentication Issues.

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

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

Operating Systemss and Multicore Programming (1DT089)

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

Chapter 1 Introduction

Operating Systemss and Multicore Programming (1DT089)

Process Management! Goals of this Lecture!

Transcription:

Signals and Inter-Process Communica.on Housekeeping Paper reading assigned for next class Don Porter 1 2 Binary Formats RCU Memory Management Logical Diagram Memory Allocators Today s Lecture System Calls Process File CoordinaNon System Networking Device Drivers Threads Sync CPU Scheduler User Kernel Last Nme We ve discussed how the OS schedules the CPU And how to block a process on a resource (disk, network) Today: How do processes block on each other? And more generally communicate? Interrupts Disk Net Consistency Hardware 3 4 Outline Signals Overview and APIs Handlers Kernel-level delivery Interrupted system calls Interprocess CommunicaNon (IPC) Pipes and FIFOs System V IPC Windows Analogs What is a signal? Like an interrupt, but for applicanons < 64 numbers with specific meanings A process can raise a signal to another process or thread A process or thread registers a handler funcnon For both IPC and delivery of hardware excepnons ApplicaNon-level handlers: divzero, segfaults, etc. No message beyond the signal was raised And maybe a lible metadata PID of sender, faulnng address, etc. But placorm-specific (non-portable) 5 6 1

Pid 300 Pid 300 Pid 400... signal(sigusr1, &usr_handler);...... PC kill(300, SIGUSR1); int usr_handler() { Register usr_handler() to handle SIGUSR1 7 Send signal to PID 300 8 Basic Model ApplicaNon registers handlers with signal or sigacnon Send signals with kill and friends Or raised by hardware excepnon handlers in kernel Signal delivery jumps to signal handler Irregular control flow, similar to an interrupt API names are admibedly confusing 9 Signal Types See man 7 signal for the full list: (varies by sys/arch) SIGTSTP 1 Stop typed at terminal (Ctrl+Z) SIGKILL 9 Kill a process, for realzies SIGSEGV 11 SegmentaNon fault SIGPIPE 13 Broken pipe (write with no readers) SIGALRM 14 Timer SIGUSR1 10 User-defined signal 1 SIGCHLD 17 Child stopped or terminated SIGSTOP 19 Stop a process SIGCONT 18 ConNnue if stopped 10 Language ExcepNons Signals are the underlying mechanism for ExcepNons and catch blocks JVM or other runnme system sets signal handlers Signal handler causes execunon to jump to the catch block Signal Handler Control Flow Normal program flow User Mode Signal handler do_signal() Kernel Mode handle_signal() setup_frame() Return code on the stack system_call() sys_sigreturn() restore_sigcontext() Figure 11-2. Catching a signal 11 From Understanding the Linux Kernel 12 2

Alternate Stacks Signal handlers execute on a different stack than program execunon. Why? Safety: App can ensure stack is actually mapped And avoid assumpnons about applicanon not using space below rsp Set with sigaltstack() system call Like an interrupt handler, kernel pushes register state on interrupt stack Return to kernel with sigreturn() system call App can change its own on-stack register state! Nested Signals What happens when you get a signal in the signal handler? And why should you care? 13 14 The Problem with NesNng /*... */ signal(sigint, &handler); Another signal Double free! delivered on signal(sigterm, &handler); return /*... */ PC Calls munmap() Signal Stack int handler() { SIGINT free(buf1); SIGTERM free(buf2); Nested Signals The original signal() specificanon was a total mess! Now deprecated---do not use! New sigacnon() API lets you specify this in detail What signals are blocked (and delivered on sigreturn) Similar to disabling hardware interrupts As you might guess, blocking system calls inside of a signal handler are only safe with careful use of sigacnon() 15 16 ApplicaNon vs. Kernel App: signals appear to be delivered roughly immediately Kernel (lazy): Send a signal == mark a pending signal in the task And make runnable if blocked with TASK_INTERRUPTIBLE flag Check pending signals on return from interrupt or syscall Deliver if pending Pid 300 RUNNING INTERRUPTIBLE read(); PC 10 Mark pending signal, Block on unblock disk Pid 400 read! What happens to read? kill(300, SIGUSR1); 17 int usr_handler() { Send signal to PID 300 18 3

Interrupted System Calls If a system call blocks in the INTERRUPTIBLE state, a signal wakes it up Yet signals are delivered on return from a system call How is this resolved? The system call fails with a special error code EINTR and friends Many system calls transparently retry aver sigreturn Some do not check for EINTR in your applicanons! Default handlers Signals have default handlers: Ignore, kill, suspend, connnue, dump core These execute inside the kernel Installing a handler with signal/sigacnon overrides the default A few (SIGKILL) cannot be overridden 19 20 RT Signals Default signals are only in 2 states: signaled or not If I send 2 SIGUSR1 s to a process, only one may be delivered If system is slow and I furiously hit Ctrl+C over and over, only one SIGINT delivered Real Nme (RT) signals keep a count Deliver one signal for each one sent Signal Summary AbstracNon like hardware interrupts Some care must be taken to block other interrupts Easy to write buggy handlers and miss EINTR Understand control flow from applicanon and kernel perspecnve Understand basic APIs 21 22 Pipes, Sockets, and FIFOs System V IPC Windows comparison Other IPC Pipes Stream of bytes between two processes Read and write like a file handle But not anywhere in the hierarchical file system And not persistent And no cursor or seek()-ing Actually, 2 handles: a read handle and a write handle Primarily used for parent/child communicanon Parent creates a pipe, child inherits it 23 24 4

int pipe_fd[2]; int rv = pipe(pipe_fd); int pid = fork(); if (pid == 0) { close(pipe_fd[1]); //Close unused write end dup2(pipe_fd[0], 0); // Make the read end stdin exec( grep, quack ); else { close (pipe_fd[0]); // Close unused read end FIFOs (aka Named Pipes) ExisNng pipes can t be opened---only inherited Or passed over a Unix Domain Socket (beyond today s lec) FIFOs, or Named Pipes, add an interface for opening exisnng pipes 25 26 Sockets Similar to pipes, except for network connecnons Setup and connecnon management is a bit trickier A topic for another day (or class) Select What if I want to block unnl one of several handles has data ready to read? Read will block on one handle, but perhaps miss data on a second Select will block a process unnl a handle has data available Useful for applicanons that use pipes, sockets, etc. 27 28 Synthesis : The Shell Almost all commands are really binaries /bin/ls Key abstracnon: RedirecNon over pipes >, <, and implemented by the shell itself Shell Ex: ls grep foo ImplementaNon sketch: Shell parses the ennre string Sets up chain of pipes Forks and exec s ls and grep separately Wait on output from grep, print to console 29 30 5

Job control in a shell Shell keeps its own scheduler for background processes How to: Put a process in the background? SIGTSTP handler catches Ctrl-Z Send SIGSTOP to current foreground child Resume execunon (fg)? Send SIGCONT to paused child, use waitpid() to block unnl finished Execute in background (bg)? Send SIGCONT to paused child, but block on terminal input Other hints Splice(), tee(), and similar calls are useful for connecnng pipes together Avoids copying data into and out-of applicanon 31 32 System V IPC Semaphores Lock Message Queues Like a mail box, small messages Shared Memory parncularly useful A region of non-cow anonymous memory Map at a given address using shmat() Can persist longer than an applicanon Must be explicitly deleted Can leak at system level But cleared aver a reboot System V Keys and IDs Programmers pick arbitrary 32-bit keys Use these keys to name shared abstracnons Find a key using shmget(), msgget(), etc. Kernel internally maps key to a 32-bit ID 33 34 Windows Comparison Hardware excepnons are treated separately from IPC Upcalls to ntdll.dll (libc equivalent), to call handlers All IPC types can be represented as handles Process terminanon/suspend/resume signaled with process handles Signals can be an Event handle Semaphores and Mutexes have handles Shared memory equally complicated (but snll handles) Single select()-like API to wait on a handle to be signaled Summary Understand signals Understand high-level propernes of pipes and other Unix IPC abstracnons High-level comparison with Windows 35 36 6