I want to write a few extra lines to a file when interrupted with ctrl-c before the program dies. However the location of the file is not hard coded so I need something more tha
One common scheme for loop-based scientific code is to have a global volatile sig_atomic_t boolean indicating whether a signal was caught and add it to the loop condition.
e.g.
volatile sig_atomic_t interrupted=false;
...
void signal_handler(int s)
{
// ...
interrupted=true;
}
...
while (!converged && !interrupted)
{
// Perform computations
}
// Save file properly