fork download
  1. #include <signal.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4.  
  5. static volatile bool *termination_flag_ptr = NULL;
  6.  
  7. static void trap_ctrl_c(int sig)
  8. {
  9. if (termination_flag_ptr != NULL)
  10. {
  11. *termination_flag_ptr = true;
  12. }
  13. }
  14.  
  15. static void calculate_something(void)
  16. {
  17. volatile bool stop = false;
  18. // Регистрируем.
  19. termination_flag_ptr = &stop;
  20.  
  21. unsigned long long int n = 0;
  22. printf("Calculating, press Ctrl+C to stop...\n");
  23. while (!stop)
  24. {
  25. // Делаем какие-нибудь тяжелые вычисления.
  26. ++n;
  27. }
  28.  
  29. printf("Calculation was terminated at %llu\n", n);
  30. }
  31.  
  32.  
  33. int main(void)
  34. {
  35. signal(SIGINT, trap_ctrl_c);
  36. calculate_something();
  37. }
  38.  
Time limit exceeded #stdin #stdout 5s 2156KB
stdin
Standard input is empty
stdout
Standard output is empty