CSE ¡506: ¡Opera.ng ¡Systems ¡
Signals ¡and ¡Inter-‑Process ¡ Communica.on ¡
¡ Don ¡Porter ¡
1 ¡
Signals and Inter-Process Communica.on Don Porter 1 - - PowerPoint PPT Presentation
CSE 506: Opera.ng Systems Signals and Inter-Process Communica.on Don Porter 1 CSE 506: Opera.ng Systems Housekeeping Paper reading assigned for
CSE ¡506: ¡Opera.ng ¡Systems ¡
1 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– (Class ¡a:er ¡midterm) ¡
2 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
3 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– And ¡how ¡to ¡block ¡a ¡process ¡on ¡a ¡resource ¡(disk, ¡network) ¡
– How ¡do ¡processes ¡block ¡on ¡each ¡other? ¡ – And ¡more ¡generally ¡communicate? ¡
4 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Overview ¡and ¡APIs ¡ – Handlers ¡ – Kernel-‑level ¡delivery ¡ – Interrupted ¡system ¡calls ¡
– Pipes ¡and ¡FIFOs ¡ – System ¡V ¡IPC ¡ – Windows ¡Analogs ¡
5 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– < ¡64 ¡numbers ¡with ¡specific ¡meanings ¡ – A ¡process ¡can ¡raise ¡a ¡signal ¡to ¡another ¡process ¡or ¡thread ¡ – A ¡process ¡or ¡thread ¡registers ¡a ¡handler ¡funcNon ¡
– ApplicaNon-‑level ¡handlers: ¡divzero, ¡segfaults, ¡etc. ¡
– And ¡maybe ¡a ¡lible ¡metadata ¡
6 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
7 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
PC ¡
8 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Or ¡raised ¡by ¡hardware ¡excepNon ¡handlers ¡in ¡kernel ¡
– Irregular ¡control ¡flow, ¡similar ¡to ¡an ¡interrupt ¡
9 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
10 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Signal ¡handler ¡causes ¡execuNon ¡to ¡jump ¡to ¡the ¡catch ¡block ¡
11 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
Figure 11-2. Catching a signal
Normal program flow Signal handler Return code
do_signal() handle_signal() setup_frame() system_call() sys_sigreturn() restore_sigcontext() User Mode Kernel Mode
12 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Why? ¡
– And ¡avoid ¡assumpNons ¡about ¡applicaNon ¡not ¡using ¡space ¡below ¡rsp ¡
– Set ¡with ¡sigaltstack() ¡system ¡call ¡
– Return ¡to ¡kernel ¡with ¡sigreturn() ¡system ¡call ¡ – App ¡can ¡change ¡its ¡own ¡on-‑stack ¡register ¡state! ¡
¡
13 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
14 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
SIGINT ¡ SIGTERM ¡ Signal ¡Stack ¡ PC ¡
Calls ¡ munmap() ¡ Another ¡signal ¡ delivered ¡on ¡ return ¡ Double ¡free! ¡
15 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Now ¡deprecated-‑-‑-‑do ¡not ¡use! ¡
– What ¡signals ¡are ¡blocked ¡(and ¡delivered ¡on ¡sigreturn) ¡ – Similar ¡to ¡disabling ¡hardware ¡interrupts ¡
16 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Send ¡a ¡signal ¡== ¡mark ¡a ¡pending ¡signal ¡in ¡the ¡task ¡
– Check ¡pending ¡signals ¡on ¡return ¡from ¡interrupt ¡or ¡syscall ¡
17 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
PC ¡ … ¡ … ¡ 10 ¡
INTERRUPTIBLE ¡
18 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– EINTR ¡and ¡friends ¡ – Many ¡system ¡calls ¡transparently ¡retry ¡a:er ¡sigreturn ¡ – Some ¡do ¡not ¡– ¡check ¡for ¡EINTR ¡in ¡your ¡applicaNons! ¡
19 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Ignore, ¡kill, ¡suspend, ¡conNnue, ¡dump ¡core ¡ – These ¡execute ¡inside ¡the ¡kernel ¡
20 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– 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 ¡
21 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Some ¡care ¡must ¡be ¡taken ¡to ¡block ¡other ¡interrupts ¡ – Easy ¡to ¡write ¡buggy ¡handlers ¡and ¡miss ¡EINTR ¡ ¡
22 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
23 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– 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 ¡
24 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
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 …
25 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Or ¡passed ¡over ¡a ¡Unix ¡Domain ¡Socket ¡(beyond ¡today’s ¡lec) ¡
26 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– A ¡topic ¡for ¡another ¡day ¡(or ¡class) ¡
27 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Useful ¡for ¡applicaNons ¡that ¡use ¡pipes, ¡sockets, ¡etc. ¡
28 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– /bin/ls ¡
– ‘>’, ¡‘<‘, ¡and ¡‘|’implemented ¡by ¡the ¡shell ¡itself ¡
29 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– 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 ¡
30 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Put ¡a ¡process ¡in ¡the ¡background? ¡
– Resume ¡execuNon ¡(fg)? ¡
– Execute ¡in ¡background ¡(bg)? ¡
31 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Avoids ¡copying ¡data ¡into ¡and ¡out-‑of ¡applicaNon ¡
32 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– 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 ¡a:er ¡a ¡reboot ¡
33 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Use ¡these ¡keys ¡to ¡name ¡shared ¡abstracNons ¡
– Kernel ¡internally ¡maps ¡key ¡to ¡a ¡32-‑bit ¡ID ¡
34 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– Upcalls ¡to ¡ntdll.dll ¡(libc ¡equivalent), ¡to ¡call ¡handlers ¡
– 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) ¡
35 ¡
CSE ¡506: ¡Opera.ng ¡Systems ¡
– High-‑level ¡comparison ¡with ¡Windows ¡
36 ¡