InterProcess Communication Signals David Ferry Chris Gill CSE

  • Slides: 11
Download presentation
Inter-Process Communication: Signals David Ferry, Chris Gill CSE 522 S - Advanced Operating Systems

Inter-Process Communication: Signals David Ferry, Chris Gill CSE 522 S - Advanced Operating Systems Washington University in St. Louis, MO 63130 1

Signals Asynchronous notifications: • Generated by hardware or requested by another process • Delivered

Signals Asynchronous notifications: • Generated by hardware or requested by another process • Delivered to a specific process • Processes receive signals and respond Allows for event based programming Conceptually is very similar to hardware interrupts and exceptions, but for processes CSE 522 S – Advanced Operating Systems 2

Common Signals • • • SIGINT – keyboard interrupt (CTRL+C) SIGTERM – terminate program

Common Signals • • • SIGINT – keyboard interrupt (CTRL+C) SIGTERM – terminate program SIGKILL – kill program immediately SIGSEGV – segmentation fault SIGUSR 1 & SIGUSR 2 Allows for the delivery of a notifications, but not for delivery of data. (E. g. segmentation faults simply crash) CSE 522 S – Advanced Operating Systems 3

Signal Receipt For each type of signal, a process can handle signal delivery by:

Signal Receipt For each type of signal, a process can handle signal delivery by: 1. Ignoring it 2. Terminating 3. Invoking a signal handler Additionally, a process may block delivery of certain signals. Undelivered signals are said to be pending. CSE 522 S – Advanced Operating Systems 4

Signal Handler Example #include <signal. h> void sigusr 1_handler( int signum ){ //Handle signal

Signal Handler Example #include <signal. h> void sigusr 1_handler( int signum ){ //Handle signal } int main( int argc, char* argv[]){ //Normal program flow } CSE 522 S – Advanced Operating Systems 5

Signal Example Process A: Executes code Event happens kill( B, SIGUSR 1); context switch

Signal Example Process A: Executes code Event happens kill( B, SIGUSR 1); context switch Kernel sends signal context switch Process B: context switch sigusr 1_handler(); CSE 522 S – Advanced Operating Systems 6

Signal Default Behaviors All signals have a default action. E. g. : • SIGTERM

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 522 S – Advanced Operating Systems 7

Signals as Hardware Events A hardware event triggers an interrupt or exception handler that

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 522 S – Advanced Operating Systems 8

Signal Handler Concurrency A signal handler may be called at any point of execution!

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 522 S – Advanced Operating Systems 9

Concurrency Race Example char* buffer; void sig_handler( int signum ){ buffer = “Handler calledn”;

Concurrency Race Example char* buffer; void sig_handler( int signum ){ buffer = “Handler calledn”; write( buffer ); } int main( int argc, char* argv[] ){ buffer = “Main calledn”; write( buffer ); } CSE 522 S – Advanced Operating Systems 10

More Subtle Concurrency Race Example int temp; void swap( int *a, int *b){ temp

More Subtle Concurrency Race Example int temp; void swap( int *a, int *b){ temp = &a; &a = &b; &b = temp; } What race can happen if this function is called from a signal handler and from elsewhere? CSE 522 S – Advanced Operating Systems 11