Skip to content

A structured framework for handling Unix signals with customizable behaviors and dynamic dispatching capabilities.

Notifications You must be signed in to change notification settings

ajitm722/Unix-Signal-Handling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Signal Handler Project - README

Overview

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.

Features

  • Custom Signal Handlers: Handlers for six common signals: SIGINT, SIGTERM, SIGHUP, SIGUSR1, SIGUSR2, and SIGPIPE.
  • 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.

Signals Handled

The project handles the following Unix signals, each with a specific purpose and customizable behavior:

  1. SIGINT (Interrupt Signal):

    • Triggered by pressing Ctrl+C in the terminal.
    • Current Behavior: Exits the program gracefully.
    • Use Case: Perform cleanup tasks like saving data or releasing resources before exiting.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

How to Build

Prerequisites

  • CMake: Version 3.10 or higher.
  • Compiler: A C++ compiler supporting C++17 (e.g., g++ or clang).

Steps to Build

  1. Clone the repository:

    git clone <repository_url>
    cd <repository_folder>
  2. Create a build directory:

    mkdir build
    cd build
  3. Configure the project using CMake:

    cmake ..
  4. Build the project:

    make
  5. The executable, SignalHandlerProject, will be created in the build/ directory.


How to Test

Running the Program

  1. Start the program:

    ./SignalHandlerProject
  2. While the program is running, send signals to it using the kill command:

    • For SIGINT: Press Ctrl+C in the terminal.

    • For other signals, use the process ID (PID) of the running program. Example:

      kill -SIGTERM <pid>
      kill -SIGUSR1 <pid>
  3. Observe the output for each signal handled.

Example -

Output Screenshot

Customization

To handle additional signals or modify the behavior of existing ones:

  1. Add a New Handler:

    • Create a new .hpp and .cpp file for the signal handler.
    • Implement the SignalHandler interface.
  2. Register the New Handler:

    • Add the new handler to SignalDispatcher in SignalDispatcher.cpp.
  3. Rebuild the Project:

    • Follow the build instructions to apply changes.

About

A structured framework for handling Unix signals with customizable behaviors and dynamic dispatching capabilities.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published