Signals in Haskell
Here’s the translation of the Go code to Haskell, with explanations in Markdown format suitable for Hugo:
Our first example demonstrates how to handle Unix signals in Haskell. We’ll create a program that gracefully handles SIGINT and SIGTERM signals.
In this Haskell version, we use the System.Posix.Signals
module to handle Unix signals. Here’s a breakdown of the code:
We import necessary modules for signal handling, concurrency, and inter-thread communication.
In the
main
function, we create anMVar
calleddone
. This will act as a channel for signaling between threads.We set up signal handlers for SIGINT and SIGTERM using
installHandler
. Each handler calls thehandleSignal
function with the appropriate signal name and thedone
MVar.We fork a new thread that simulates some work by sleeping for 100 seconds. This is analogous to the main work of a server or long-running process.
The main thread then waits on the
done
MVar usingtakeMVar
. This blocks until a signal is received and handled.The
handleSignal
function prints the name of the received signal and puts a value into thedone
MVar, which unblocks the main thread.
When we run this program, it will block waiting for a signal. By typing Ctrl-C
, we can send a SIGINT signal, causing the program to print the signal name and then exit.
This Haskell implementation uses MVars for inter-thread communication, which is conceptually similar to channels in other languages. The forkIO
function is used to create a new thread, analogous to goroutines in Go.
While the structure is different from the original Go code, this Haskell version achieves the same functionality of gracefully handling Unix signals.