Linux under the signal

Source: Internet
Author: User
Tags function prototype signal handler sleep function

Signals and interrupts are very similar, except that one is a hardware interrupt and the other is a soft interrupt. Interrupts are the system's response to an asynchronous event.

The simple understanding is that the interrupt source sends the interrupt signal to save the field information before executing the interrupt handler in the Interrupt vector table

The response of an asynchronous event: The process executes code that can be interrupted at any time and then executes the exception handler.

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.

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.

Interrupts are prioritized and interrupts can be masked.

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, such as a DMA interrupt

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.

x86 platform int instruction arm soft interrupt instruction SWI

A signal is an event that occurs when a UNIX system responds to certain conditions, and the process takes action when it receives a signal.

Signals are caused by certain error conditions, such as memory segment collisions, floating-point processor errors, or illegal instructions.

A 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

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.

Process to the signal of three kinds of corresponding

Ignore signal

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

The process cannot ignore the two signals Sigkill, sigstop. If an application can ignore these 2 signals, system management cannot kill, halt, or manage the system. SIGKILL (signal number 9th) and Sigstop signals cannot be captured.

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.

Signal signal installation function is to reset the signal status in the PCB.

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);

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

It's actually the two macros defined above, which is 0 and 1.

Unreliable signals and reliable signals

The Linux signaling mechanism is basically inherited from Unix systems. The signaling mechanism in the early Unix system was relatively simple and primitive, and later exposed some problems in practice, and its main problems were:

Each time the process processes the signal, the response to the signal is set to the default action. In some cases, it will cause error handling of the signal, so if the user does not want to do so, it is necessary to re-install the signal by calling signal () at the end of the signal processing function.

Unreliable signals in the early stages of Unix mainly mean that the process may react incorrectly to the signal and the signal may be lost.

Linux supports unreliable signaling, but improves the unreliable signaling mechanism: after the signal processing function is called, it is not necessary to recall the installation function of the signal (the signal installation function is implemented on a reliable mechanism). Therefore, the unreliable signal problem under Linux mainly means that the signal may be lost.

UNIX signaling mechanism unreliable place, 1) after processing signal, need to re-register the signal, 2) the signal may be lost. Linux has been optimized for 1. Linux mainly refers to the loss of signals, such as sending 3 of signals, may only accept a signal.

Reliable signals, these signals support queueing and are not lost. At the same time, the signal transmission and installation also appeared a new version: Signal transmission function sigqueue () and Signal installation function sigaction ().

Unreliable signals do not support queueing, and reliable signals support queueing.

Signal registration function and signal sending function

Signal Sigaciton signal installation function is called the kernel service do_signal function, the application system cannot directly call the function

int Kill (pid_t pid, int siq)

int Kill (pid_t pid, int siq)

Parameter Combination Case Explanation:

Kill (pid_t pid, int siq)

Pid>0 sends signal SIG to PID process

Pid=0 signal SIG to the same group process

Pid=-1 sends the signal sig to all processes, and the caller process has permission to send each process (in addition to the 1th process and itself)

Pid<-1 sends the signal SIG to each process that the process group is PID (absolute value)

If the signal is installed before the fork, the child process can inherit the signal.

Some notes on the sleep function

1) Sleep function, let the process of sleeping.

2) can be interrupted by the signal, and then after processing the signal function, no longer sleep. Execute code directly down

3) The return value of the sleep function, which is the remaining number of seconds

So if you want to hibernate for a sufficient number of seconds, you need to use the Do and loop.

Raise send a signal to himself. Raise (SIG) is equivalent to Kill (Getpid (), SIG);

The KILLPG sends a signal to the process group. KILLPG (Pgrp, SIG) is equivalent to Kill (-pgrp, SIG);

The sigqueue sends a signal to the process, supports queueing, and can be accompanied by information.

So the function that can send signal to the process has, kill, KILLGP, raise, Sigqueue.

Pause () function

