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

How to use signal function in Linux

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use the signal function in Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The simplest interface of signal mechanism in Linux system is signal function. Function of the signal function: installs a new signal processing function for the specified signal.

1. Function

Set the corresponding action of a signal

two。 Statement # include typedef void (* sighandler_t) (int); sighandler_t signal (int signum, sighandler_t handler); 3. Parameter description

The first parameter, signum: indicates the type of signal to be processed, which can take any kind of signal except SIGKILL and SIGSTOP. The second parameter, handler: describes the actions associated with the signal, which can take the following three values:

(1) SIG_IGN

This symbol indicates ignoring the signal. For example:

# include # include int main (int argc, char * argv []) {signal (SIGINT, SIG_IGN); while (1); return 0;}

SIGINT signals are generated by InterruptKey, usually CTRL + C or DELETE. When the above code is executed, pressing the CTRL + C program does not respond. That's right, if we want to end the program, we can press CTRL +\ to finish. When we press CTRL +\, a SIGQUIT signal is generated, which is not ignored.

(2) SIG_DFL

This symbol indicates that the system default processing of the signal is restored. Do not write this handler, the default is also to perform the system default action. For example

# include # include int main (int argc, char * argv []) {signal (SIGINT, SIG_DFL); while (1); return 0;}

At this point you can press CTRL + C to terminate the process. Remove this sentence from signal (SIGINT, SIG_DFL), and the effect is the same.

(3) the function pointer of sighandler_t type mentions the sighandler_t type declaration:

Typedef void (* sighandler_t) (int); sighandler_t signal (int signum, sighandler_t handler)

This function must be declared before signal () is called, which is the name of the function in handler. When a signal of type sig is received, the function specified by handler is executed. (int) signum is the only parameter passed to it. After executing the signal () call, the process executes the func () function as soon as it receives a signal of type sig, regardless of which part of the program it is executing. When the execution of the func () function ends, control returns to the point where the process was interrupted to continue execution. For example

# include # include typedef void (* signal_handler) (int); void signal_handler_fun (int signum) {printf ("catch signal% d\ n", signum);} int main (int argc, char * argv []) {signal (SIGINT, signal_hander_fun); while (1); return 0;}

When executed, when we press the CTRL + C key, the signal processing function we defined will be executed.

Catch signal 2catch signal 2catch signal 2catch signal 2 = exit

Whenever we press the CTRL + C key, the number of the signal is printed. You can see that the num of the signal is 2. To exit, press CTRL +\ to print the result as the last line.

4. Function description

Signal () sets the processing function of the signal according to the signal number specified by the parameter signum. When the specified signal arrives, it jumps to the function specified by the parameter handler.

When the signal processing function of a signal is executed, if the process receives the signal again, the signal will be automatically stored without interrupting the execution of the signal processing function, and the corresponding processing function will be called again until the signal processing function is completed. However, if the process receives other types of signals during the execution of the signal processing function, the execution of the function will be interrupted.

5. Return value

Returns the previous signal handler pointer, or SIG_ERR (- 1) if there is an error.

6. Some commonly used Signal: SignalDescriptionSIGABRT is generated by calling the abort function, and the process abnormally exits the timer timeout set by the SIGALRM function or the interval timer timeout SIGBUS set by the setitimer function. Some specific hardware exception is usually caused by memory access that causes the SIGCANCEL to be used internally by the Solaris Thread Library. Usually, when the SIGCHLD process Terminate or Stop is not used, SIGCHLD will send it to its parent process. By default, the Signal will be ignored SIGCONT when the stop process resumes running, automatically sends SIGEMT and implementation-related hardware exceptions SIGFPE math-related exceptions, such as divided by 0, floating-point overflow, etc. SIGFREEZESolaris-specific, send SIGHUP to Controlling Process with Terminal when Hiberate or Suspended, send SIGILL illegal instruction exception SIGINFOBSD signal when terminal is disconnect. Produced by Status Key, usually CTRL+T. The process SIGINT sent to all Foreground Group is generated by Interrupt Key, usually CTRL+C or DELETE. The process SIGIO asynchronous IO event SIGIOT that is sent to all ForeGround Group implements related hardware exceptions, which generally cannot be handled or ignored by SIGABRTSIGKILL. To abort a process SIGLWP is used internally by Solaris Thread Libray to use SIGPIPE to send SIGPOLL when writing Pipe after reader abort. When an event is sent to Pollable Device, the Profiling Interval Timer specified by SIGPROFSetitimer is related to the system. It's related to UPS. SIGQUIT input Quit Key when (CTRL+\) sent to all Foreground Group processes SIGSEGV illegal memory access SIGSTKFLTLinux special, mathematical coprocessor stack exception SIGSTOP aborts the process. Unable to handle and ignore. SIGSYS illegal system call SIGTERM request to abort the process, the kill command sends SIGTHAWSolaris special by default, and sends SIGTRAP to realize the related hardware exception when recovering from Suspend. Usually debug exception SIGTSTPSuspend Key, usually Ctrl+Z. Send to all Foreground Group processes SIGTTIN when Background Group's process tries to read Terminal, when Background Group's process tries to write Terminal, send SIGURG when out-of-band data receives, it may send SIGUSR1 user-defined signal 1SIGUSR2 user-defined signal 2SIGVTALRMsetitimer function Virtual Interval Timer timeout SIGWAITINGSolaris Thread Library internal implementation of dedicated SIGWINCH when the window size of Terminal changes All processes sent to Foreground Group SIGXCPU when the CPU time limit timeout when the SIGXFSZ process exceeds the file size limit SIGXRESSolaris dedicated, when the process exceeds the resource limit on "how to use the signal function in Linux" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

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