[Learning Notes] signal basic concepts (interrupts and Signals)/name and common signal/Signal processing/signal function Practice

Source: Internet
Author: User

1 Basic Concepts

Interrupt

Q Interrupts are system responses to asynchronous events

Q Interrupt Signal

Q Interrupt Source

Q Site Information

Q Interrupt Handler

Q Interrupt Vector table

Asynchronous event Response: Process Execution code can be interrupted at any time, then go to execute exception handler

Interruptions in life and interruptions in computer systems

1) Non-disruptive life scene

Zhang San reading, kitchen boiling water

2) Interrupted life scene

Zhang San reading, set alarm clock, kitchen boil water.

Alarm Clock signal interrupt, Zhang San the book (page 20th), go to the kitchen to deal with boiling water, Zhang San reopen 20 pages to read.

3) Interrupt scenario for the computer system

Interrupt source signal interrupt, CPU to determine whether the interrupt shielding shielding, protection site, CPU execution interrupt handler, CPU recovery site, continue the original task.
4) Other concepts of interruption

The Interrupt vector table holds the entry address of the interrupt handler.

The number of interrupts is fixed, and the interrupt vector table is initialized when the operating system starts.

Interrupt has priority (someone knocks, someone calls, has priority)

Interrupts can be masked (Zhang San can block the phone).

Interrupt classification

Q 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.

Q Software Interrupt (internal interrupt)

An internal interrupt is an interrupt, also known as a software interrupt, caused by the CPU running a program error or by executing an internal program call.

Q x86 platform int instruction arm soft interrupt instruction SWI

Signal concept

The Q signal is an event generated by the UNIX system responding to certain conditions, and the process takes action when it receives the signal.

Q signals are generated due to certain error conditions, such as memory segment collisions, floating-point processor errors, or illegal instructions.

The q signal is a simulation of interrupts at the software level, so it is often called a soft interrupt

The difference between a signal and an interrupt

Q signals are similar to interrupts:

Q (1) Adopts the same asynchronous communication mode;

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

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

Q (4) the signal or interrupt can be shielded.

The difference between the Q signal and the interrupt:

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

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

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

2 signal name and common signal

Signal name          description

sigabrt  process stops running 6

SIGALRM warning Clock

sigfpe      calculation exception

sighup     system hangs up

sigill       illegal directive

sigint       Terminal interrupt  2

sigkill     stop process (this signal cannot be ignored or captured)

sigpipe    Write data to a pipe without readers

sigsegv  Invalid memory segment access

 

sigquit   terminal exit    3

SIGTERM Terminating

sigusr1  user-defined signal 1

sigusr2  user-defined signal 2

sigchld  child process has stopped or exited  

Sigcont If stopped, continue execution

sigstop   stop execution

sigtstp   terminal stop signal

sigtout  background Process request write

sigttin   background Process Request read Operation

 

Experiment 1:

Kill–l     can view the signals supported by the Linux kernel

Man 7 signal View the default action of the signal, meaning of the signal

3 Signal Processing

Process to the signal of three kinds of corresponding

Q Ignore Signal

No action is taken and two signals cannot be ignored: SIGKILL (signal 9th) and Sigstop.

Think 1: Why the process cannot ignore Sigkill, sigstop signals. (If the application can ignore these 2 signals, system management cannot kill, halt the process, and the system cannot be managed.) )。 SIGKILL (signal number 9th) and Sigstop signals cannot be captured.

Q Capture and process signals

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

Q Perform the default action

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

Default operation of the signal: viewing through the man 7 signal process

Mans 7 Signal

Programming Practice: Let applications capture CTRL + C signals

The CTRL + C will produce an interrupt. The current application captures the CTRL + C interrupt signal.

4 signal signal Function programming practice

Signal Signal Installation function

Signal function, function 1: Stand in the application's perspective, register a signal processing function.

Function 2: Ignore the signal, set the signal default processing signal installation and recovery

q typedef void (*__sighandler_t) (int);

Q #define SIG_ERR ((__sighandler_t)-1)

Q #define SIG_DFL ((__sighandler_t) 0)

Q #define SIG_IGN ((__sighandler_t) 1)

Q Function Prototype:

__sighandler_t signal (int signum, __sighandler_t handler);

Q parameter

Q 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

Q Handler This function must have an int type parameter (that is, the received signal code), which itself is of type void

Q Handler can also be the following two special values:

Sig_ign Shielding the Signal

SIG_DFL Restore default behavior

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/wait.h>#include<signal.h>/*typedef void (*sighandler_t) (int); sighandler_t signal (int signum, sighandler_t handler);*/intG_tag =1; __sighandler_t old;//The Linux kernel calls this functionvoidMyhandle (intnum) {printf ("recv signal ID num:%d\n", num); if(3==num) {G_tag=0;
Signal (SIGINT, SIG_DFL);
Signal (SIGINT, old); } return;}intMainvoid){ //installation of the signal old = signal (SIGINT, Myhandle) if(Signal (SIGINT, myhandle) = =Sig_err) {Perror ("Signal Err"); return 0; } if(Signal (sigquit, myhandle) = =Sig_err) {Perror ("Signal Err"); return 0; }/*Invalid parameter if (signal (SIGKILL, myhandle) = = Sig_err) {perror ("signal ERR"); return 0; }*/ while(G_tag); return 0;}

[Learning Notes] signal basic concepts (interrupts and Signals)/name and common signal/Signal processing/signal function Practice

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.