Linux

 

 

 

 

IPC : Signal

 

Signal is a kind of mechanism for IPC (Inter Process Communication). That is, it is a mechanism by which a Process can communicate with other process. The way it works is very similar to the way an interrupt works. Overall procedure in which a Signal works is as follows.

    i) A Process request the operating system to send a Signal to a specified target processor.

    ii) The target processor receiving the signal execute a specific (a predefined) routine

 

 

 

How to know what kind of signal is supported ?

 

The types of Signal is determined by Operating System. you can figure out what kind of Signal is supported by your Linux by using 'kill -l' command as shown below. (This is from Ubuntu 16.4)

 

 

    $ kill -l

     

     1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP

     6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1

    11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM

    16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP

    21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ

    26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR

    31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3

    38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8

    43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13

    48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12

    53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7

    58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2

    63) SIGRTMAX-1  64) SIGRTMAX    

 

 

 

Handling Signal - signal()

 

 

signal_01.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <signal.h>

 

void sig_handler_02(int signum)

{

    printf("\n[^C] Signal (%d) Recieved \n", signum);

}

 

void sig_handler_06(int signum)

{

    printf("\n[ABORT] Signal (%d) Recieved \n", signum);

}

 

void sig_handler_10(int signum)

{

    printf("\n[USER SIGNAL] Signal (%d) Recieved \n", signum);

}

 

int main()

{

    // register sig_handler_02() as the handler for the signal SIGINT

    signal(SIGINT, sig_handler_02);  

 

    // register sig_handler_06() as the handler for the signal SIGABRT

    signal(SIGABRT, sig_handler_06);

    

        // register sig_handler_10() as the handler for the signal SIGUSR1

    signal(SIGUSR1, sig_handler_10);

 

    printf("\nThe process is waiting for 120 sec before exit\n");

    sleep(120);

    printf("\nThe process quits\n");

 

    return 0;

}

 

 

Compile the code as shown below.

    $ gcc signal_01.c -o signal_01

     

List files and confirm that the executable file is created.

    $ ls -al

     

    drwxrwxr-x  2 jaekuryu jaekuryu 4096 Feb 23 21:43 .

    drwxr-xr-x 22 jaekuryu jaekuryu 4096 Feb 23 17:34 ..

    -rwxrwxr-x  1 jaekuryu jaekuryu 8880 Feb 23 21:43 signal_01

    -rw-rw-r--  1 jaekuryu jaekuryu  626 Feb 23 21:43 signal_01.c

     

Run the executable

    $ ./signal_01

     

    The process is waiting for 120 sec before exit

 

Press ^C and you will have the following print and the process get killed.

    [^C] Signal (2) Recieved

     

    The process quits

     

 

In the test menthod shown above, you can send only signal id 2 (^C). What if you want to send other signal ID. You can do this with 'kill' command as below.

 

In order to run kill command while my process is running. To do this, I will run the program in the background

    $ ./signal_01 &

     

    [1] 7011

     

    The process is waiting for 120 sec before exit

 

At this status, you cannot run other command yet because it still does not get back to command prompt. To get to the command prompt mode, press ^C and then you will get the command prompt.

 

Then try ps and you will find the process signal_01 still running as shown below.

    $ ps

     

      PID TTY          TIME CMD

     3622 pts/4    00:00:00 bash

     7011 pts/4    00:00:00 signal_01

     7014 pts/4    00:00:00 ps

     

Now let's send a signal (ID 6) to the process signal_01 as shown below.

    $ kill -6 7011

     

    [ABORT] Signal (6) Recieved

     

    The process quits

 

Still I don't get the command prompt. Press ^C to get back to the command prompt.

    ^C

    [1]+  Done                    ./signal_01

 

Try ps and see if the process signal_01 is still running. As you see, the signal_01 is gone.

    $ ps

      PID TTY          TIME CMD

     3622 pts/4    00:00:00 bash

     7017 pts/4    00:00:00 ps

     

 

In the same as shown above. Let's try with signal -10 as shown below. I will not explain on each of the steps since it is already explained above.

 

    $ ./signal_01 &

     

    [1] 7018

     

    The process is waiting for 120 sec before exit

     

    ^C

     

     

    $ ps

      PID TTY          TIME CMD

     3622 pts/4    00:00:00 bash

     7018 pts/4    00:00:00 signal_01

     7019 pts/4    00:00:00 ps

     

     

    $ kill -10 7018

     

    [USER SIGNAL] Signal (10) Recieved

     

    The process quits

    [1]+  Done                    ./signal_01

     

     

    $ ps

     

      PID TTY          TIME CMD

     3622 pts/4    00:00:00 bash

     7022 pts/4    00:00:00 ps