Process Communication - Unix Signals

  • Upload
    ismimi

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

  • 7/28/2019 Process Communication - Unix Signals

    1/12

  • 7/28/2019 Process Communication - Unix Signals

    2/12

    notifications sent to a process to notify certainevents (errors or other).

    interrupt the normal execution of the process.

    force it to handle them immediately. A signal has an integer representation (1, 2,)

    A signal has also a symbolic name ex.: SIGKILL,SIDUSR1.

    signal names are constants usually defined in/usr/include/signal.h

  • 7/28/2019 Process Communication - Unix Signals

    3/12

    kill -l unix command.It lists all the signals on the system (names and codes).

    Most signals symbolize a certain abnormal event or behavior.

    Example: SIGFPE : arithmetical error. SIGKILL : Terminating signal. The process receiving this signal terminates

    immediately.

    SIGINT : execution interruption (CTRL + C). SIGUSR1 & SIGUSR2 : used for user processes. SIGSTP : suspends execution (CTRL +Z). SIGCONT: signal to continue the execution of a stopped process.fg after a CTRL +Z. SIGSEGV : Segmentation Violation (like wrong addresses).

  • 7/28/2019 Process Communication - Unix Signals

    4/12

    In the Shell: Ctrl-C (SIGINT) : Terminates the process

    Ctrl-Z (SIGTSTOP) : stops the process

    Ctrl-\ (SIGABRT) : Terminates the process From the Command Line kill -

    Send the specified signal to the process having PID asprocess id.

    The parameter signal is an integer (a number or aconstant)

    Ex.:

    Kill -9 2345

    Kill -KILL 2345

  • 7/28/2019 Process Communication - Unix Signals

    5/12

    #include /* signal name macros, and the kill() prototype */

    kill( pid, SIG#)

    Sending the signal having sig# to the specified process.

    Ex.:pid_t my_pid = getpid();

    Kill(mypid, SIGSTOP); /* now that i got my PID, send myselfthe STOP signal. */

  • 7/28/2019 Process Communication - Unix Signals

    6/12

    Each signal may have a signal handler.

    Its a function called when the processreceives that signal.

    signal sent to the process OS stops theprocess execution & calls the signal handler.

    After the signal handler, process continuesexecution.

    In general, signal reception causes thetermination of the process

  • 7/28/2019 Process Communication - Unix Signals

    7/12

    Most signals could be caught by the process.We can define the action to do when receivingthis signal (signal handler).

    Some could not like SIGKILL (-9 ).

    To define a signal handler for a certain process:#include /* signal name macros, and thesignal() prototype */

    signal(SIG#, handler_func);

    Now the function handler_func will be executed when theprocess receives the signal SIG#.

    pause(): causes the process to halt execution,until a signal is received.

  • 7/28/2019 Process Communication - Unix Signals

    8/12

    The signal handler is valid for one and only one signal

    reception

    To let it valid for future signal receptions re-set it usingsignal.

    Signal handler format:

    void sighandler(int sig_num)/* the parameter sig_num is the number of the signal

    received */

    {/* re-set the signal handler for next time */

    signal(SIGINT, catch_int);

    - - -}

    The same handler function could be set to more than onesignal.

  • 7/28/2019 Process Communication - Unix Signals

    9/12

    SIG_IGN: if this handler is set the processignores the specified signal.Ex.:signal(SIG#, SIG_IGN);

    SIG_DFL: its the default signal handlerdefined by the system.Ex.:

    signal(SIG#, SIG_DFL);

  • 7/28/2019 Process Communication - Unix Signals

    10/12

    #include

    #include

    /* signal handler */

    void catch_int(int sig_num)

    {

    /* re-set the signal handler again to catch_int, for next time */

    signal(SIGINT, catch_int);

    /*print the message */printf("Don't do that\n");

    }

    Void main(){

    /* set the (Ctrl-C) signal handler to 'catch_int' */

    signal(SIGINT, catch_int);

    /* wait for signals */for ( ;; )

    pause();

    }

  • 7/28/2019 Process Communication - Unix Signals

    11/12

    SIGALRM: signal used for alarmfunctionnalities.

    #include

    alarm(int n);

    /*asks the sys. To send SIGALRM to the callingprocess after n senconds.*/

    if n = 0 annulate the last alarm call.

    Exercise:Write a program displaying the alphabetical letters

    such that each second it displays one letter.

  • 7/28/2019 Process Communication - Unix Signals

    12/12

    Exercise 1: Write a program ignoring all the signals (if possible.

    Then try to send it several signals from the shell.

    What happens?

    Exercise 2: Write a program that terminates after the 3rd reception of the Ctrl-

    C signal.