[Linux Basic] Basics-Signal

Source: Internet
Author: User

Thread-"signal
Signal is an event that occurs when UNIX and Linux systems respond to certain conditions. The process that receives the signal takes some action accordingly.
Raise generation represents the generation of a signal
Catch capture indicates the generation of a signal received, which is caused by certain error conditions, memory segment collisions, floating-point processor errors, or illegal instructions. They are generated by the shell and the terminal processor
Cause interrupts, which can be explicitly sent to another process as a process, as a way of communicating messages or modifying behavior. In any case, their programming interfaces are the same, and the signals can be
Generate, capture, respond, ignore.
signal.h======

Sigabort * Process Abnormal termination
SIGALRM Timeout Warning
SIGFPE * Floating-point arithmetic error
Sighup Link hang-off
Sigill * Illegal Instruction
SIGINT Terminal Interrupt
Sigkill Terminate process (this signal cannot be ignored or omitted)
Sigpipe writing data to a pipeline without a read process
Sigquit Interrupt Exit
SIGSEGV * Invalid memory segment access
sigterm termination
Siguser1 user-defined signal
SIGUSER2 user-defined signal 2
Note: * Indicates that the system's response to the signal varies depending on the implementation
-----------If the process accepts one of these signals, but without prior arrangement, the process will be terminated immediately. Typically, the system produces core dump file cores and places them in the current
Directory. The file is the image of the process in memory, and he is very helpful in debugging the program.


Other signals
SIGCHLD child process has stopped or exited
Sigcont continue execution of the pause process
Sigstop Stop Execution (this signal cannot be captured or ignored)
SIGSTSP Terminal hangs
Sigttin Background Process attempt read operation
Sigttou Background Process attempt write operation



If the shell and terminal driver are configured as usual, typing the interrupt character (CTRL + C) on the keyboard will cause the SIGINT signal to forward (that is, the currently executing program), which causes the
The process terminates unless it is previously scheduled to capture the signal.


If you want to send a command to a process, but the process is not the current foreground process, you need to use the KILL command, which requires
An optional signal code or signal name and
PID of a target process receiving signal (PID usually requires PS command view)
Kill-Sends a command to a process where the commonly used signals include hup,int,kill,stop,cont,0.

The program can use the Signal library function to process the signal,
#include <signal.h>
void (*signal (int sig,void (*FUNC) (int)))) (int);
Signal is a function with a sig and func two parameters;
The signal to be captured/ignored is given by the parameter sig, and the function to be called after receiving the specified signal is given by the parameter func,
The signal processing function must have an int type parameter (that is, the received signal code) and the return type is void,
The signal function itself also returns a function of the same type, the function previously used to process the signal (not the Func function);
Returns a function pointer to the signal handler function that was previously processed for the specified signal, returns SIG_ERR if no signal handler is defined, and sets errno as a positive integer value.
If an invalid signal is given, or the signal being tried is not captured/not negligible (for example, Sigkill), the errno will be set to Einval.
Special VALUES (SIG_IGN,SIG_DFL) can also be used instead of signal processing functions;
Sig_ign Ignore Signal
SIG_DFL Restore the default signal


-----------------------
Signal Processing Example: Write a program ctrlc.c, in response to user-typed CTRL + C key combination, print an appropriate message on the screen instead of terminating the program's operation; Ctrl + C again, end.
#include <signal.h>#include<stdio.h>#include<unistd.h>voidOuchintSIG) {printf ("ouch!-I got signal%d\n", SIG); (void) signal (SIGINT,SIG_DFL);}intMain () {(void) signal (Sigint,ouch);  while(1) {printf ("Hello world\n"); Sleep (1); }}
The function of main () is to capture the SIGINT signal generated by CTRL + C, and when no signal is generated, it will print characters in an infinite loop;
(void) signal (Sigint,ouch), after capturing the SIGINT signal, calls the Ouch function for processing;
(void) signal (SIGINT,SIG_DFL), capturing the SIGINT signal again, SIG_DFL to restore the default behavior of the signal;
------------



[Linux Basic] Basics-Signal

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.