Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Inter-Process Communication: Signals David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO 63130 1 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 522S – Advanced Operating Systems 2 Common Signals • • • • • SIGINT – keyboard interrupt (CTRL+C) SIGTERM – terminate program SIGKILL – kill program immediately SIGSEV – segmentation fault SIGUSR1 & SIGUSR2 Allows for the delivery of a notifications, but not for delivery of data. (E.g. segmentation faults simply crash) CSE 522S – Advanced Operating Systems 3 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 522S – Advanced Operating Systems 4 Signal Handler Example #include <signal.h> void sigusr1_handler( int signum ){ //Handle signal } int main( int argc, char* argv[]){ //Normal program flow } CSE 522S – Advanced Operating Systems 5 Signal Example Process A: Executes code Event happens kill( B, SIGUSR1); context switch Kernel sends signal context switch Process B: context switch context switch sigusr1_handler(); CSE 522S – Advanced Operating Systems 6 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 7 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 (SIGSEV) 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 8 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 9 Concurrency Race Example char* buffer; void sig_handler( int signum ){ buffer = “Handler called\n”; write( buffer ); } int main( int argc, char* argv[] ){ buffer = “Main called\n”; write( buffer ); } CSE 522S – Advanced Operating Systems 10 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 522S – Advanced Operating Systems 11