Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC Advanced Unix Programming, Fall 2015

Similar presentations


Presentation on theme: "CSC Advanced Unix Programming, Fall 2015"— Presentation transcript:

1 CSC 552 - Advanced Unix Programming, Fall 2015
September 30 signals

2 sigaction() and signal() and kill()
A UNIX signal is like a user-process-level interrupt. Each distinct signal provides a way to trigger asynchronous processing in a single-threaded (or multi-threaded) process. sigaction() system call specifies a handler signal() was the old way of doing that kill() system call sends a signal to another process

3 sigaction() #include <signal.h> int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact); The sigaction() function allows the calling process to examine or specify the action to be taken on delivery of a specific signal. See signal.h(3HEAD) for an explanation of general signal concepts. The sigaction structure includes the following members: void (*sa_handler)(int); // SIG_DFL, SIG_IGN or event-handling function void (*sa_sigaction)(int, siginfo_t *, void *); // realtime handler sigset_t sa_mask; // other signals to be blocked during execution int sa_flags; //flags and options

4 signal.h Signals / default actions are on p. 257 of text
SIGHUP Exit Hangup (see termio(7I)) SIGINT Exit Interrupt (see termio(7I)) SIGQUIT Core Quit (see termio(7I)) SIGILL Core Illegal Instruction SIGTRAP Core Trace or Breakpoint Trap SIGABRT Core Abort SIGEMT Core Emulation Trap SIGFPE Core Arithmetic Exception SIGKILL Exit Killed SIGBUS Core Bus Error SIGSEGV Core Segmentation Fault SIGSYS Core Bad System Call SIGPIPE Exit Broken Pipe SIGALRM Exit Alarm Clock SIGTERM Exit Terminated SIGUSR Exit User Signal 1 SIGUSR Exit User Signal 2 SIGCHLD Ignore Child Status Changed SIGPWR Ignore Power Fail or Restart SIGWINCH Ignore Window Size Change SIGURG Ignore Urgent Socket Condition SIGPOLL Exit Pollable Event (see streamio(7I)) SIGSTOP Stop Stopped (signal) SIGTSTP Stop Stopped (user) (see termio(7I)) SIGCONT Ignore Continued SIGTTIN Stop Stopped (tty input) (see termio(7I)) SIGTTOU Stop Stopped (tty output) (see termio(7I)) SIGVTALRM Exit Virtual Timer Expired SIGPROF Exit Profiling Timer Expired SIGXCPU Core CPU time limit exceeded (see getrlimit(2)) SIGXFSZ Core File size limit exceeded (see getrlimit(2)) Some abort the process, some core dump by default. See instead man –s7 signal on Linux (above is Solaris).

5 kill() kill() send a signal to a process #include <sys/types.h>
#include <signal.h> int kill(pid_t pid, int sig); pid from getpid(), getppid(), fork() You could pass them via pipes or FIFOs. kill also available as level 1 shell /usr/bin/kill unsigned alarm(unsigned secs) schedules SIGALRM

6 signal masks signal masks disable target signals until a masked signal is re-enabled unlike SIG_IGN, the ignore+discard handler sigset_t objects can be set up for signals, similar to setting up fd_sets for select()/poll() int sigprocmask(…) sets up signal masks at the process level pthread_sigmask for multi-threaded processes

7 pause() int pause(void) pauses until interruption by a signal
always returns -1 pause() suffers from a race condition – between a call to sigprocmask() and pause(), the signal may already have arrived and been handled signal unmasking and pausing should be atomic

8 sigsuspend and sigwait
int sigsuspend(const sigset_t *set); set is set of masked (disabled) signals former set is restored on return after a non-terminating signal int sigwait(const sigset_t *set, int *sig); synchronously wait on a set of signals in the set this set is independent of the process mask (and thread masks) disable those signals in the process (and thread) masks via sigprocmask() in order to avoid “competition”

9 race conditions Most 3C library functions and many system calls are not safe to use in a signal handler because its asynchronous execution may occur within a critical section! Text p. 285 holds a list of safe POSIX functions. Perform minimal flag setting within signal handlers. Do not block indefinitely! Do not use sigsetjmp, siglongjmp, setjmp, longjmp – they leak storage from the stack!

10 Asynchronous I/O and signals
aio_read() and aio_write() use signals to generate events on I/O completion. Doing substantial data processing inside a signal handler is extremely error prone! Create a worker thread that is allowed to block, and let it blocks using select(), read() and write(). It can use semaphores, etc. to sync with other threads. Signal handlers cannot!


Download ppt "CSC Advanced Unix Programming, Fall 2015"

Similar presentations


Ads by Google