![]() ![]() So rather than calling printf(), which is not async-safe, we can use write(), which is safe to call from a signal handler. This can be achieved using sigprocmask(). One way of making a function async-safe is for it to stop signals from arriving while it is active, and enable them only after it has left the critical code section. These are either listed in the documentation as being safe by POSIX definition or the functions are described as being async-safe on their man pages. There are some functions that are async-safe. If printf() in the main thread has acquired a mutex M in order to print something, and then the printf() in the signal handler attempts to acquire the same mutex M, the thread will deadlock-it's waiting for a mutex it has already acquired. ![]() This characteristic is referred to as whether a function is "async-safe." Suppose the main thread is calling printf() when it is interrupted by the signal. Listing 6: Tail end of output from non-async-safe example The sa_mask field indicates any signals that should be blocked while the signal handler is being executed. To pass information about the handler to sigaction(), we need to use a sigaction structure, which takes the address of the routine that will handle the signal, as well as certain other settings. Information about the old signal handler can be useful when you need to preserve the existing behavior. This identifies the signal that we want to handle and the action that we want to take when the signal is received, and it takes an optional parameter that will receive information about any previous signal handler that was installed for that signal. The next step, which is shown in Listing 2, is to install the signal handler using the sigaction() system call. ![]() Void handle_prof_signal(int signal, siginfo_t * info, void * context) Listing 1 shows an empty handler that receives information about the signal number, information about the signal delivery, and information about the thread context. The parameters are necessary if the objective is to extract information from the current state of the application. There are two kinds of signal handling routines: one kind that does not take any parameters and another kind that takes a set of three parameters. The first step is to declare a signal handling routine. Signals can be used to handle events such as timers expiring, and they can even handle hardware problems such as misaligned memory accesses or illegal/unsupported instructions. ![]() In many cases, the default handler causes the process to exit. Once a process receives a signal, it either handles it with a custom handler or leaves it for the default handler. They can come from within the process, or they can be sent externally using the kill command or some other mechanism. Signals are used to interrupt a running process and get it to do something else. SPDX-License-Identifier: GPL-2.0 /* * linux/kernel/itimer.This article demonstrates how to use signal handlers for various scenarios, such as profiling an application and handling illegal instructions in a UNIX environment. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |