Signals UNIX

download Signals UNIX

of 19

Transcript of Signals UNIX

  • 8/12/2019 Signals UNIX

    1/19

  • 8/12/2019 Signals UNIX

    2/19

    Difference System Calls n Signals

  • 8/12/2019 Signals UNIX

    3/19

    Three Actions Possible Ignore the signal

    Catch and handle the signal

    Perform the default action There are two signals that cannot be ignored: SIGKILL

    and SIGSTOP.

  • 8/12/2019 Signals UNIX

    4/19

    Basics Eer! 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 thesignal sent when a "rocess is forcefull! terminated.

  • 8/12/2019 Signals UNIX

    5/19

    Basics Signals are all defined in a header file included from

    -signal.h.

    Eer! signal is also associated with an integeridentifier.

    The name'to'integer ma""ing for the signals isim"lementation'de"endent% and aries among /ni#

    s!stems.

  • 8/12/2019 Signals UNIX

    6/19

    Common Signals

  • 8/12/2019 Signals UNIX

    7/19

    Basics /ses signal+, s!stem call

    #include

    typedef void (*sighandler_t)(int); sighandler_t signal (int signo, sighandler_t handler);

    ( successful call to signal+ , remoes the current actionta0en 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 ma0esno sense.

  • 8/12/2019 Signals UNIX

    8/19

    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.

  • 8/12/2019 Signals UNIX

    9/19

    Basics2hen 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 thesignal

    handler.

    The handler is "assed the alue of the signal% which is

    the signo originall! "roided to signal+ ,.

  • 8/12/2019 Signals UNIX

    10/19

    Basics 4ou ma! also use signal+ , to instruct the 0ernel to ignore a

    gien signal for the current "rocess% or to reset the signal tothe default behaior.

    This is done using s"ecial alues for the handler "arameter:

    SIG56$L ' Set the behaior of the signal gien b! signoto its default.

    SIG5IG& ' Ignore the signal gien b! signo.

  • 8/12/2019 Signals UNIX

    11/19

    Waiting for a Signal "ause+ , s!stem call "uts a "rocess to slee" until it

    receies a signal that either is handled or

    terminates the "rocess: #include

    int pause (void);

    "ause+ , returns onl! if a caught signal is receied%in which case the signal is handled% and "ause+ ,returns '7% and sets errno to EI&T*.

  • 8/12/2019 Signals UNIX

    12/19

    Pause "erforms onl! two actions. $irst% it "uts the"rocess in the interru"tible slee" state. &e#t% it callsschedule+ , to ino0e the Linu# "rocess scheduler tofind another "rocess to run.

    (s the "rocess is not actuall! waiting for an!thing% the0ernel will not wa0e it u" unless it receies a signal.

  • 8/12/2019 Signals UNIX

    13/19

    Execution and Inheritance2hen a "rocess is first e#ecuted% all signals are set

    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 alsoignore those signals.

    Put another wa!% an! signal caught b! the "arent isreset to the default action in the new "rocess% andall other signals remain the same.

  • 8/12/2019 Signals UNIX

    14/19

    Sending a Signal The 0ill+ , s!stem call% the basis of the common kill utility, sends a

    signal from one "rocess to another:

    #include

    #include int kill (pid_t pid, int signo);

    In its normal use +i.e.% if "id is greater than 9,% 0ill+ , sends the signalsigno to the "rocess identified b! "id.

    If "id is 9% signo is sent to eer! "rocess in the ino0ing "rocess3

    "rocess grou". If "id is '7% signo is sent to eer! "rocess for which the ino0ing "rocess

    has "ermission to send a signal% e#ce"t itself and init.

  • 8/12/2019 Signals UNIX

    15/19

    Sending a Signal to Yourself The raise+ , function is a sim"le wa! for a "rocess to

    send a signal to itself:

    #include int raise (int signo);

    This call: raise +signo,8 is euialent to the followingcall: 0ill +get"id + ,% signo,8

  • 8/12/2019 Signals UNIX

    16/19

    eentrancy2hen 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"ortanto"eration that% if interru"ted% would leae the "rocessis an inconsistent state

  • 8/12/2019 Signals UNIX

    17/19

    Signal handlers cannot tell what code the "rocess ise#ecuting when a signal hits8 the handler can run inthe 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

  • 8/12/2019 Signals UNIX

    18/19

    It is a good idea for a signal handler neer to touchglobal data

  • 8/12/2019 Signals UNIX

    19/19

    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 "roided to it b! the

    caller% and must not ino0e an! nonreentrantfunction