#include #include // getpid #include // for nanosleep #include // for signal, don't worry about this yet. using namespace std; static void Handler(int) { cout << "\t\tBRRRRRIIIINNNNGGGGG!!!!!!!!" << endl; return; } int main(int argc, char * argv[]) { timespec want, left ={0,0}; want.tv_sec = 5; want.tv_nsec = 0; if (argc > 1) { want.tv_sec = static_cast (atoi(argv[1])); } // bad, there are better ways to do this. // Don't worry about this for now. // This is for demo purposes only. signal(SIGUSR1, Handler); pid_t mypid = getpid(); cout << "Hello, this is process " << mypid << endl; do { cout << "\tTrying to sleep for " << want.tv_sec << " seconds " << want.tv_nsec << " nanoseconds" << endl; if (-1 == nanosleep(&want, & left) ) { cout << "\tShoot, someone got me up early, " << left.tv_sec << " seconds " << left.tv_nsec << " nanoseconds left" << endl; cout << endl; } want = left; // left is not changed on success, reset it for next pass. left = {0,0}; } while (want.tv_sec > 0 and want.tv_nsec > 0); cout << "All done sleeping " << endl; return 0; }