Use signal (signal) Instances in perl
This article mainly introduces the use of signal (signal) Instances in perl. This article describes signal-related knowledge and provides perl code examples. For more information, see
Using signal can enrich your program functions. To list all signal in Linux, use kill-l. Below is the output on my machine (which is not listed in 64 ):
The Code is as follows:
Xuyang @ xuyang-desktop:/$ 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
Each signal is described here.
The following is an example. Disable ctrl + c (ctrl + c will generate an INT signal to the current program) to close the current program.
The Code is as follows:
Sub INT_handler {
Print ("Don't Interrupt! \ N ");
}
$ SIG {'int'} = 'int _ handler'; # another way is $ SIG {'int'} =\& INT_handler;
For ($ x = 0; $ x <10; $ x ++ ){
Print ("$ x \ n ");
Sleep 1;
}
Among them, % SIG is the built-in hash of perl, which is specially used to process signals. If you want an ignore signal, you can write it
The Code is as follows:
$ SIG {'int'} = 'ignore ';
Use
The Code is as follows:
$ SIG {'int'} = 'default ';
To send a ctrl + c to a program, in addition to using ctrl + c, you can also type
The Code is as follows:
Kill-s INT.
What is more useful is SIGUSR1 and SIGUSR2, which are customized. You can process these two signals in any way in your program. When you want to trigger this signal, you only need to type
The Code is as follows:
Kill-s USR1 pid
You can trigger this signal processing program. You can enable or disable debugging information in this signal processing program, or switch the working mode. Of course, because signal is a function of the operating system, the language here is not only applicable to perl, but also to other languages. The syntax is different.