This project is a Signal Handler Framework for Unix-like systems, designed to handle various POSIX signals gracefully. It features a modular architecture, making it easy to extend and modify signal handling behavior.
- Custom Signal Handlers: Handlers for six common signals:
SIGINT,SIGTERM,SIGHUP,SIGUSR1,SIGUSR2, andSIGPIPE. - Modular Design: Separate implementation for each signal, ensuring clear separation of concerns.
- Customizable: Easily add or modify handlers for different signals to suit various application needs.
- Efficient Signal Dispatching: Signals are handled immediately upon receival using the
SignalDispatcher.
The project handles the following Unix signals, each with a specific purpose and customizable behavior:
-
SIGINT(Interrupt Signal):- Triggered by pressing
Ctrl+Cin the terminal. - Current Behavior: Exits the program gracefully.
- Use Case: Perform cleanup tasks like saving data or releasing resources before exiting.
- Triggered by pressing
-
SIGTERM(Termination Signal):- Sent to request program termination.
- Current Behavior: Outputs a termination message and exits.
- Use Case: Handle cleanup tasks on shutdown in server applications.
-
SIGHUP(Hangup Signal):- Sent when a terminal disconnects or a configuration reload is needed.
- Current Behavior: Reloads configuration (simulated by a message).
- Use Case: Reinitialize application state or reload configurations without restarting.
-
SIGUSR1(User-Defined Signal 1):- A custom signal available for application-defined behavior.
- Current Behavior: Prints a message indicating a custom action.
- Use Case: Trigger user-specific actions like toggling logging or enabling debug mode.
-
SIGUSR2(User-Defined Signal 2):- Another custom signal for application-defined behavior.
- Current Behavior: Prints a message for another custom action.
- Use Case: Manage alternative actions or workflows triggered externally.
-
SIGPIPE(Broken Pipe Signal):- Sent when writing to a closed pipe or socket.
- Current Behavior: Outputs a message and continues execution.
- Use Case: Ignore this signal or log the event in inter-process communication.
- CMake: Version 3.10 or higher.
- Compiler: A C++ compiler supporting C++17 (e.g.,
g++orclang).
-
Clone the repository:
git clone <repository_url> cd <repository_folder>
-
Create a build directory:
mkdir build cd build -
Configure the project using CMake:
cmake ..
-
Build the project:
make
-
The executable,
SignalHandlerProject, will be created in thebuild/directory.
-
Start the program:
./SignalHandlerProject
-
While the program is running, send signals to it using the
killcommand:-
For
SIGINT: PressCtrl+Cin the terminal. -
For other signals, use the process ID (PID) of the running program. Example:
kill -SIGTERM <pid> kill -SIGUSR1 <pid>
-
-
Observe the output for each signal handled.
To handle additional signals or modify the behavior of existing ones:
-
Add a New Handler:
- Create a new
.hppand.cppfile for the signal handler. - Implement the
SignalHandlerinterface.
- Create a new
-
Register the New Handler:
- Add the new handler to
SignalDispatcherinSignalDispatcher.cpp.
- Add the new handler to
-
Rebuild the Project:
- Follow the build instructions to apply changes.
