本文转载自:Linux Signals – Example C Program to Catch Signals,目的是为了展示如何使用Linux signal。

可以去看Linux Kernel 读书笔记 Signals的前半部分,建立起Linux signal的概念。

In this article we will learn about how to catch signals in a process.

1. Catching a Signal

If a process wishes to handle certain signals, then in the code, the process has to register a signal handling function to the kernel.

The following is the prototype of a signal handling function :
void <signal handler func name> (int sig)

The signal handler function has void return type and accepts a signal number corresponding to the signal that needs to be handled.

To get the signal handler function registered to the kernel, the signal handler function pointer is passed as second argument to the ‘signal’ function. The prototype of the signal function is :
void (*signal(int signo, void (*func )(int)))(int);

This might seems a complicated declaration. If we try to decode it :

  • The function requires two arguments.
  • The first argument is an integer (signo) depicting the signal number or signal value.
  • The second argument is a pointer to the signal handler function that accepts an integer as argument and returns nothing (void).
  • While the ‘signal’ function itself returns function pointer whose return type is void.

Well, to make things easier, lets use typedef :
typedef void sigfunc(int)

So, here we have made a new type ‘sigfunc’. Now using this typedef, if we redesign the prototype of the signal handler :
sigfunc *signal(int, sigfunc*);

Now we see that its easier to comprehend that the signal handler function accepts an integer and a sigfunc type function pointer while it returns a sigfunc type function pointer.

2. Example C Program to Catch a Signal

Most of the Linux users use the key combination Ctr+C to terminate processes in Linux.

Have you ever thought of what goes behind this. Well, whenever ctrl+c is pressed, a signal SIGINT is sent to the process. The default action of this signal is to terminate the process. But this signal can also be handled. The following code demonstrates this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
if (signo == SIGINT)
printf("received SIGINT\n");
}

int main(void)
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
// A long long wait so that we can easily issue a signal to this process
while(1)
sleep(1);
return 0;
}

In the code above, we have simulated a long running process using an infinite while loop.

A function sig_handler is used a s a signal handler. This function is registered to the kernel by passing it as the second argument of the system call ‘signal’ in the main() function. The first argument to the function ‘signal’ is the signal we intend the signal handler to handle which is SIGINT in this case.

On a side note, the use of function sleep(1) has a reason behind. This function has been used in the while loop so that while loop executes after some time (ie one second in this case). This becomes important because otherwise an infinite while loop running wildly may consume most of the CPU making the computer very very slow.

Anyways, coming back , when the process is run and we try to terminate the process using Ctrl+C:

1
2
3
4
5
6
7
8
$ ./sigfunc
^Creceived SIGINT
^Creceived SIGINT
^Creceived SIGINT
^Creceived SIGINT
^Creceived SIGINT
^Creceived SIGINT
^Creceived SIGINT

We see in the above output that we tried the key combination ctrl+c several times but each time the process didn’t terminate. This is because the signal was handled in the code and this was confirmed from the print we got on each line.

3. SIGKILL, SIGSTOP and User Defined Signals

Apart from handling the standard signal that are available. We can also have user defined signals that can be sent and handled. Following is the code handling a user defined signal USR1 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
if (signo == SIGUSR1)
printf("received SIGUSR1\n");
else if (signo == SIGKILL)
printf("received SIGKILL\n");
else if (signo == SIGSTOP)
printf("received SIGSTOP\n");
}

int main(void)
{
if (signal(SIGUSR1, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGUSR1\n");
if (signal(SIGKILL, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGKILL\n");
if (signal(SIGSTOP, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGSTOP\n");
// A long long wait so that we can easily issue a signal to this process
while(1)
sleep(1);
return 0;
}

We see that in the above code, we have tried to handle a user defined signal USR1. Also, as we know that two signals KILL and STOP cannot be handled. So we have also tried to handle these two signals so as to see how the ‘signal’ system call responds in this case.

When we run the above code :

1
2
3
4
5
$ ./sigfunc

can't catch SIGKILL

can't catch SIGSTOP

So the above output makes clear that as soon as the system call ‘signal’ tries to register handler for KILL and STOP signals, the signal function fails indicating that these two signals cannot be caught.

Now we try to pass the signal USR1 to this process using the kill command:
$ kill -USR1 2678
and on the terminal where the above program is running we see :

1
2
3
4
5
6
$ ./sigfunc

can't catch SIGKILL

can't catch SIGSTOP
received SIGUSR1

So we see that the user defined signal USR1 was received in the process and was handled properly.


参考资料:

  1. Linux Signals – Example C Program to Catch Signals