What is the difference between sigset and signal?

When reading "In-depth understanding of the UNIX system kernel", I learned about the use of signals. Recalling that I used signal() to process signals before, but I had a wrong understanding when I practiced at that time: the signal processing only captures the signal once, and the subsequent signals are not captured, because my signal processing function is not called. Now it seems that the misunderstanding is not because it is not caught, but it is caught, but it is handled by the default signal processing function.

The signaling mechanism in the unix version of SVR2 is unreliable and has many flaws, the most important problem being the reliable delivery of signals. The signal processing function is not persistent and effective, and the corresponding signal will not be shielded during execution (that is, when the signal processing function is executed, it is possible to receive a new signal. The method of not shielding is: it will not block the new signal, and will pass it directly. For this process, the method of shielding is: if the new signal is set to block, the kernel will remember it, but it will not be passed to the process immediately. When the process unblocks the new signal, the signal will be passed by the kernel. given to the process, and handled in the process, which allows the programmer to protect the critical section of the code from corruption from some signals). Under this signal mechanism, there is a signal() function. There is this passage in the book:

"Suppose the user sets a signal handler function for a special signal. When the signal occurs, the kernel will set the signal handling action as the default action before calling the handler function. If the user wants to capture the re-send of the signal, every This handler must be reset every time."

That is, the correct way to use signal() is this:

void sigint_handler (sig)

int sig;

{

       signal(SIGINT,sigint_handler);

        ……

}

main()

{

        signal(SIGINT,sigint_handler);

}

The sigset system call specifies a persistent signal processing function, which will not be reset to the default processing action when the signal occurs. The old signal call is preserved for backward compatibility, the handler function specified by signal is not persistent.

But I tried to capture the SIGINT signal with signal on my own machine. Even if I don't set the signal handler function again in sigint_handler(), the next time the signal arrives, my signal handler function will be called instead of the signal handler function. Set the signal handler function again in . Maybe the current version is SVR4 or BSD, and signal is optimized for persistence. But write down this article and remind yourself to use sigset as much as possible and use signal less in the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449896&siteId=291194637