C++ Tutorial/Development/signal — различия между версиями


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:28, 25 мая 2010

Using signal handling

#include <iostream>
#include <iomanip>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void signal_handler( int );
int main()
{
   signal( SIGINT, signal_handler );
   srand( time( 0 ) );
   
   for ( int i = 1; i < 101; i++ ) {
      int x = 1 + rand() % 50;
      
      if ( x == 25 )
         raise( SIGINT );
         
   }
   return 0;
}
void signal_handler( int signalValue )
{
   cout << "\nInterrupt signal (" << signalValue
        << ") received.\n"
        << "Do you wish to continue (1 = yes or 2 = no)? ";
   int response;
   cin >> response;
   while ( response != 1 && response != 2 ) {
      cout << "(1 = yes or 2 = no)? ";
      cin >> response;
   }
   
   if ( response == 1 )
      signal( SIGINT, signal_handler );
   else
      exit( EXIT_SUCCESS );
}