linux-signal mechanism detailed (I.)

Source: Internet
Author: User
Tags sigint signal terminates

Before there is a semaphore mechanism written over SYSTEMV, it is now a signal. The signal here is different from the signal in front. The signal here is a process to the operating system or process of some kind of information, let the operating system or other processes to make some kind of reaction.

The signal is the only asynchronous communication mechanism in the interprocess communication mechanism , and a process does not have to wait for the signal to arrive through any operation, in fact, the process does not know when the signal arrives exactly. Processes can send soft interrupt signals to each other through system call kill. The kernel can also send a signal to the process because of an internal event, notifying the process that an event has occurred. In addition to the basic notification function, the signaling mechanism can also pass additional information.


Here are some examples of common signals:

1. The user enters a command to start a foreground process under the shell.

2. The user presses CTRL-C, this keyboard input produces a hardware interrupt.

3. If the CPU is currently executing code for this process, the user-space code for the process pauses execution and the CPU

Switch to a kernel-state processing hardware interrupt.

4. The terminal driver interprets the ctrl-c as a SIGINT signal, which is recorded in the PCB of the process (it can also be said to send a

A SIGINT signal to the process).

5. Before the user-space code returning from the kernel to the process continues to execute at a certain time, it is necessary to process the PCB

SIGINT signal to be processed, the default processing action with this signal is to terminate the process,

A user-space code execution that terminates the process directly and no longer returns it.


The foreground process may press the CTRL-C key at any time during operation to produce a signal, that is, the process's user space code execution to any ground value may receive the SIGINT signal and terminate, so the signal is asynchronous (asynchronous) relative to the process control flow.


One. Signal type:

A lot of signals are defined in Linux signal.h, and a list of system-defined signals can be viewed using the command kill-l:



Can be found in fact no 32, signal number 33rd. Signal number 1-31 is called a normal signal, signal number 34-64 is called real-time signal.

Command kill signal Sequence number process can send a signal to a process


Two. Conditions for signal generation:

1. When the user presses certain keys at the terminal, the terminal driver sends a signal to the foreground process, for example CTRL-C generates SIGINT signal, Ctrl-\ generates SIGQUIT signal, Ctrl-z generates SIGTSTP signal (can make foreground process stop)

The default processing action of the SIGINT is to terminate the process, and the default processing action of Sigquit is to terminate the process and core Dump, so we can write a dead loop to terminate the process by sending a SIGINT (CTRL-C) sigquit (ctrl-\) signal to the process.



You can see that the process has been terminated, and ctrl-c doesn't seem to be working ....


Explain coredump:

When a process terminates abnormally, it is possible to save all the user space memory data of the process to disk, usually the core, which is called core Dump. Process abort is usually due to a bug, such as an illegal memory access caused by a segment error, you can later use the debugger to check the core file to find out the cause of the error, this is called Post-mortem Debug. This process allows multiple cores to be generated depending on the resource Limit of the process (this information is stored in the PCB). The default is not to allow the core to be generated, because the core file may contain sensitive information such as user passwords, unsafe. This restriction can be changed with the Ulimit command during the development debugging phase, allowing the creation of core files.


2. Hardware anomalies generate signals that the hardware detects and notifies the kernel, and the kernel sends the appropriate signal to the current process. For example, the current process executes the order divided by 0, and the CPU's operating unit generates an exception, which the kernel interprets as a SIGFPE signal sent to the process. If the current process accesses an illegal memory address, the MMU generates an exception and the kernel interprets the exception as a SIGSEGV signal sent to the process.

The dead Loop program is executed in the background first, and then the KILL command is used to send the SIGSEGV signal to it. put the process in the background by adding a & to the executable file :


You can see that the process was killed.

The KILL command is implemented by the tune kill function. The KILL function can send a specified signal to a specified process.
The Raise function can send a specified signal to the current process (self-signaling itself).

<span style= "Font-family:microsoft yahei;font-size:14px;" > #include <signal.h>int Kill (pid_t pid, int signo); int raise (int signo);</span>

Both functions return 0 successfully, and the error returns-1.
The Abort function terminates abnormally when the current process receives a SIGABRT signal.

<span style= "Font-family:microsoft yahei;font-size:14px;" > #include <stdlib.h>void abort (void);</span>

Just like the Exit function, the Abort function is always successful, so there is no return value.


