SIGNAL(3)
π SIGNAL(3) β simplified software signal facilities π
okay bestie, welcome to signal(3), the β¨ Standard C Library β¨ (libc, -lc) era of signal handling. this is literally the manual page for the simplified software signal facilities and i'm gonna walk you through it, no cap πΈ
β¨ SYNOPSIS β¨
so first up, the vibe of what you're actually calling:
#include <signal.h>
void
(*signal(int sig, void (*func)(int)))(int);
yes bestie that's a function returning a pointer to a function, it's giving "i survived the declarator" energy π
or, in FreeBSD's equivalent but easier to read typedef'd version (bless):
typedef void (*sig_t) (int);
sig_t
signal(int sig, sig_t func);
π¦ DESCRIPTION π¦
this signal() facility is a simplified interface to the more general sigaction(2) facility. like, it's the same idea but wearing a cuter outfit π
signals allow the manipulation of a process from outside its domain, and also let the process manipulate itself or copies of itself (children). iconic, honestly π
there are two general types of signals:
- those that cause termination of a process
- those that don't
signals which cause termination of a program might result from an irrecoverable error, or might be the result of a user at a terminal typing the interrupt character. signals are used when a process is stopped because it wishes to access its control terminal while in the background (see tty(4)). signals are optionally generated when a process resumes after being stopped, when the status of child processes changes, or when input is ready at the control terminal.
most signals result in the termination of the process receiving them if no action is taken. some signals instead cause the process receiving them to be stopped, or are simply discarded if the process has not requested otherwise.
and here's the big one: except for the SIGKILL and SIGSTOP signals, the signal() function allows for a signal to be caught, to be ignored, or to generate an interrupt. these signals are defined in the file <signal.h> π
π the signal lineup (yes, all 33 of them)
1SIGHUPβ default action: terminate process β terminal line hangup2SIGINTβ default action: terminate process β interrupt program3SIGQUITβ default action: create core image β quit program4SIGILLβ default action: create core image β illegal instruction5SIGTRAPβ default action: create core image β trace trap6SIGABRTβ default action: create core image β abort program (formerlySIGIOT)7SIGEMTβ default action: create core image β emulate instruction executed8SIGFPEβ default action: create core image β floating-point exception9SIGKILLβ default action: terminate process β kill program10SIGBUSβ default action: create core image β bus error11SIGSEGVβ default action: create core image β segmentation violation12SIGSYSβ default action: create core image β non-existent system call invoked13SIGPIPEβ default action: terminate process β write on a pipe with no reader14SIGALRMβ default action: terminate process β real-time timer expired15SIGTERMβ default action: terminate process β software termination signal16SIGURGβ default action: discard signal β urgent condition present on socket17SIGSTOPβ default action: stop process β stop (cannot be caught or ignored)18SIGTSTPβ default action: stop process β stop signal generated from keyboard19SIGCONTβ default action: discard signal β continue after stop20SIGCHLDβ default action: discard signal β child status has changed21SIGTTINβ default action: stop process β background read attempted from control terminal22SIGTTOUβ default action: stop process β background write attempted to control terminal23SIGIOβ default action: discard signal β I/O is possible on a descriptor (seefcntl(2))24SIGXCPUβ default action: terminate process β cpu time limit exceeded (seesetrlimit(2))25SIGXFSZβ default action: terminate process β file size limit exceeded (seesetrlimit(2))26SIGVTALRMβ default action: terminate process β virtual time alarm (seesetitimer(2))27SIGPROFβ default action: terminate process β profiling timer alarm (seesetitimer(2))28SIGWINCHβ default action: discard signal β Window size change29SIGINFOβ default action: discard signal β status request from keyboard30SIGUSR1β default action: terminate process β User defined signal 131SIGUSR2β default action: terminate process β User defined signal 232SIGTHRβ default action: terminate process β thread interrupt33SIGLIBRTβ default action: terminate process β real-time library interrupt
π§ the sig and func girlies
the sig argument specifies which signal was received. the func procedure allows a user to choose the action upon receipt of a signal. so like, you're the main character choosing your destiny π«
- to set the default action of the signal to occur as listed above,
funcshould beSIG_DFL. aSIG_DFLresets the default action. - to ignore the signal,
funcshould beSIG_IGN. this will cause subsequent instances of the signal to be ignored and pending instances to be discarded.
if SIG_IGN is not used, further occurrences of the signal are automatically blocked and func is called.
the handled signal is unblocked when the function returns, and the process continues from where it left off when the signal occurred. and unlike previous signal facilities, the handler func() remains installed after a signal has been delivered β she's staying, periodt π
π«Ά the restart situation
for some system calls, if a signal is caught while the call is executing and the call is prematurely terminated, the call is automatically restarted. any handler installed with signal(3) will have the SA_RESTART flag set, meaning that any restartable system call will not return on receipt of a signal.
the affected system calls include read(2), write(2), sendto(2), recvfrom(2), sendmsg(2) and recvmsg(2) on a communications channel or a low speed device and during a ioctl(2) or wait(2).
however β calls that have already committed are not restarted, but instead return a partial success (for example, a short read count). these semantics could be changed with siginterrupt(3).
πΈ forks, exec, and inheriting your baggage
when a process which has installed signal handlers forks, the child process inherits the signals. all caught signals may be reset to their default action by a call to the execve(2) function; ignored signals remain ignored.
π the zombie situation (very on brand)
if a process explicitly specifies SIG_IGN as the action for the signal SIGCHLD, the system will not create zombie processes when children of the calling process exit. slay, undeclared.
as a consequence, the system will discard the exit status from the child processes. and if the calling process subsequently issues a call to wait(2) or equivalent, it will block until all of the calling process's children terminate, and then return a value of -1 with errno set to ECHILD. π
see sigaction(2) for a list of functions that are considered safe for use in signal handlers. that's the canon list bestie, don't freestyle it.
β RETURN VALUES β
the previous action is returned on a successful call. otherwise, SIG_ERR is returned and the global variable errno is set to indicate the error.
π ERRORS π
the signal() function will fail and no action will take place if one of the following occur:
[EINVAL]β Thesigargument is not a valid signal number.[EINVAL]β An attempt is made to ignore or supply a handler forSIGKILLorSIGSTOP.
fr fr,SIGKILLandSIGSTOPare not negotiable, they will not be caught, they will not be ignored, they are the unbothered queens of the signal world π
π₯ SEE ALSO π₯
kill(1), kill(2), ptrace(2), sigaction(2), sigaltstack(2), sigprocmask(2), sigsuspend(2), wait(2), fpsetmask(3), setjmp(3), siginterrupt(3), tty(4)
π HISTORY π
the signal() function appeared in Version 4 AT&T UNIX. the current signal facility appeared in 4.0BSD. the option to avoid the creation of child zombies through ignoring SIGCHLD appeared in FreeBSD 5.0. a whole glow-up timeline, honestly β¨
that's the whole page bestie, go forth and handle your signals responsibly π¦π
mwah π β this Debian page is dated December 1, 2017, and the source/version footer says Debian β catch you in the next manual, ily! π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms