Skip to content

Instantly share code, notes, and snippets.

@itsmikeq
Created February 12, 2017 02:31
Show Gist options
  • Save itsmikeq/b02e42ed7d46ebd179f8159ce71d5c64 to your computer and use it in GitHub Desktop.
Save itsmikeq/b02e42ed7d46ebd179f8159ce71d5c64 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <unistd.h>
#include <csignal>
#include <sys/select.h>
using namespace std;
void signalHandler( int signum ) {
cout << "Interrupt signal (" << signum << ") received.\n";
// cleanup and close up stuff here
// terminate program
exit(signum);
}
int main () {
int i = 0;
int n;
// register signal SIGINT and signal handler
signal(SIGINT, signalHandler);
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(STDIN_FILENO, &readSet);
struct timeval tv = {10, 0}; // 10 seconds, 0 microseconds;
if (select(STDIN_FILENO+1, &readSet, NULL, NULL, &tv) < 0) perror("select");
bool b = (FD_ISSET(STDIN_FILENO, &readSet)) ? (cin>>n) : false;
if(b==1)
cout<<"input is integer for n and it's correct"<<endl;
else
cout<<"Either n is not integer or no input for n"<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment