When a process captures and processes the signal, the command sequence being executed by the process is temporarily interrupted by the signal processing program. It first executes the commands in the signal processing program. If it is returned from the signal processing program (for example, the signal processing program does not call exit or longjmp), it continues to execute the normal command sequence that the process is executing when the signal is captured. However, in the signal processing program, the process cannot determine where to execute when the signal is captured.If the program is executing malloc, allocate another storage space in its heap, and insert and execute the signal processing program due to capturing the signal, and then call malloc, this may cause damage to the process, because malloc usually maintains a chain table for the storage zone it is allocated, and the process may be changing the chain table when inserting the signal processing program. For example, if a process is executing a function, such as getpwnam, that stores its results in a static storage unit, and inserts an execution signal processing program, it calls such a function, the information returned to the normal caller may be overwritten by the information returned to the signal processing program.
Most functions not listed in table 10-3 cannot be reentrant because: (a) they are known to use static data structures, (B) they call malloc or free, or (c) they are standard I/O functions.
It should be noted that even if the signal processing program calls functions listed in table 10-3, since each thread only has one errno variable, the signal processing program may modify its original value. Therefore,As a general rule, when calling the functions listed in table 10-3 in the signal processing program, it should be saved before it and restored after it.
If a non-reentrant function is called in the signal processing program, the result is unpredictable.
This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.