Graphics Programs Reference
In-Depth Information
System daemons run detached from a controlling terminal, so the new
tinyweb daemon code writes to a log file. Without a controlling terminal,
system daemons are typically controlled with signals. The new tinyweb
daemon program will need to catch the terminate signal so it can exit
cleanly when killed.
0x621
Crash Course in Signals
Signals provide a method of interprocess communication in Unix. When a
process receives a signal, its flow of execution is interrupted by the operating
system to call a signal handler. Signals are identified by a number, and each
one has a default signal handler. For example, when CTRL -C is typed in a
program's controlling terminal, an interrupt signal is sent, which has a default
signal handler that exits the program. This allows the program to be inter-
rupted, even if it is stuck in an infinite loop.
Custom signal handlers can be registered using the signal() function.
In the example code below, several signal handlers are registered for certain
signals, whereas the main code contains an infinite loop.
signal_example.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
/* Some labeled signal defines from signal.h
* #define SIGHUP 1 Hangup
* #define SIGINT 2 Interrupt (Ctrl-C)
* #define SIGQUIT 3 Quit (Ctrl-\)
* #define SIGILL 4 Illegal instruction
* #define SIGTRAP 5 Trace/breakpoint trap
* #define SIGABRT 6 Process aborted
* #define SIGBUS 7 Bus error
* #define SIGFPE 8 Floating point error
* #define SIGKILL 9 Kill
* #define SIGUSR1 10 User defined signal 1
* #define SIGSEGV 11 Segmentation fault
* #define SIGUSR2 12 User defined signal 2
* #define SIGPIPE 13 Write to pipe with no one reading
* #define SIGALRM 14 Countdown alarm set by alarm()
* #define SIGTERM 15 Termination (sent by kill command)
* #define SIGCHLD 17 Child process signal
* #define SIGCONT 18 Continue if stopped
* #define SIGSTOP 19 Stop (pause execution)
* #define SIGTSTP 20 Terminal stop [suspend] (Ctrl-Z)
* #define SIGTTIN 21 Background process trying to read stdin
* #define SIGTTOU 22 Background process trying to read stdout
*/
/* A signal handler */
void signal_handler(int signal) {
 
Search WWH ::




Custom Search