In C programming, a signal is an asynchronous notification sent to a process to notify it that a specific event has occurred. Think of it as a "software interrupt."
In the context of this Flappy Bird lab, signals play a crucial role in managing the game's timing and logic.
1. Why are we using signals here?
In your code, you see #include <signal.h> and a function declaration void drop(int sig);.
In a terminal game like Flappy Bird, things need to happen automatically over time (like the bird falling or pipes moving) even if you don't press a key. Signals allow us to:
- Create a Timer: We use a system timer to send a "pumping" signal to the program at regular intervals (e.g., every 100 milliseconds).
- Trigger Events: Every time the program receives this signal, it pauses what it's doing, runs the
dropfunction (to update the bird's position), and then goes back to what it was doing.
2. Common Roles of Signals
Beyond this lab, signals are used for:
- Error Handling: If your program tries to divide by zero, the system sends a
SIGFPEsignal. If it tries to access invalid memory, it sends aSIGSEGV(Segmentation Fault). - External Interrupts: When you press
Ctrl+Cin the terminal, you are actually sending aSIGINTsignal to tell the program to terminate. - Inter-process Communication: One program can send a signal to another to tell it to reload configuration or shut down gracefully.
3. How it looks in your Lab code:
In the next steps of this lab, you will use:
signal(SIGALRM, drop): This tells the computer: "Hey, whenever the alarm clock goes off, run thedropfunction."set_ticker(): This sets how often that "alarm clock" (signal) should ring.
By using signals, your game gains a "heartbeat" that keeps the action moving! Do you have any other questions about the functions we've declared?