Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Safely" terminate a running C++ program on keypress?

Tags:

c++

exit

keyboard

I'm trying to write a simulation that will carry on running until I press a certain key (like 'q' for quit). Then after I press that, I want the program to finish writing the data its currently writing, close files, then gracefully exit (as opposed to just pressing ctrl+c to force the program to stop). Is there any way to do this on C++?

Thanks

like image 662
Eddy Avatar asked Mar 19 '12 19:03

Eddy


1 Answers

Have the user press CTRL-C, but install a signal handler to deal with it. In the signal handler, set a global boolean variable, for example user_wants_to_quit. Then your sim loop can look like:

while ( work_to_be_done && !user_wants_to_quit) {
 …
}
// Loop exited, clean up my data

A complete POSIX program (sorry, if you were hoping for Microsoft Windows), including setting and restoring the SIGINT (CTRL-C) handler:

#include <iostream>
#include <signal.h>

namespace {
  sig_atomic_t user_wants_to_quit = 0;

  void signal_handler(int) {
    user_wants_to_quit = 1;
  }
}

int main () {

  // Install signal handler
  struct sigaction act;
  struct sigaction oldact;
  act.sa_handler = signal_handler;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;
  sigaction(SIGINT, &act, &oldact);


  // Run the sim loop
  int sim_loop_counter = 3;
  while( (sim_loop_counter--) && !user_wants_to_quit) {
    std::cout << "Running sim step " << sim_loop_counter << std::endl;

    // Sim logic goes here. I'll substitute a sleep() for the actual
    // sim logic
    sleep(1);

    std::cout << "Step #" << sim_loop_counter << " is now complete." << std::endl;
  }

  // Restore old signal handler [optional]
  sigaction(SIGINT, &oldact, 0);

  if( user_wants_to_quit ) {
    std::cout << "SIM aborted\n"; 
  } else {
    std::cout << "SIM complete\n";
  }

}
like image 131
Robᵩ Avatar answered Oct 23 '22 15:10

Robᵩ