Signal, signal Korean Drama
Basic concepts of signals:Each signal has a number and a macro definition name, which can be found in signal. h. Run the kill-l command to view the signal list defined in the system: 1-31 is a common signal; 34-64 is a real-time signal. All signals are sent by the operating system!Three Signal Processing Methods: Ignore the signal, directly execute the default action of the process for the signal, and execute custom actions;
Signal generation conditions:
1. When you press some keys on the terminal, the terminal driver sends a signal to the foreground program. For example, Ctrl-c generates a SIGINT signal, Ctrl-\ generates a SIGQUIT signal, and Ctrl-z generates a SIGTSTP signal. 2. Hardware exception generates a signal. This type of signal is detected by the hardware and notifies the kernel, then the kernel sends an appropriate signal to the current process. For example, if the current process runs commands divided by 0, an exception occurs in the CPU computing unit. The kernel interprets the process as a SIGFPE signal and sends it to the current process. The current process has accessed the invalid memory address, and MMU will generate an exception. The kernel interprets this exception as a SIGSEGV signal sent to the process. 3. A process can call the kill (2) function to send signals to another process. You can use the kill (1) command to send a signal to a process. The kill (1) command is also implemented by calling the kill (2) function. If the signal is not explicitly specified, the SIGTERM signal is sent, the default processing action for this signal is to terminate the process.
Signal generation:1.
Generate signals through Terminal buttonsFor example, write an endless loop, run the program on the foreground, and press Ctrl-c on the terminal. When the CPU is executing the code of this process, the Terminal Driver sends a SIGINT signal to the process and records it in the PCB of the process. The user space code of the process is paused, the CPU switches from the user mode to the kernel mode to handle hardware interruptions. Before returning from the kernel state to the user State, the system processes the signal recorded in the PCB and finds that there is a SIGINT signal to be processed. The default processing action of this signal is to terminate the process, therefore, the process is terminated directly without returning its user space code for execution. 2.
Call system functions to send signals to processes
/************************************************************************* > File Name: test.c > Author:Lynn-Zhang > Mail: iynu17@yeah.net > Created Time: Fri 15 Jul 2016 03:03:57 PM CST ************************************************************************/ #include<stdio.h>int main(){ printf("get pid :%d circle ...\n",getpid()); while(1); return 0;}
Write a program to execute an endless loop in the background, obtain the id of the process, and then use the kill command to send it a SIGSEGV signal to terminate the process. You can also use kill-11 5796,11 to indicate the ID of the signal SIGSEGV. The kill command is implemented by calling the 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 (send a signal to itself)
#include<signal.h>int kill(pid_t pid,int signo);int raise(int signo);
Both functions return 0 successfully, and the error returns-1. In addition, the abort function causes the current process to receive the SIGABRT signal and terminate abnormally.
#include<stdlib.h>void abort(void);
Like the exit function, the abort function always succeeds, so there is no return value. 3.
Signals generated by software conditions
/************************************************************************* > File Name: alarm.c > Author:Lynn-Zhang > Mail: iynu17@yeah.net > Created Time: Fri 15 Jul 2016 08:52:02 PM CST ************************************************************************/#include<stdio.h>int main(){ int count=0; alarm(1); while(1) { printf("%d\n",count); count++; } return 0;}
By implementing the above code, you can call the alarm function to set an alarm clock to tell the kernel to send a SIGALRM signal to the current process after seconds. The default processing action of this signal is to terminate the current process.
The program will count continuously within one second, and print the counter. When one second arrives, it will be terminated by the SIGALRM signal. Because of different computer configurations, the Count value of each computer within one second is generally different.
#include <unistd.h>unsigned int alarm(unsigned int seconds);
The Return Value of the alarm function is 0 or the remaining time of the last set alarm.
Blocking signal:
1. Representation of signals in the kernel:
Signal delivery to delivery: Actually executed Signal Processing
Pending: The status between signal generation and arrival. The signal is generated but not processed.
Ignore: An action after arrival
Block Blocking: When the signal is received, the blocked signal will remain in the pending state until the process unblocks the signal, the arrival action signal generation and blocking are not directly related to arrival and unblocking! After a process receives a signal, it will not be processed immediately and it will be processed at the right time.
Each signal is marked by two flags, respectively, indicating blocking and pending, and a function pointer indicating the signal processing action.
In the example,
1. The SIGHUP signal is neither blocked nor generated. When it is delivered, it performs the default processing action.
2. The SIGINT signal has been generated but is being blocked. Therefore, it cannot be delivered for the time being. Although its processing action is ignored, this signal cannot be ignored before the process can cancel blocking, because the process still has the opportunity to change the processing action and then lift the blocking.
Plug.
3. The SIGQUIT signal has not been generated. Once the SIGQUIT signal is generated, it will be blocked and its processing action is the User-Defined Function sighandler.
A blocked signal set is also called a signal shielding word.
2. Signal Set operation functions
# Include <signal. h> int sigemptyset (sigset_t * set); // initialize the signal set to which the set points, so that the corresponding bits of all signals are cleared by 0int sigfillset (sigset_t * set ); // initialize the signal set pointed to by the set, indicating that the valid signal of the set includes all the signals supported by the system, int sigaddset (sigset_t * set, int signo ); // Add the valid int sigdelset (sigset_t * set, int signo) to the signal set; // Delete the valid int sigismember (const sigset_t * set, int signo); // determines whether a certain signal is contained in a valid signal of a Signal Set.
3. Call the sigprocmask function to read or change the signal shielding characters of a process (blocking the signal set ).
#include <signal.h>int sigprocmask(int how, const sigset_t *set, sigset_t *oset);
If sigprocmask is called to remove the congestion on several pending signals, at least one signal is delivered before sigprocmask returns.
4. sigpending reads the pending Signal set of the current process and transmits it through the set Parameter
#include <signal.h>int sigpending(sigset_t *set);