InterProcess Communication Signals David Ferry Chris Gill Marion

  • Slides: 24
Download presentation
Inter-Process Communication: Signals David Ferry, Chris Gill, Marion Sudvarg CSE 422 S - Operating

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

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course 3. Real-Time Signals CSE 422 S – Operating Systems Organization 2

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course 3. Real-Time Signals CSE 422 S – Operating Systems Organization 3

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 422 S – Operating Systems Organization 4

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. A single bit indicates pending status for each signal type. Pending signals are not queued. (Except real-time signals!) CSE 422 S – Operating Systems Organization 5

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 422 S – Operating Systems Organization 6

Two Ways to Write Signal Handlers 1 handler for many signals 1 handler for

Two Ways to Write Signal Handlers 1 handler for many signals 1 handler for each signal void sig_handler( int signum ){ if (signum == SIGUSR 1) { //Handle SIGUSR 1 } if (signum == SIGUSR 2) { //Handle SIGUSR 2 } } void sig_handler_sigusr 1( int signum ){ //Handle SIGUSR 1 } void sig_handler_sigusr 2( int signum ){ //Handle SIGUSR 2 } int main (int argc, char* argv[]){ struct sigaction ss; ss. sa_handler = sig_handler; sigaction( SIGUSR 1, &ss, NULL ); sigaction( SIGUSR 2, &ss, NULL ); } int main (int argc, char* argv[]){ struct sigaction ss 1, ss 2; ss 1. sa_handler = sig_handler_sigusr 1; ss 2. sa_handler = sig_handler_sigusr 2; sigaction( SIGUSR 1, &ss 1, NULL ); sigaction( SIGUSR 2, &ss 2, NULL ); } Functionally equivalent, matter of programming style, though handler for each signal avoids overhead of if statement CSE 422 S – Operating Systems Organization 7

Signal Example Process A (pid 1000) Executes code Event happens kill(2000, SIGUSR 1); Process

Signal Example Process A (pid 1000) Executes code Event happens kill(2000, SIGUSR 1); Process B (pid 2000) context switch Kernel sends signal context switch sigusr 1_handler() CSE 422 S – Operating Systems Organization 8

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course 3. Real-Time Signals CSE 422 S – Operating Systems Organization 9

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. (Except real-time signals!) CSE 422 S – Operating Systems Organization 10

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 422 S – Operating Systems Organization 11

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 422 S – Operating Systems Organization 12

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 422 S – Operating Systems Organization 13

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 422 S – Operating Systems Organization 14

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 422 S – Operating Systems Organization 15

Signal Safety Guidelines • Use only async-signal-safe functions – Many library functions, e. g.

Signal Safety Guidelines • Use only async-signal-safe functions – Many library functions, e. g. printf(), backed by static memory – Main thread of execution calls printf(), begins writing to buffer – Signal received, handler calls printf(), corrupts buffer • Declare global shared variables volatile – Prevents compiler from optimizing away reads from memory for value already in register – Ensures main thread of execution sees updates from signal handlers • Declare global shared variables sig_atomic_t – Prevents inconsistent state of variable if update instructions interrupted by signal handler CSE 422 S – Operating Systems Organization 16

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course

Overview 1. Signals: CSE 361 Review Material 2. Signals: Material New to this Course 3. Real-Time Signals CSE 422 S – Operating Systems Organization 17

Real-Time Signals: Overview • Provide an increased range of signals (SIGRTMIN to SIGRTMAX) •

Real-Time Signals: Overview • Provide an increased range of signals (SIGRTMIN to SIGRTMAX) • May be sent with accompanying data • Queued (up to a limit, SIGQUEUE_MAX) • Delivery order is guaranteed (lowestnumbered signal delivered first) CSE 422 S – Operating Systems Organization 18

Increased Range • Linux has many standard signals • Only signals without assigned meaning

Increased Range • Linux has many standard signals • Only signals without assigned meaning are SIGUSR 1 and SIGUSR 2 • But what if … – Process 1: Agent: processes batches of jobs – Process 2: Monitor: monitors progress of Agent • Agent reports status to Monitor after each batch – Status 1: Completed Successfully – Status 2: Completed with Errors – Status 3: Incomplete • Need 3 signal types! • Use SIGRTMIN, SIGRTMIN+1, SIGRTMIN+2 CSE 422 S – Operating Systems Organization 19

Sending Real-Time Signals • Use: sigqueue(pid_t pid, int sig, const union sigval value) •

Sending Real-Time Signals • Use: sigqueue(pid_t pid, int sig, const union sigval value) • Unlike kill(), can’t target a process group • sigval may contain – An integer value or – A (void) pointer to a data structure CSE 422 S – Operating Systems Organization 20

Receiving Real-Time Signals • Just like any signal! • Use: sigaction(int sig, struct sigaction

Receiving Real-Time Signals • Just like any signal! • Use: sigaction(int sig, struct sigaction * act, struct sigaction * oldact) • act->sa_sigaction points to signal handler: void handler(int sig, siginfo_t * si, void * ucontext) • si->si_value is union sigval CSE 422 S – Operating Systems Organization 21

Accompanying Data • What if the Monitor needs to know the results of each

Accompanying Data • What if the Monitor needs to know the results of each batch? • The Agent can assign an integer result to the sigval passed by sigqueue • OR assign a pointer to a more complex data structure (the Monitor must know the pointer type to cast from void *) CSE 422 S – Operating Systems Organization 22

Queueing Example Agent (pid 1000) Monitor (pid 2000) Completes Batch 1 sigqueue(2000, SIGRTMIN+2, sigval)

Queueing Example Agent (pid 1000) Monitor (pid 2000) Completes Batch 1 sigqueue(2000, SIGRTMIN+2, sigval) Completes Batch 2 sigqueue(2000, SIGRTMIN+2, sigval) context switch Kernel sends real-time signals context switch sigrtmin 2_handler() (Knows two batches completed) CSE 422 S – Operating Systems Organization 23

Delivery Order Example Agent (pid 1000) Monitor (pid 2000) Completes Batch 1 sigqueue(2000, SIGRTMIN+2,

Delivery Order Example Agent (pid 1000) Monitor (pid 2000) Completes Batch 1 sigqueue(2000, SIGRTMIN+2, sigval) Terminates Batch 2 sigqueue(2000, SIGRTMIN, sigval) context switch Kernel sends real-time signals context switch sigrtmin_handler() (Guaranteed to handle terminated batch first!) sigrtmin 2_handler() CSE 422 S – Operating Systems Organization 24