Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the signal function?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the signal function? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

C traps and pitfalls explains the signal function in great detail.

Medium

Void (* signal (int sig, void (* handler) (int) (int)

Int (* p) ()

This is a function pointer. The function p points to is a function that takes no arguments and returns int.

Int (* fun ()) ()

The difference between this formula and the above formula is that fun () is used instead of p, and fun () is a function, so it can be regarded as a function pointer after the function fun () is executed, and the function pointer (actually the p above) points to a function without any parameters and the return value is int.

Void (* signal (int signo, void (* handler) (int)) (int) can be thought of as the signal () function (itself is a function with two arguments, one is an integer and the other is a function pointer), and the return value of this signal () function is also a function pointer, which points to a function with an integer parameter and the return value is void.

When writing a signal processing function, the signal processing function is also void sig_fun (int signo); this type is exactly the same as the function pointer returned by the signal () function above.

Void (* signal ()) (int)

Signal is a function that returns a function pointer that points to a function that accepts an integer parameter and does not return a value. Look carefully whether it is the second parameter of siganal (int signo, void (* handler) (int)). By the way, what he returns is the second signal processing function of signal. Pointing to the signal processing function, you can execute the function (inside signal). Signal passes the signal as a parameter to the handler signal processing function, and then signal

When the function returns a pointer and points to the signal processing function, it starts to execute)

So, what are the parameters of the signal function? The signal function takes two arguments: a signal number of an integer and a pointer to a user-defined signal processing function. We have previously defined a pointer sfp to the user-defined signal processing function:

Void (* sfp) (int)

The type of sfp can be obtained by removing sfp from the above declaration, that is, void (*) (int). In addition, the return value of the signal function is a pointer to the user-defined signal processing function before the call, which is of the same type as the sfp pointer. Therefore, we can declare the signal function as follows:

Void (* signal (int, void (*) (int) (int)

Similarly, the above function declaration can be simplified using typedef:

Typedef void (* HANDLER) (int); HANDLER signal (int, HANDLER)

Let's look at a simple example:

# include # include # include void sigroutine (int dunno) {/ * signal processing routine, where dunno will get the value of the signal * / switch (dunno) {case 1: printf ("Get a signal-SIGHUP\ n"); break; case 2: printf ("Get a signal-SIGINT\ n") Break; case 3: printf ("Get a signal-SIGQUIT\ n"); break;} return;} int main () {printf ("process id is% d\ n", getpid ()); signal (SIGHUP, sigroutine); / / * set three signal processing methods signal (SIGINT, sigroutine) Signal (SIGQUIT, sigroutine); for (;);}

The signal SIGINT is sent by pressing Ctrl-C, and the signal SIGQUIT is sent by pressing Ctrl-. The results of the program execution are as follows:

[zcm@t # 29] $makegcc-g-c-o a.o a.cgcc-g-o an a.o [zcm@t # 30] $. / aprocess id is 7666 ^ C Get a signal-- sign ^ Z [1] + Stopped. / a [zcm@t # 31] $ps aux | grep. / aroot 1164 0.0 13320 748? S

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report