Linux signal signal Introduction, signal () function, sigaction () function __oracle

Source: Internet
Author: User
Tags arithmetic
Signal () function to describe Linux functions in detail


A signal (signal) is a interprocess communication mechanism that provides an application with an asynchronous software interrupt that gives the application the opportunity to accept commands (i.e., signals) sent by other program live terminals. After the application receives a signal, there are three ways to handle it: Ignore, default, or capture. When a process receives a signal, it checks the processing mechanism of the signal. If it's sig_ign, the signal is ignored and, if it is SIG_DFT, the system default processing action is used, usually terminating the process or ignoring the signal, and if a handler function (capture) is given to the signal, the task being performed by the current process is interrupted and the processing function of the signal is executed. Return to execution of the interrupted task.


Sighup Terminate process terminal line hangs up
SIGINT Terminate process Interrupt process
Sigquit establishes core file termination process and generates core files
Sigill establish core file illegal instruction
Sigtrap establishes core file tracking self-trapping
Sigbus Build core File Bus error
SIGSEGV Establish core file segment illegal error
SIGFPE creates a core file floating-point exception
Sigiot build core file to perform I/O self-trapping
SIGKILL Terminate process Kill process
Sigpipe terminates the process to write data to a pipe that does not read the process
Sigalarm Terminate process timer to
Sigterm terminate process software termination signal
SIGSTOP stops the process from terminating the signal.
SIGTSTP stops the process terminal to stop the signal
Sigcont ignores the signal and continues to execute a stopped process
Sigurg Ignore signal I/O emergency signal
Sigio ignores signal descriptors for I/O
SIGCHLD Ignore signal Notify parent process when child process stops or exits
Sigttou Stop process background process write terminal
Sigttin Stop process background process read Terminal
SIGXGPU Terminate process CPU time limit timeout
Sigxfsz Terminate process file length is too long
Sigwinch ignores signal window size changes
SIGPROF Terminate process statistic distribution chart with timer to time
SIGUSR1 Terminate process user-defined signal 1
SIGUSR2 Terminate process user-defined signal 2
SIGVTALRM Terminate process Virtual timer to

1 Sighup This signal at the end of the user terminal connection (normal or abnormal), usually at the end of the control process, notify the same session of each job, when they are no longer associated with the control terminal.
2) SIGINT program termination (interrupt) signal, when the user types Intr characters (usually ctrl-c) issued
3) Sigquit and SIGINT are similar, but are controlled by quit characters (usually ctrl). A process produces a core file when it exits Sigquit, in the sense that it is similar to a program error signal.
4) Sigill executed the illegal instruction. Typically, an error occurs in the executable itself, or an attempt is made to execute a data segment. This signal can also be generated when the stack overflows.
5) The Sigtrap is produced by a breakpoint instruction or other trap instruction. Used by debugger.
6) The SIGABRT program discovers errors and invokes abort when it is generated.
6) Sigiot is produced on PDP-11 by IoT instructions, as in other machines and SIGABRT.
7 Sigbus illegal address, including memory address alignment (alignment) error. Eg: Access a four-word integer, but its address is not a multiple of 4.
8) Sigfpe in the event of a fatal arithmetic operation error. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and divisor 0.
9) SIGKILL is used to immediately end the operation of the program. This signal cannot be blocked, processed and ignored.
SIGUSR1 left to the user to use
One) SIGSEGV attempts to access memory that is not assigned to itself, or attempts to write data to a memory address that does not have write access.
SIGUSR2 left to the user to use
Sigpipe Broken Pipe
SIGALRM clock timing signal, calculated is the actual time or clock time. The alarm function uses this signal.
Sigterm program End (terminate) signal, unlike Sigkill, the signal can be blocked and processed. Usually used to require the program to exit normally. The shell command kill defaults to produce this signal.
SIGCHLD The parent process receives this signal when the child process ends.
Sigcont let a stop (stopped) process continue. This signal cannot be blocked. You can use a handler to allow a program to perform a specific job when the stopped state becomes a continuation. For example, to display the prompt again
SIGSTOP Stop (Stopped) the execution of the process. Notice the difference between it and terminate and interrupt: The process is not finished, it's just suspending execution. This signal cannot be blocked, processed or ignored.
SIGTSTP stops the process from running, but the signal can be processed and ignored. The user types the Susp character characters (usually ctrl-z) to send this signal
Sigttin when a background job is to read data from a user terminal, all processes in the job receive a sigttin signal. By default, these processes will stop executing.
Sigttou is similar to Sigttin, but is received when writing a terminal (or modifying terminal mode).
Sigurg "Emergency" data or Out-of-band data is generated when the socket is reached.
SIGXCPU exceeds the CPU time resource limit. This restriction can be read/changed by Getrlimit/setrlimit
Sigxfsz exceeds the file size resource limit.
) SIGVTALRM virtual clock signal. Similar to SIGALRM, but calculates the CPU time consumed by the process.
Sigprof is similar to SIGALRM/SIGVTALRM, but includes the CPU time used for the process and the time of the system call.
Sigwinch when the window size changes.
Sigio file descriptor is ready to start an input/output operation.
) SIGPWR power failure

