Xathrya Sabertooth |
POSIX/ANSI Signal and Signal Handling in C++ Posted: 20 Sep 2013 04:51 AM PDT In Linux and UNIX world, signals is defined as notifications to a process that an event has occurred. In details, a signal is a software interrupt which delivered by process or kernel. Signal occur asynchronously, which means the process doesn’t know ahead of time when a signal will occur. Signal can be sent in following scenario:
When an application receive signal, it postpone the current activity and work other thing based on interrupt (signal) it receive. Any application has default behavior for some signal, i.e. a process terminated when it receives an interrupt SIGNINT signal by pressing keystroke Ctrl-C. A programmer can modify the behavior by his own code when application receive specific signal, which will be discussed in this article. This article is tested using following tools & environment:
Note that not all signals can be handled. The TheoryTo handle a signal, one should define a callback function to manage the signal. This callback will be registered so it will change the default behavior of signal handler. There are several types available which can be read on appendix A section. Sending a signal from terminal can be down using a “kill” command. Despite of the name, kill is not only for killing an application (thought it is when you don’t specify any signal number). To send a signal, the following is the general syntax: kill -s <signal-number> <pid> With <signal-number> is the signal number as specified on table appendix A and pid is the process id we want to send to. Handling a Signal using Signal FunctionThe easiest way to handling a signal is using signal function to register signal callback. #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> // Define the function to be called when Ctrl-C (SIGINT) signal is sent to process void callback_sigint(int signum) { // prompt that we got SIGINT printf("We got signal %d\n", signum); // Terminate program exit(signum); } int main() { // Register signal and signal handler signal(SIGINT, callback_sigint); while (true) { printf("Program is processing stuff here.\n"); sleep(1); } return 0; } Compile and followed by run: gcc SignalExample.cpp -o SignalExample ./SignalExample To terminate, press ctrl-c. As you may suspect, the callback function is callback_sigint(). The callback function always has prototype as: void signal_name (int signum); The signum is a variable which hold the signal number we received. Thus, we can use a single signal handle to handle various signal sent. However this is beyond our league, but you should be able to figure it out. If you are curious, the function signal has prototype: void (*signal (int sig, void (*func)(int)))(int); Handling a Signal using Registration and Handling ClassNow let’s see how can be implement the signal handling using Object Oriented Programming. Here is our header file: #ifndef __SIGNALHANDLER_HPP__ #define __SIGNALHANDLER_HPP__ #include <stdexcept> using std::runtime_error class SignalException : public runtime_error { public: SignalException(const std::string& _message) : std::runtime_error(_message) {} }; class SignalHandler { protected: static bool mbGotExitSignal; public: SignalHandler(); ~SignalHandler(); static bool gotExitSignal(); static void setExitSignal(bool _bExitSignal); void setupSignalHandlers(); static void exitSignalHandler(int _ignored); };
Langganan:
Posting Komentar (Atom)
|
Tidak ada komentar:
Posting Komentar