Signals in Fortran
Here’s the translation of the Go code to Fortran, with explanations in Markdown format suitable for Hugo:
Our program demonstrates how to handle signals in Fortran. It will gracefully shut down when it receives a termination signal.
In Fortran, we use the signal
function from the C library to set up signal handlers. We define two handler subroutines: one for the interrupt signal (SIGINT, usually triggered by Ctrl+C) and one for the termination signal (SIGTERM).
The program sets up these handlers and then enters an infinite loop, waiting for a signal. When a signal is received, the appropriate handler is called, which prints a message and exits the program.
To compile and run this program:
In this example, we use Ctrl+C to send an interrupt signal, causing the program to print “Interrupt signal received” and then exit.
Note that Fortran doesn’t have built-in support for channels or goroutines like Go does. Instead, we use a more traditional approach with signal handlers. The concept of graceful shutdown remains the same, but the implementation details differ due to the language’s features and idiomatic practices.