fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.05k stars 164 forks source link

Signal handling #314

Open ivan-pi opened 3 years ago

ivan-pi commented 3 years ago

I found a document which describes signal handling in Fortran: https://www.sharcnet.ca/help/images/4/42/Fortran_Signal_Handling.pdf

Some compilers already provide extensions, in gfortran the subroutine signal is available.

Originally posted by @ivan-pi in https://github.com/fortran-lang/stdlib/issues/308#issuecomment-776620922

ivan-pi commented 3 years ago

Reply from @awvwgk: https://github.com/fortran-lang/stdlib/issues/308#issuecomment-776624029

From my experience the signal extension in gfortran and ifort is not really satisfactory, therefore I usually resort to install signal handlers via C. I use it to gracefully catch SIGINT and SIGTERM and shut down the program, depending on your compiler those are most likely completely ignored by default which makes it a pain to kill a Fortran program.

ivan-pi commented 3 years ago

The tutorial by @interkosmos contains a small section on signals also demonstrating the approach using C: https://cyber.dabamos.de/programming/modernfortran/inter-process-communication.html#signals

Perhaps a derived type similar to the boost::asio::signal_set would be suitable for Fortran. This should also work under Windows.