Linux signal Practice (1)--linux Signal Programming Overview

Source: Internet
Author: User
Tags signal handler

Interrupts

Interrupts are the system's response to an asynchronous event, which can be interrupted at any time while the process executes the code, and then execute the exception handler;

Interrupt scenario for a computer system: Interrupt source interrupt signal, CPU judgment Interrupt Shield Shield and CPU (query interrupt vector table, find interrupt Service Program entry address) execution interrupt Handler------recovery site, after processing interrupt Continue with the original task

Interrupt classification

Hardware interrupt (external interrupt)

An external interrupt is an interrupt, also known as a hardware interrupt, that is generated by an external device through a hardware request.

Software interrupt (internal interrupt)

An internal interrupt is an interrupt caused by the CPU running a program error or by executing an internal program call, also known as a software interrupt (for example, performing a 0 operation, being trapped in the kernel space by user space, etc.).

Signal

A signal is an event that occurs when a unix/linux system responds to certain conditions, and the process takes action when the signal is received. The signal is usually caused by some error condition, such as memory segment conflict, floating-point processor error or illegal instruction, etc., the signal is a kind of simulation to interrupt at the software level, so it is often referred to as soft interrupt;

The similarity between the signal and the interrupt:

(1) The same asynchronous communication method is adopted;

(2) When a signal is detected or interrupt the request, the execution of the program is suspended to execute the corresponding processing program;

(3) return to the original breakpoint after the processing is finished;

(4) The signal or interrupt can be shielded .

The difference between a signal and an interrupt:

(1) The interrupt has priority, and the signal has no priority , all the signals are equal;

(2) The Signal processing program is running under the user state, and the interrupt processing program is running under the kernel mentality;

(3) The interrupt response is timely, and the signal response usually has a large time delay .

Common signals

Signal Name

Describe

SIGABRT (6)

Process stops running

Sigalrm

Warning Clock

SIGFPE

Calculate an operation exception (e.g., except 0)

SIGHUP

System hangs up

Sigill

Illegal instructions

SIGINT (2)

Terminal interrupt

SIGKILL

Stop process (this signal cannot be ignored or captured)

Sigpipe

Write data to a pipeline without readers

SIGSEGV

Invalid memory segment Access

Sigquit

Terminal exit

SIGTERM

Terminate

SIGUSR1

User-defined Signal 1

SIGUSR2

User-defined Signal 2

SIGCHLD

Child process has stopped or exited

Sigcont

If it is stopped, continue execution

SIGSTOP

Stop execution

Sigtstp

Terminal Stop signal

Sigtout

A background process requests a write operation

Sigttin

A background process requests a read operation

The response of the process to the signal

Ignore signal

No action is taken and two signals cannot be ignored: Sigkill and Sigstop.

[Why the process cannot ignore the sigkill/sigstop signal. (If the application can ignore these 2 signals, system management cannot kill, halt the process, and the system cannot be managed.) )]

capture and process signals

The kernel interrupts the code being executed and goes to execute a previously registered handler.

perform the default action

The default action is usually to terminate the process, depending on the signal being sent.

Default action for signal: View via man 7 signal

signal Installation -signal
typedef void (*__sighandler_t) (int), #define SIG_ERR ((__sighandler_t)-1) #define SIG_DFL ((__sighandler_t) 0) #define Sig_ign ((__sighandler_t) 1) __sighandler_t signal (int signum, __sighandler_t handler);

Parameters

Signal is a function with two parameters of Signum and handler, the signal to be captured or masked is given by the parameter Signum, and the function to be called when the specified signal is received is given by handler

Handler This function must have an int type parameter (that is, the received signal code), which itself is of type void, handler can also be the following two special values:

sig_ign Shielding the signal

SIG_DFL Restore default behavior

Example 1void handler (int sigNum) {    cout << "recv a signal =" << sigNum << Endl;} int main (int argc, char *argv[]) {    signal (SIGINT, handler);    while (true)        sleep (1);}

RETURN VALUE

Signal () returns the previous value of the signal handler, or Sig_err on error.

In the event of a error, errno is set to indicate the cause.

int main (int argc, char *argv[]) {    sighandler_t Oldhandler = Signal (SIGINT, handler);    if (Oldhandler = = Sig_err)        err_exit ("Signal error");    while (GetChar ()! = ' \ n ')        sleep (1);    Equivalent to signal (SIGINT, SIG_DFL)    if (signal (SIGINT, oldhandler) = = Sig_err)        err_exit ("Signal error");    while (true)        sleep (1);}
Example: Capturing all signals and printing the information of the signal int main () {for    (int i = 1; i < NSIG; ++i)    {        if (signal (i, sighandler) = = Sig_err) 
   {            cerr << "signal" << I << ":" << strsignal (i) << "set error";            Cerr << ", errno desc:" << strerror (errno) << Endl;        }    }    while (true)        pause ();} void Sighandler (int signo) {    cout << "Catch a signal, Signo =" << signo << ", desc:" << STRs Ignal (Signo) << Endl;}


Linux signal Practice (1)--linux Signal Programming Overview

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.