Signals in Swift
Here’s an idiomatic Swift example demonstrating the concept of handling signals:
This Swift program demonstrates how to handle Unix signals, specifically SIGINT (interrupt signal, usually triggered by Ctrl+C) and SIGTERM (termination signal).
Let’s break down the code:
We import the
Foundation
framework, which provides the necessary functionality for signal handling in Swift.We define a
signalHandler
function that takes anInt32
parameter representing the signal. This function prints the received signal and then exits the program.We use the
signal()
function to register oursignalHandler
for both SIGINT and SIGTERM. This tells the system to call our handler function when these signals are received.We print a message indicating that the program is waiting for a signal.
Finally, we use
RunLoop.main.run()
to keep the program running indefinitely, waiting for signals.
To run this program:
- Save the code in a file named
SignalHandling.swift
. - Open a terminal and navigate to the directory containing the file.
- Compile and run the code using the Swift compiler:
The program will continue running until it receives a signal. You can test it by pressing Ctrl+C in the terminal, which will send a SIGINT:
This example demonstrates how to handle signals in Swift, which is useful for creating more robust command-line tools or server applications that need to respond to system events or gracefully shut down when requested.
Note that Swift’s approach to signal handling is somewhat simpler than Go’s channel-based approach. Swift uses a more traditional callback-style API, which is common in many systems programming contexts.