This recipe will teach you how to catch (or trap) a signal in a program. There might be a need to perform some actions for a specific signal. An example of this is when an application receives the signal to terminate (SIGTERM) but we are required to clean up some used resources before quitting.Â
Learning how to trap a signal
How to do it...
Let's write an application where we'll catch the SIGTERM signal, print a string, and terminate the application:
- On a shell, create a new file called signal_trap.cpp. We need to include, among other headers, <signal.h> to be able to handle signals. We also have to add the prototype needed to manage the signal we want to trap. In the main method then, we call the...