[Linux] Process signal (medium)

In the previous article, there was nothing to add about the generation of the signal, so I will add it in this article

1. Signal Generation

hardware exception signal

a/=0 problem

Create mysignal.cc file

#include<iostream>
using namespace std;
int main()
{
    
    
   int a=10;
   a/=0;
   
   cout<<"div zero"<<endl;

    return 0;
}

insert image description here
When using make to generate an executable program, a/=0 will alarm


insert image description here
The executable program mysignal can still be generated, but an error will be reported when running the executable program


Why is it reporting an error when dividing by 0?
When the code is divided by 0, the program crashes after running, and the program runs into a process. When the process runs the code, an illegal code appears, and the process exits.


insert image description here
Load the instruction data in the memory to the CPU.
There is a bit in the status register to indicate the current calculation status.
Some registers in the CPU store the future calculation results. The status register is used to indicate the correctness or error of the calculation result.
There is a bit in the status register. The bit is 0/1, indicating whether there is an overflow problem in this calculation.
Assuming that there are 32/64 bits, when dividing by 0, there will be a higher carry, and the computer will recognize that there is an overflow. If it overflows, the overflow flag bit of the status register will be set to 1


The operating system finds that the flag bit of the status register is 1, that is, it recognizes a hardware exception, and immediately sends a signal to the target process. The
signal is: Floating point exception


insert image description here
The essence of dividing 0 by the number 8 signal at the end of FPE is to trigger a hardware (CPU) exception

Verified as signal number 8


Make the process not exit by setting,
change the default method of the 8 process into a custom method


Running the executable program again will keep printing in a loop, and the process can only be terminated by other signals


Why does it keep printing in a loop?
The operating system finds that the overflow flag is set to 1, and the hardware is abnormal, and it sends signal No. 8 to the process. However, because the No. 8 signal implements a custom method, the process does not exit, and the overflow flag belongs to the context of the process and exists as 1 all the time. , the operating system will always detect that the flag bit is 1, so it will always send the No. 8 signal to the process

wild pointer problem

p is used as a pointer variable, with 4/8 bytes of space
1 is to write 100 as address data into the p variable
2 is to use p as nullptr, *p takes the address of number 0 in the memory
*p=100, which is equivalent to writing to Write 100 at address 0, but address 0 has not been applied for, so the wild pointer problem is caused


insert image description here
After running the executable program, a segmentation fault occurs


Why does the out-of-bounds crash the program?
In fact, the address presented in the language is a virtual address


insert image description here
The virtual address is mapped to the physical memory through the page table. The page table
queries the kv relationship. The action of looking up the table is completed by the MMU hardware (memory management unit).
The input data is imported into the MMU, and then transferred out through the MMU.
Therefore, from the virtual address to the physical memory Address, using a combination of software and hardware


*p=100, not for writing, but for virtual-to-physical conversion
. If there is no mapping relationship, the MMU hardware will report an error.
If there is a mapping relationship, but there is no permission, the MMU will report
an error directly, which will cause the operating system Recognized, the operating system will find the PCB in the corresponding target process, send the corresponding signal, and terminate the process


insert image description here
Segmentation fault corresponds to signal No. 11

Verified as signal number 11

After the executable program is executed, it will continue to print in a wireless cycle.
Since the MMU hardware error has not been repaired, it has always existed, so every time a process is scheduled, the operating system will recognize the exception and send a signal No. 11 to the process, resulting in
continuous wireless cycle printing.


core dump

Among many signals, there are Core and Term types, both of which can terminate the process.
What is the difference between the two?
let me take my time


Linux provides a capability at the system level. When a process is abnormal,
the operating system can perform a core dump of the core code part of the process when the process is abnormal
(dump all the relevant data of the process in the memory to the disk. )
will generally form a binary file of core.pid in the running directory of the current process, such as core.pid is called a core dump file

The core dump file cannot be seen on the cloud server, because this function is turned off by default on the cloud server


Enter ulimit -athe command
to view the upper limit corresponding to a specific resource in the current system

core file size represents the core dump, the default size is 0, the current system is not allowed to form a core file in the current directory

Set core dump size

Pass ulimit -c +大小, such as the core file size becomes 10240

The difference between Core and Term

Create Terminal 2 by duplicating the SSH channel


insert image description here
Signal No. 2 corresponds to Term and terminates the process


insert image description here
Run the executable program in terminal 1, and send signal 2 in terminal 2 to kill the process


When the process is killed, no file ending with pid is found
indicating that the Term type signal is used. After the process is killed, no core dump occurs.


insert image description here
No. 8 signal Core, floating point exception

insert image description here
Run the executable program in terminal 1, send signal No. 8 in terminal 2 to kill the process, and a core dump appears


insert image description here
Use ls -lthe command and find that there is an extra core.2257 file, which is the core dump file


Term: Termination is termination, there is no extra action
Core: Termination, the core dump will be performed first, and the process will be terminated

The role of core dumps

After the exception is convenient, debug

insert image description here
In order to change the code from release to debug, so add it to the makefile -g
If you don’t understand, please see: The use of gdb debugger


insert image description here
Enter the gdbexecutable program to enter the gdb debugger
and enter again core-file +corethe file
gdb directly locates the current process to terminate because of signal No. 8, and the more detailed description of the signal is Arithmetic exception


The role of the core file:
you don’t need to locate it yourself, there is gdb automatic positioning, and you can debug afterwards


Why are core dumps usually turned off?
The cloud server belongs to the production environment, that is, the real service after the test


insert image description here
The size of the core.6288 file is 232 bytes, and the core dump file is often larger. A certain
service deployed online may hang up, and the core dump file will continue to be formed if the hangup restarts continuously, which may cause the host hang up

2. Signal preservation

1. Concept

1. The actual execution of the signal processing action is called signal delivery
2. The state between signal generation and delivery is called signal pending
3. A process can choose to block a signal


Suppose you don't like a teacher very much, so when a teacher leaves homework, you just record what the homework is, because you are currently in class and don't have time to do homework, only when you find time in the afternoon to do homework, the teacher assigns
homework Behavior is the process of signaling by the operating system. As a process, you are currently in class because you are doing something with higher priority, so you have no time to process the signal. You can only write down your homework. When you have time to do your homework, that is, the signal is delivered.


4. When the blocked signal is generated, it will remain in a pending state until the process unblocks the signal to perform the delivery action


Suppose there is teacher ABC, and everyone left homework, because teacher AB is very kind to you, so you plan to write the homework left by teacher AB first, but you don’t like teacher C very much, so you don’t want teacher C’s homework, you would rather write Game, so you left the homework left by teacher C unresolved.
Since you can’t hand it in, you blocked teacher C’s homework.
Suddenly one day, you like teacher C, so you want to write the homework left by teacher C. The job, that is, unblock, and deliver the job after writing


5. Blocking and ignoring are different. As long as the signal is blocked, it will not be delivered, and ignoring is an optional processing work after delivery

There is also a teacher, after assigning homework, you recorded it, but this teacher usually does not check the homework, so just cross out the homework, and the default is finished, that is, ignore the signal


Ignore is to cross out the homework. By default, the delivery action is completed after writing.
Blocking is to record the homework and do not want to write the homework. That is, there is no delivery action if it is not completed.

2. Signal list

insert image description here
Pending table:
The position of the bit in the bitmap structure indicates which signal
the content of the bit indicates whether the signal has been received.
For example: 00000000...0001000 means that the No. 4 signal is received


bolck table: the position of the bitmap structure
bit indicates which signal,
and the content of the bit indicates whether the corresponding signal is blocked.
For example: 0000000...0010 means that signal No. 2 is blocked


handler table: array of function pointers

insert image description here
The return value is void, and the parameter is a function pointer of int.
The subscript of the array indicates the signal number.
The content of the specific subscript of the array indicates the delivery action of the signal.

3. Signal processing actions

In addition to custom capture, there are SIG_DFL (default action) and SIG_IGN (ignore signal)



insert image description here
Forcibly convert 0 into a function pointer type, that is, terminate the process by default, and
perform SIG_DFL on signal 2, that is, the default processing

insert image description here

After running the executable program, use signal 2 to terminate the process



insert image description here
Forcibly convert 1 into a function pointer type to ignore the signal
and ignore the signal No. 2


insert image description here
Signals are ignored, so actions on them are useless

4.sigset_t

siget_t is used to control the block and pending bitmaps.
The control block table is called the signal mask word, and the control pending table is called the pending signal set.


sigset_t is a bitmap structure provided by the operating system

5. Signal set operation function

Operate on signal sets

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

The function sigemptyset initializes the signal set pointed to by set,
and clears the corresponding bits of all signals in it, indicating that the signal set does not contain any valid signals


The function sigfillset initializes the signal set pointed to by set,
so that the corresponding bits of all signals in it are set, indicating that the effective signals of this signal set include all signals supported by the system


Note that before using the variable of type sigset_t, be sure to call sigemptyset or sigfillset for initialization, so that the signal set is in a definite state. After initializing the sigset_t variable, you can call sigaddset and sigdelset to add or delete a valid signal in the signal set

sigprocmask

Read/change the signal mask word of the process, that is, the bitmap of the block can be changed

int sigprocmask(int how, const sigset_t *set, sigset_t *oset);
if the operation is successful, return 0, otherwise return -1
how and set are input parameters, and oset is an output parameter


oset: When resetting the signal mask word, you must make various modifications to the old block. Before the modification, return the old block through oset, and then you can set it


How means how to change it.
There are three options
SIG_BLOCK: add all the signals specified by the second parameter set to the kernel block table
SIG_UNBLOCK: remove the specified signals from the kernel block table (for signal shielding)
SIG_SETMASK : Set the current signal mask word to the value specified by set, which is equivalent to setting whatever is passed

The bolck bitmap is used to judge whether the signal is blocked.
The old signal mask word is the default action to terminate the process, so the block bitmap is all zero


Entering ctrl c does not respond, because the signal in the set set is shielded by using sigprocmask,
and the set signal set includes signal No. 2

sigpending

enterman sigpending

insert image description here
Use this system call to get the pending bitmap of the calling process.
The call returns 0 successfully, and returns -1 when an error occurs.



The pending bitmap indicates whether the signal is received.
After running the executable program, the pending table is all 0 because there is no signal at the beginning.
When using the signal No. 2 to kill the process, the process cannot be terminated and pending because the signal No. 2 is blocked. The bit of the corresponding No. 2 signal in the table appears 1


If the shielding of the No. 2 signal is released, the No. 2 signal will be input, and the delivery action will be entered immediately


At the beginning of executing the executable program, since there is no signal input, the pending table is all 0,
print it first, and the shielding of the signal is not released until the cycle is repeated 10 times, and finally the printed sentence is displayed, and the default action of the No. 2 process is executed. terminate process

Guess you like

Origin blog.csdn.net/qq_62939852/article/details/130518733