Fall 2014:: CSE 506:: Section 2 (PhD)
Signals and Inter-Process Communication (IPC)
Nima Honarmand (Based on slides by Don Porter and Mike Ferdman)
Signals and Inter-Process Communication (IPC) Nima Honarmand - - PowerPoint PPT Presentation
Fall 2014:: CSE 506:: Section 2 (PhD) Signals and Inter-Process Communication (IPC) Nima Honarmand (Based on slides by Don Porter and Mike Ferdman) Fall 2014:: CSE 506:: Section 2 (PhD) Outline Signals Overview and APIs Handlers
Fall 2014:: CSE 506:: Section 2 (PhD)
Nima Honarmand (Based on slides by Don Porter and Mike Ferdman)
Fall 2014:: CSE 506:: Section 2 (PhD)
– Overview and APIs – Handlers – Kernel-level delivery – Interrupted system calls
– Pipes and FIFOs – System V IPC
Fall 2014:: CSE 506:: Section 2 (PhD)
– < 64 numbers with specific meanings – Sending: A process can raise a signal to another process or thread – Sending: Kernel can send signals to processes or threads – Receiving: A process or thread registers a handler function
– Application-level handlers: divzero, segfaults, etc.
– And maybe a little metadata
Fall 2014:: CSE 506:: Section 2 (PhD)
Fall 2014:: CSE 506:: Section 2 (PhD)
PC
Fall 2014:: CSE 506:: Section 2 (PhD)
– Or raised by hardware exception handlers in kernel
– Irregular control flow, similar to an interrupt
Fall 2014:: CSE 506:: Section 2 (PhD)
SIGTSTP: Stop typed at terminal (Ctrl+Z) SIGKILL: Kill a process SIGSEGV: Segmentation fault SIGPIPE: Broken pipe (write with no readers) SIGALRM: Timer SIGUSR1: User-defined signal 1 SIGCHLD: Child stopped or terminated SIGSTOP: Stop a process SIGCONT: Continue if stopped
Fall 2014:: CSE 506:: Section 2 (PhD)
– Signal handler causes execution to jump to the catch block
Fall 2014:: CSE 506:: Section 2 (PhD)
Fall 2014:: CSE 506:: Section 2 (PhD)
– Why?
– Set with sigaltstack() system call
– Return to kernel with sigreturn() system call – App can change its own on-stack register state!
Fall 2014:: CSE 506:: Section 2 (PhD)
Fall 2014:: CSE 506:: Section 2 (PhD)
int main() { /* ... */ signal(SIGINT, &handler); signal(SIGTERM, &handler); /* ... */ } int handler() { free(buf1); free(buf2); }
SIGINT SIGTERM Signal Stack PC
Calls munmap() Another signal delivered on return Double free!
Fall 2014:: CSE 506:: Section 2 (PhD)
– Now deprecated---do not use!
– What signals are blocked (and delivered on sigreturn) – Similar to disabling hardware interrupts
Fall 2014:: CSE 506:: Section 2 (PhD)
– Send a signal == mark a pending signal in the task
– Check pending signals on return from interrupt or syscall
Fall 2014:: CSE 506:: Section 2 (PhD)
Pid 300 RUNNING
Pid 400
PC … … SIGUSR1
Pid 300
INTERRUPTIBLE
Block on disk read! Mark pending signal, unblock What happens to read?
Fall 2014:: CSE 506:: Section 2 (PhD)
– EINTR and friends – Many system calls transparently retry after sigreturn – Some do not – check for EINTR in your applications!
Fall 2014:: CSE 506:: Section 2 (PhD)
– Ignore, kill, suspend, continue, dump core – These execute inside the kernel
Fall 2014:: CSE 506:: Section 2 (PhD)
– 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,
– Deliver one signal for each one sent
Fall 2014:: CSE 506:: Section 2 (PhD)
Fall 2014:: CSE 506:: Section 2 (PhD)
– 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
– Parent creates a pipe, child inherits it
Fall 2014:: CSE 506:: Section 2 (PhD)
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 …
Fall 2014:: CSE 506:: Section 2 (PhD)
– Or passed over a Unix Domain Socket (beyond today’s lec)
Fall 2014:: CSE 506:: Section 2 (PhD)
– A topic for another day (or class)
Fall 2014:: CSE 506:: Section 2 (PhD)
– Useful for applications that use pipes, sockets, etc.
Fall 2014:: CSE 506:: Section 2 (PhD)
– /bin/ls
– ‘>’, ‘<‘, and ‘|’implemented by the shell itself
Fall 2014:: CSE 506:: Section 2 (PhD)
– Shell parses the entire string – Sets up chain of pipes – Forks and exec’s ‘ls’ and ‘grep’ separately – Wait on output from ‘grep’, print to console
Fall 2014:: CSE 506:: Section 2 (PhD)
– How to suspend the foreground process?
– Resume execution (fg)?
finished
– Execute in background (bg)?
Fall 2014:: CSE 506:: Section 2 (PhD)
– Avoids copying data into and out-of application
Fall 2014:: CSE 506:: Section 2 (PhD)
– A region of non-COW anonymous memory – Map at a given address using shmat()
– Must be explicitly deleted – Can leak at system level – But cleared after a reboot