C 35 carnegie mellon today multasking shells signals

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: nterrupts read call   Linux: upon return from signal handler, the read call is restarted automa2cally   Some other flavors of Unix can cause the read call to fail with an EINTR error number (errno) in this case, the applica2on program can restart the slow system call   Subtle differences like these complicate the wri&ng of portable code that uses signals   Consult your textbook for details 32 Carnegie Mellon A Program That Reacts to Externally Generated Events (Ctrl ­c) #include <stdlib.h> #include <stdio.h> #include <signal.h> void handler(int sig) { safe_printf("You think hitting ctrl-c will stop the bomb?\n"); sleep(2); safe_printf("Well..."); linux> ./external sleep(1); <ctrl-c> printf("OK\n"); You think hitting ctrl-c will stop exit(0); the bomb? } Well...OK linux> main() { signal(SIGINT, handler); /* installs ctl-c handler */ while(1) { } } external.c 33 Carnegie Mellon A Program That Reacts to Internally Generated Events #include <stdio.h> #include <signal.h> int beeps = 0; main() { signal(SIGALRM, handler); alarm(1); /* send SIGALRM in 1 second */ /* SIGALRM handler */ void handler(int sig) { safe_printf("BEEP\n"); while (1) { /* handler returns here */ } } if (++beeps < 5) alarm(1); else { safe_printf("BOOM!\n"); exit(0); } } internal.c linux> ./internal BEEP BEEP BEEP BEEP BEEP BOOM! bass> 34 Carnegie Mellon Async ­Signal ­Safety     Func&on is async ­signal ­safe if either reentrant (all variables stored on stack frame, CS:APP2e 12.7.2) or non ­interrup&ble by signals. Posix guarantees 117 func&ons to be async ­signal ­safe   write is on the list, printf is not   One solu&on: async ­signal ­safe wrapper for printf: void safe_printf(const char *format, ...) { char buf[MAXS]; va_list args; va_start(args, format); vsnprintf(buf, sizeof(buf), format, args); va_end(args); write(1, buf, strlen(buf)); /* /* /* /* reentrant */ reentrant */ reentrant */ async-signal-safe */ } safe_prini.c 35 Carnegie Mellon Today       Mul&tasking, shells Signals Nonlocal jumps 36 Carnegie Mellon Nonlocal Jumps: setjmp/longjmp   Powerful (but dangerous) user ­level mechanism for transferring control to an arbitrary loca&on   Controlled to way to break the procedure call / return discipline   Useful for error recovery and signal handling   int setjmp(jmp_buf j)   Must be called before longjmp   Iden2fies a return site for a subsequent longjmp   Called once, returns one or more 2mes   Implementa&on:   Remember where you are by storing the current register context, stack pointer, and PC value in jmp_buf   Return 0 37 Carnegie Mellon setjmp/longjmp (cont)   void longjmp(jmp_buf j, int i)   Meaning: return from the setjmp remembered by jump buffer j again ......
View Full Document

Ask a homework question - tutors are online