Brief description: Linux interprocess communication-signal

Signals for communication between Linux processes

Introduction: Introduction: Brief Introduction:

 The signal mechanism is a method of transferring messages between processes. The signal is called a soft interrupt signal, which is used to notify the process of an asynchronous event. Processes can send soft interrupt signals to each other through the system call kill; the kernel can also send signals to the process due to internal events to notify the process that an event has occurred.

 The process that receives the signal has different processing methods for various signals. Processing methods can be divided into three categories:
 1. Similar to interrupt handlers, the process can specify a processing function for the signal that needs to be processed, and the function will handle it.
 2. Ignore a certain signal and do nothing on the signal, as if it had never happened.
 3. The processing of the signal retains the system default value. This default operation, the default operation for most signals is to terminate the process.

Basic terminology of signal Basic terminology of signal Letter number of groups present technique Language

  • The actual signal processing action is called signal delivery (Delivery)
  • The state between signal generation and delivery is called signal pending (Pending).
  • The process can choose to block (Block) a signal.
  • The blocked signal will remain in the pending state when it is generated, and the delivery action will not be performed until the process unblocks the signal.

The life cycle of a process Into the process of green life periphery of

When a signal SIGXXX is sent to the target process, the Linux kernel receives the generated signal, and then records a record in the process descriptor of the target process: the signal SIGXXX is received, but has not been delivered to the target process in this period of time , The signal is in a pending state, it is called pending signal pending signalNo decision letter No . The kernel delivers the signal to the process, and the process suspends the current flow of control and instead executes the signal processing function. This is the complete life cycle of a signal.

Signal classification Signal classification Channel number of the sub- class

不可靠信号:信号值在[1,31]之间的所有信号,被称为不可靠信号;也称非实时信号。对于不可靠信号,内核不一定能递送给目标进程,信号可能会丢失。
可靠信号:在[SIGRTMIN, SIGRTMAX]之间的信号被称为可靠信号;也称为实时信号。可靠信号时为了解决不可靠信号的丢失问题而产生的,Linux内核保证可靠信号能够递送给目标进程而不会丢失。

Characteristics of different types of signals:

  1. For unreliable signals, the kernel uses a bitmap (that is, a soft interrupt signal field in the performance of the process table, each bit in the field corresponds to a signal) to record whether the signal is in a suspended state. If an unreliable signal is received and the kernel finds that the signal is already pending, it will simply discard the signal. Therefore, if an unreliable signal is sent, the signal may be lost, that is, the number of times the kernel delivers to the target process may be less than the number of times the signal is sent.

  2. For reliable signals, there is a queue inside the kernel to maintain. If a reliable signal is received, the kernel will hang the signal in the corresponding queue, so it will not be discarded. Strictly speaking, the kernel also has an upper limit, and the number of pending signals cannot be increased indefinitely, otherwise it consumes kernel resources. Therefore, it can only be said that within a certain range, reliable signals will not be discarded.

To put it simply, unreliable signals are generated multiple times before being delivered and only counted once, while reliable signals are generated multiple times before being delivered and can be placed in a queue in turn

Linux's concrete realization of unreliable signals Linux's concrete realization of unreliable signals L I n- U X to not be against the channel number of the tool body real now

 For unreliable signals (non-real-time signals), each signal has two flag bits to indicate block and pending, and a function pointer to indicate processing actions.

 Each signal has a pending flag of only one bit, which is not 0 or 1. It does not record how many times the signal is generated, and the blocking flag is also represented in this way. Therefore, pending and blocked flags can be stored with the same data type sigset_t, which is called a signal set.

This type can represent the "valid" or "invalid" status of each signal. The meaning of "valid" and "invalid" in the blocked signal set is whether the signal is blocked, and "valid" and "invalid" in the pending signal set. The meaning is whether the signal is pending.

The sigsett type uses a bit for each signal to indicate the "valid" or "invalid" state, and how to store these bits inside this type depends on the operating system implementation.

Note that blocking and ignoring are different, as long as the signal is blocked, it will not be delivered, and ignoring is an optional processing action after delivery

One more thing to note is: One more thing to note is: Also there is a point needs to stay intended to be : a main flow and a control signal in response to different functions using stack space, and calling the called relationship does not exist between them, two independent control flow


related functions:

  1. signal function
#include <signal.h>
void (*signal(int signum, void (*handler)(int)))(int); 

typedef void (*sighandler_t)(int); 
sighandler_t signal(int signum, sighandler_t handler);
  1. kill function
#include <sys/types.h> 
#include <signal.h>
int kill(pid_t pid, int sig); 

Used to send any signal to any process or process group
pid> 0: send the signal sig to the process whose process number is pid
pid = 0: the signal sig will be sent to all processes in the process group of the current process
pid = -1: signal sig will be sent to all processes except process 1 and itself
pid <-1: the signal sig will be sent to all processes belonging to the process group -pid

If the parameter sig is 0, no signal will be sent

  1. pause function
#include <unistd.h>
int pause(void); 

The pause function puts the process to sleep until it receives a signal.
The call always returns -1 and sets the error code to EINTR (a signal is received)

If the signal processing action is to terminate the process, the process is terminated, and the pause function has no chance to return; if the signal processing action is ignored, the process continues to be in a suspended state, and pause does not return; if the signal processing action is to capture, it is called After the signal processing function, pause returns -1 and errno is set to EINTR, so pause has only an error return value. Error code EINTR means "interrupted by signal"

  1. alarm function
#include <unistd.h>
unsigned int alarm(unsigned int seconds); 

The function is to set a timer, when the timer expires, it will send a signal to the process

alarm arranges the kernel to send a SIGALRM signal to the calling process after the specified seconds

A setting after the alarm will cancel the previous setting

Note: alarm is only set to send a signal once, if you want to send multiple times, you must use the alarm call multiple times

  1. setitimer/getitimer function
#include <sys/time.h> 
int getitimer(int which, struct itimerval *value); 
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue); 

Setitimer is used to set the timer, getitimer is used to obtain the timer status.
The system call provides three timers to the process, each of which has its own unique timing domain. When any one of them arrives, it sends a corresponding signal to the process. , And make the timer restart.
Three timers are specified by the parameter which:

TIMER_REAL: actual time, time to reach the SIGALRM signal is sent
TIMER_VIRTUAL: when process execution time, time to reach the sending SIGVTALRM
TIMER_PROF:

  1. raise function
  #include <signal.h>
  int raise( int signal );

The raise function is used to send a signal to yourself

Unfinished. . .

reference:

  1. "Linux Environment Programming From Application to Kernel" Peak, Li Bin
  2. man signal
  3. "UNIX Network Programming Volume 2: Interprocess Communication"
  4. Linux signal-signal reliability and classification
  5. "Advanced Programming in UNIX Environment"
  6. linux signal collection

Guess you like

Origin blog.csdn.net/weixin_41629848/article/details/98867267