8000 looperdooper/examples at main · danish-mehmood/looperdooper · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History

examples

README.md

Event Loop Signal Handling Examples

This directory contains examples demonstrating different aspects of signal handling in the event loop library.

Basic Signal Example (signal_example.c)

Demonstrates basic signal handling with SIGINT, SIGTERM, and SIGUSR1 (on Unix systems).

gcc -I../include -o signal_example signal_example.c ../src/*.c
./signal_example

Multiple Signals Example (signal_multiple.c)

Shows how to handle multiple signals with custom data and counting.

  • Handles SIGUSR1 and SIGUSR2 with a shared counter
  • Demonstrates graceful termination with SIGTERM
gcc -I../include -o signal_multiple signal_multiple.c ../src/*.c
./signal_multiple

Graceful Shutdown Example (signal_shutdown.c)

Implements a graceful shutdown pattern:

  • First Ctrl+C initiates graceful shutdown
  • Second Ctrl+C forces immediate shutdown
  • Demonstrates proper cleanup of resources
gcc -I../include -o signal_shutdown signal_shutdown.c ../src/*.c
./signal_shutdown

Platform-Specific Notes

Linux

  • Uses signalfd() for efficient signal handling
  • All POSIX signals are supported
  • Signals are queued and delivered reliably

BSD/MacOS

  • Uses kqueue with EVFILT_SIGNAL
  • All POSIX signals are supported
  • Signals are integrated with the kqueue event mechanism

Windows

  • Limited to console control signals:
    • SIGINT (Ctrl+C)
    • SIGBREAK (Ctrl+Break)
    • SIGTERM (Console close)
  • Uses SetConsoleCtrlHandler() internally

Best Practices

  1. Keep signal handlers minimal and fast
  2. Use the event loop to defer complex processing
  3. Always check return values from signal-related functions
  4. Clean up signal handlers before destroying the event loop
  5. Avoid installing the same signal handler multiple times
0