A collection of C programs demonstrating Linux Signals. This project shows how to catch SIGINT (Ctrl+C), ignore signals, and use signals for basic Inter-Process Communication (IPC).
Inspired by the 'Core Dumped' YouTube channel: Signals: Make Ctrl+C Do Anything You Want
This project includes a Makefile to simplify compilation. You will need gcc (or clang) and make installed.
-
Clone the repository (or download and extract the files).
-
Create the
builddirectory:mkdir build
-
Compile all programs:
make all
-
Run a specific example (see below).
-
To clean up compiled files:
make clean
- What it does: Demonstrates how to "catch" the
SIGINTsignal (sent byCtrl+C). - How it works: Instead of quitting, the program catches the signal and prints a message ("Ouch! Don't do that!"). It keeps a counter and will only exit after you press
Ctrl+C5 times. - To Run:
./build/01-catch-sigint
- What it does: Shows how to completely ignore a signal.
- How it works: This program tells the OS to ignore
SIGINT. When you pressCtrl+C, nothing happens. It prints its own PID so you can stop it. - To Run:
./build/02-ignore-signal
- To Stop: You must open a second terminal and use the
kill -9command with the PID it printed.kill -9 <PID_from_program>
- What it does: A two-part demo showing how signals are used for Inter-Process Communication (IPC).
- How it works:
- Run the
receiverin one terminal. It will print its PID and wait. - Run the
senderin a second terminal, giving it the receiver's PID as an argument. - The
senderuses thekill()system call to send aSIGUSR1signal. - The
receivercatches this signal and prints ">>> Signal Received! <<<".
- Run the
- To Run (Terminal 1):
./build/receiver # It will print: Receiver PID is: 12345 - To Run (Terminal 2):
# Use the PID from Terminal 1 ./build/sender 12345
--
--