Using signal (signal) instances in Perl _perl

Source: Internet
Author: User
Tags signal handler

Using signal, you can make your program more functional. To get all the signal under Linux, use the kill-l. Here is the output on my machine (which is not listed in the following 64):

Copy Code code 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 SIGUSR1
SIGSEGV) SIGUSR2) sigpipe sigalrm) sigterm
Sigstkflt) sigchld) Sigcont SIGSTOP) SIGTSTP
Sigttin) Sigttou) Sigurg sigxcpu) Sigxfsz
SIGVTALRM) sigprof) sigwinch Sigio) SIGPWR
) Sigsys

Each of these signal is introduced here.
The following is an example of a ban on CTRL + C (CTRL + C will produce an int signal to the current program) to close the current program

Copy Code code 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 < $x + +) {
Print ("$x \ n");
Sleep 1;
}

One of the%sig is Perl's built-in hash, which is designed to handle signals. If you want to ignore a signal, you can write

Copy Code code as follows:

$SIG {' INT '} = ' IGNORE ';

To revert to the default condition, use the
Copy Code code as follows:

$SIG {' INT '} = ' DEFAULT ';

To send a CTRL + C to a program, in addition to using CTRL + C, you can also type
Copy Code code as follows:

Kill-s INT.

The more practical value is SIGUSR1, SIGUSR2, the two signal are custom-defined. You can handle the two signals in any way you want in your program. And when you want to trigger this signal, just type it in Linux
Copy Code code as follows:

Kill-s USR1 PID

Can trigger this signal handler, you can open and close debugging information in this signal handler, or switch working mode and so on. Of course, because signal is the function of the operating system, the language here is not just for Perl, but for other languages as well. It's just a different grammar.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.