120 likes | 305 Views
Inter-Process Communication: Signals. David Ferry, Chris Gill, Brian Kocoloski CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO 63130. Signals. Asynchronous notifications: Generated by hardware or requested by another process
E N D
Inter-Process Communication:Signals David Ferry, Chris Gill, Brian Kocoloski CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO 63130
Signals Asynchronous notifications: • Generated by hardware or requested by another process • Delivered to a specific process • Processes receivesignals and respond Allows for event based programming and graceful handling of errors Conceptually is very similar to hardware interrupts and exceptions, but for processes CSE 522S – Advanced Operating Systems
Common Signals • SIGINT – keyboard interrupt (CTRL+C) • SIGTERM – terminate program • SIGKILL – kill program immediately • SIGSEGV – segmentation fault • SIGUSR1 & SIGUSR2 Allows for the delivery of notifications, but not for delivery of data CSE 522S – Advanced Operating Systems
Signal Handler Example #include <signal.h> void sigusr1_handler( intsignum ){ //Handle signal } int main( intargc, char* argv[]){ //Normal program flow } CSE 522S – Advanced Operating Systems
Signal Example Process B: context switch context switch sigusr1_handler(); Process A: Executes codeEvent happens kill( B, SIGUSR1); context switch Kernel sends signal context switch CSE 522S – Advanced Operating Systems
Kernel Implementation A B kill(B, SIGUSR1) User Kernel CSE 522S – Advanced Operating Systems
Kernel Implementation A B kill(B, SIGUSR1) syscall (SYS_kill,B, SIGUSR1) void sigusr1_handler (int signum) { ... } User Kernel • Find process B • Find signal handler for process B • Is signal masked? If so, save it and raise it later • Default handler? If so, terminate B • Else, raise SIGUSR1 in B CSE 522S – Advanced Operating Systems
Signal Default Behaviors All signals have a default action. E.g.: • SIGTERM – terminate program • SIGCHLD – ignore signal See man 7 signal for details… Special signals that cannot be caught or blocked: • SIGKILL (force quit vs. orderly shutdown) • SIGSTOP CSE 522S – Advanced Operating Systems
Signals as Hardware Events A hardware event triggers an interrupt or exception handler that raises a signal, e.g.: • Divide by zero (SIGFPE) • Segmentation fault (SIGSEGV) These are synchronous with program flow! Note: Signals allow userspace programs to respond to and correct hardware-level faults. Compare to how page faults are handled entirely within the kernel. CSE 522S – Advanced Operating Systems
Concurrency Race Example int temp; void swap(int *a, int *b){ temp = *a; *a = *b; *b = temp; } void sig_handler( int signum ){ int v1 = 5, v2 = 10; swap(&v1, v2); } int main(intargc, char ** argv) { int c1 = 50, c2 = 100; sigaction(…., SIGUSR1,…); swap(&c1, &c2); } What values will v1, v2, c1, c2 have if sig_handlerand main both run once? CSE 522S – Advanced Operating Systems
Signal Handler Concurrency A signal handler may be called at any point of execution! Creates a concurrent programming problem even in single threaded programs! • Deadlock • Races • Many of the same risks/strategies of interrupt handlers apply here CSE 522S – Advanced Operating Systems
Today’s Studio • Register custom signal handler • Catch keyboard interrupts (CTRL+C) • Explore non-determinism resulting from signal handling CSE 522S – Advanced Operating Systems