There are two signals that can stop the process: Sigterm and Sigkill. Sigterm is more friendly, the process can capture this signal and close the program according to your needs. Before you close the program, you can end the open record file and complete the task you are doing. In some cases, if the process is working and cannot be interrupted, the process can ignore the sigterm signal.

For Sigkill signals, the process cannot be ignored. This is a "I don't care what you are doing, stop immediately" signal. If you send a sigkill signal to the process, Linux stops the process there.


Let's talk about the function of the signal.


The simplest signal function


typedef void (*sighandler_t) (int)

sighandler_t signal (int signum, sighandler_t handler);

Returns the original signal processing function, or sig_err


Signal () is the simplest function to install a signal processor to a process, the first parameter specifies the signal, and the second parameter assigns a handler function to the signal.

The following is a simple signal processing program that captures SIGUSR1, ignores SIGUSR2, defaults to System processing SIGINT,SIGUSR1 and SIGUSR2 are user-defined signals provided by Linux and can be used with any application. The main program does nothing, and only uses pause () to wait for the signal.


Routine 1 simplest signal processing

1.static void Pr_mask (const char * string) {
2. sigset_t Procmask;
3.

4. Sigprocmask (Sig_setmask, NULL, &procmask);
5.

6. printf ("%s:", string);
7. if (Sigismember (&procmask, SIGINT))
8. printf ("SIGINT");
9. if (Sigismember (&procmask, SIGUSR1))
printf ("SIGUSR1");
One. if (Sigismember (&procmask, SIGUSR2))
printf ("SIGUSR2");
if (Sigismember (&procmask, Sigterm))
printf ("Sigterm");
if (Sigismember (&procmask, Sigquit))
printf ("Sigquit");
printf ("/n");
18.}
19.

20.static void sigusr (int signum)
21.{
Pr_mask ("int sigusr");
23.

if (Signum = = SIGUSR1)
printf ("SIGUSR1 received/n");
/else if (Signum = SIGUSR2)
printf ("SIGUSR2 received/n");
Else29. printf ("Signal%d received/n", Signum);
30.}
31.

32.int Main (void)
33.{
if (Signal (SIGUSR1, sig_usr) = = Sig_err) {
printf ("Error catching sigusr1/n");
Exit (1);
37.}
38.

if (Signal (SIGUSR2, sig_ign) = = Sig_err) {
printf ("Error ignoring sigusr2/n");
Exit (1);
42.}
43.

if (Signal (SIGINT, sig_dft) = = Sig_err) {
printf ("Error setting SIGINT to default/n");
A. exit (1);
47.}
48.

while (1)
Pause ();
51.

A. exit (0);
53.}


Run the program in the background and send a signal to it using kill.


$./a.out &

[1] 3725

$kill-USR1 3725

In SIGUSR:SIGUSR1

SIGUSR1 received

$kill-USR2 3725

[1]+ User defined signal 2./a.out

As we can see, the default action of the Linux system for SIGUSR2 is to terminate the process.

Interrupts and automatic restart


As I said before, a signal is a software interrupt mechanism, which creates a problem: what should the system do if a low speed system call is being performed when the signal arrives. is to temporarily block system call return, after the signal processing program completes the system call that does not complete, or let system call error return, at the same time set errno to EINTR, let the caller do further error checking. With the facts, let's do an experiment first.


The following program reads the standard input and outputs it to the standard output, during which we send a SIGUSR1 signal to the process to determine how Linux is invoked on the processing system after receiving the signal.

Signal version of routine 2 signal and automatic restart

1.int Main (void)
0.9
3. Char Buf[bufsiz];
4. int n;
5.

6. Signal (SIGUSR1, SIG_USR);
7.

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.