3. A process call Kill (2) function can send a signal to another process. The Kill (1) command can be used to send a signal to a process, and the Kill (1) command is also called The Kill (2) function, if the signal is not explicitly specified to send a sigterm signal, the signal's default processing action is to terminate the process. When the kernel detects that a certain software condition occurs, it can also notify the process by signaling, for example, the alarm time-out generates a SIGALRM signal and generates a sigpipe signal when writing data to a pipe that has been closed by the reader. If you do not want to process the signal by default action, the user program can call the Sigaction (2) function to tell the kernel how to handle a signal

There are three types of processing options available:

(i) ignore this signal.

(b) Perform the default processing action for the signal.

(c) Providing a custom signal processing function requires the kernel to switch to the user state when processing the signal to execute this handler, which is called a catch (catch) signal.


Cases:

Sigpipe is a signal generated by a software condition.

<span style= "Font-family:microsoft yahei;font-size:14px;" > #include <unistd.h>unsigned int alarm (unsigned int seconds);</span>

Call the alarm function to set an alarm that tells the kernel to send a SIGALRM signal to the current process after seconds seconds, and the default processing action is to terminate the current process. The return value of this function is 0 or the number of seconds remaining for the previously set alarm time. For example, someone to sleep, set the alarm clock for 30 minutes after the sound, 20 minutes later woke up, and want to sleep a little more, so reset the alarm clock for 15 minutes after the ring, "the previous set of alarm time remaining time" is 10 minutes. If the seconds value is 0, the table cancels the previously set alarm, and the return value of the function is still the number of seconds remaining for the previously set alarm time.


The function of this program is to keep counting in 1 seconds, and 1 seconds to be SIGALRM signal termination.


Three. Blocking signal

The actual processing action of the signal is called the signal recursion (Delivery), the signal from the production to the state between the delivery, called signal Pending (Pending). A process can choose to block (block) a signal. The blocked signal will remain in the pending state until the process has unblocked the signal, only to hold the action of recursion. Note that blocking and ignoring are different as long as the signal is blocked, it will not be recursive,? Ignore is the optional after recursion. Type of processing action. The signal in the kernel table? can be seen as:


Three ways to combine signal processing:

1. Sighup signal is not blocked or not produced, when it is handed to the default processing action.

2. SIGINT signal production, but is being blocked, so temporarily can not reach. Although its processing action is ignored, this signal cannot be ignored until it is unblocked because the process still has the opportunity to change the processing action before releasing the plug.

3. Sigquit signal not produced? Sigquit signal will be blocked, its processing action is? Define function Sighandler.


Conventional signal before the delivery of a number of times only to count,? Real-time signal before the delivery of a number of times can be placed in the queue? Therefore, the pending and blocking flags can be the same data type sigset_t to be stored, sigset_t is called the signal set, this type can be table? The "active" or "efficient" state of each signal, the meaning of "active" and "effective" in the blocking signal set is whether the signal is blocked, or not in the pending signal set " The meaning of "effective" and "efficient" is whether the signal is in a pending state.

(i) Signal set operation function

Adjust the following function to manipulate the sigset_t variable

#include <signal.h>int sigemptyset (sigset_t *set);? int Sigfillset (sigset_t *set);? int Sigaddset (sigset_t *set, int signo);? int Sigdelset (sigset_t *set, int signo);? int Sigismember (const sigset_t *set, int signo);
(b) Signal shielding word

The function Sigprocmask can read or change the signal mask word (block signal set) of the process.

#include <signal.h>?int sigprocmask (int how, const sigset_t *set, sigset_t *oset);

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


If Oset is a null pointer, the current signal mask for the read process is passed through the Oset parameter. If set is a null pointer, change the signal screen word of the process, how does the parameter mean? How to change. If both the Oset and set are null pointers, the original signal shielding word is backed up to Oset first, and then the signal screen word is changed according to the set and how parameters. Assuming that the current signal mask is mask, the following table describes the optional values for the How parameter.


If the tune-up sigprocmask the blocking of the current pending signal, before the Sigprocmask returns, the major General, one of which is a signal delivery.


(iii) Outstanding signal set

#include <signal.h>int sigpending (sigset_t *set);

Sigpending reads the outstanding signal set of the current process and passes through the set parameters. The success of the tune returns 0, and the error returns-1.

linux-signal mechanism detailed (I.)

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.