Set the process to an interruptible sleep state. It then calls the kernel function schedule (), which causes the Linux process scheduler to find another process to run. Pause causes the caller process to hang until a signal is captured.

The alarm function sets an alarm delay to send a signal. Tells the Linux kernel to send a SIGALRM signal after n seconds;

Reentrant functions and non-reentrant functions

To enhance the stability of the program, the Reentrant function should be used in the signal processing function.

A reentrant function is a process that can be called by multiple tasks, and the task does not have to worry about whether the data will go wrong when it is called. Since the process receives the signal, it jumps to the signal handler to proceed. If a non-reentrant function is used in a signal-processing function, the signal-processing function may modify data that should not be modified in the original process, so that the process may have unpredictable consequences when it returns to execution from the signal handler function. The non-reentrant function is considered an unsafe function in the signal processing function. Most of the functions that meet the following conditions are non-reentrant: (1) using static data structures such as GetLogin (), Gmtime (), Getgrgid (), Getgrnam (), Getpwuid (), Getpwnam (), and so on (2) when the function is implemented, The malloc () or free () function was called, and (3) The standard I/O function was used when implemented. Use the man 7 signal to find reentrant functions and non-reentrant functions.

Signal blocking and not up

The representation of the signal in the kernel

The processing action of the signal is called the signal recursion (Delivery), the signal from the generation to the state between the recursion, called Signal pending (Pending). A process can choose to block (block) a signal. When the blocked signal is generated, it remains in the pending state until the process has unblocked the signal and executes the recursive action. Note that blocking and ignoring are different, as long as the signal is blocked will not be recursive, and ignoring is the optional after the recursion of a processing action. The recursive signal is ignored, and the callback function cannot be executed.

PCB Process Control block function has a signal block status word (block) signal pending status Word (pending) and whether the flag is ignored, the signal shielding status word (block), 1 for blocking, 0 for non-blocking, signal pending status Word (Pending) 1 is outstanding, 0 means the signal can be reached; Send SIGINT to the process, the kernel first to determine whether the signal screen state word is blocked, the signal pending status word (pending corresponding bits made 1; If the blocking is lifted, the signal pending status word (pending) corresponding bits made 0; signal can be reached. Block status word, pending status Word 64bit;block State Word user can read and write, pending state Word user only read; This is the signal design mechanism. is to control the pending status word indirectly through block, so that they remain in the state.

Signal Set Operation function

#include <signal.h>

int Sigemptyset (sigset_t *set); 64bit/8=8 the signal set condition to a byte buffer

int Sigfillset (sigset_t *set); Put the signal set condition 1

int Sigaddset (sigset_t *set, int signo); According to Signo, the corresponding signal concentration is set to 1

int Sigdelset (sigset_t *set, int signo); According to Signo, the corresponding signal concentration is set to 0

int Sigismember (const sigset_t *set, int signo);//Determine if Signo is in the signal set

The above is only the operation of the signal set, the corresponding signal set is not placed in the kernel, you need to use the following function to operate the corresponding signal mask word.

Read or change the signal mask status word (block) for a process

#include <signal.h>

int sigprocmask (int how, const sigset_t *set, sigset_t *oset);

Function: Read or change the signal screen word of the process.

Return Value: 0 if successful, or 1 if there is an error

If the Oset is a non-null pointer, the current signal screen word of the read process is passed through the Oset parameter. If the set is a non-null pointer, the signal mask word for the process is changed, and the parameter how indicates how to change it. If both the Oset and the set are non-null pointers, the original signal shielding word is backed up into the oset, and the signal screen word is changed according to the set and how parameters. Assuming that the current signal mask is mask,

The data in the corresponding signal word type can be written to the block state word of the process by Sigprocmask.

Sigpending used to obtain signal pending status word (pending) information

The function prototype is this way. int sigpending (sigset_t *set);

So far, the signal setup has been completed, and the rest is the signal sending and processing of the signal.

Linux under the 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.