Beruflich Dokumente
Kultur Dokumente
and SIGSTOP.
Basics
E er! signal has a s!mbolic name that starts with the
"refi# SIG. $or e#am"le% SIGI&T is the signal sent when the user "resses Ctrl'C% SIG()*T is the signal sent when the "rocess calls the abort+ , function% and SIGKILL is the signal sent when a "rocess is forcefull! terminated.
Basics
Signals are all defined in a header file included from
-signal.h.. E er! signal is also associated with an integer identifier. The name'to'integer ma""ing for the signals is im"lementation'de"endent% and aries among /ni# s!stems.
Common Signals
Basics
/ses signal+, s!stem call #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal (int signo, sighandler_t handler); ( successful call to signal+ , remo es the current action
ta0en on recei"t of the signal signo% and instead handles the signal with the signal handler s"ecified b! handler. ( "rocess can catch neither SIGKILL nor SIGSTOP% so setting u" a handler for either of these two signals ma0es no sense.
Basics
The handler function must return oid% which
ma0es sense because +unli0e with normal functions, there is no standard "lace in the "rogram for this function to return. The function ta0es one argument% an integer% which is the signal identifier +for e#am"le% SIG/S*1, of the signal being handled. This allows a single function to handle multi"le signals.
Basics
2hen it raises a signal to a "rocess that has registered
a signal handler% the 0ernel sus"ends e#ecution of the "rogram3s regular instruction stream% and calls the signal handler. The handler is "assed the alue of the signal% which is the signo originall! "ro ided to signal+ ,.
Basics
4ou ma! also use signal+ , to instruct the 0ernel to ignore a
gi en signal for the current "rocess% or to reset the signal to the default beha ior. This is done using s"ecial alues for the handler "arameter: SIG56$L ' Set the beha ior of the signal gi en b! signo to its default. SIG5IG& ' Ignore the signal gi en b! signo.
recei es a signal that either is handled or terminates the "rocess: #include <unistd.h> int pause (void); "ause+ , returns onl! if a caught signal is recei ed% in which case the signal is handled% and "ause+ , returns '7% and sets errno to EI&T*.
"rocess in the interru"tible slee" state. &e#t% it calls schedule+ , to in o0e the Linu# "rocess scheduler to find another "rocess to run. (s the "rocess is not actuall! waiting for an!thing% the 0ernel will not wa0e it u" unless it recei es a signal.
to their default actions% unless the "arent "rocess +the one e#ecuting the new "rocess, is ignoring them8 In this case% the newl! created "rocess will also ignore those signals. Put another wa!% an! signal caught b! the "arent is reset to the default action in the new "rocess% and all other signals remain the same.
Sending a Signal
The 0ill+ , s!stem call% the basis of the common kill utility, sends a
signal from one "rocess to another: #include <sys/types.h> #include <signal.h> int kill (pid_t pid, int signo); In its normal use +i.e.% if "id is greater than 9,% 0ill+ , sends the signal signo to the "rocess identified b! "id. If "id is 9% signo is sent to e er! "rocess in the in o0ing "rocess3 "rocess grou". If "id is '7% signo is sent to e er! "rocess for which the in o0ing "rocess has "ermission to send a signal% e#ce"t itself and init.
send a signal to itself: #include <signal.h> int raise (int signo); This call: raise +signo,8 is e:ui alent to the following call: 0ill +get"id + ,% signo,8
eentrancy
2hen the 0ernel raises a signal% a "rocess can be
e#ecuting code an!where. $or e#am"le% it might be in the middle of an im"ortant o"eration that% if interru"ted% would lea e the "rocess is an inconsistent state
e#ecuting when a signal hits8 the handler can run in the middle of an!thing. It is thus er! im"ortant that an! signal handler !our "rocess installs be er! careful about the actions it "erforms and the data it touches
global data
eentrant !unction
( reentrant function is a function that is safe to call
from within itself +or concurrentl!% from another thread in the same "rocess,. In order to :ualif! as reentrant% a function must not mani"ulate static data% must mani"ulate onl! stac0'allocated data or data "ro ided to it b! the caller% and must not in o0e an! nonreentrant function