fork(2) download
  1. #include <iostream>
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <signal.h>
  7. #include <errno.h>
  8. #include <assert.h>
  9.  
  10. #include <unistd.h>
  11. #include <sys/time.h>
  12. #include <sys/select.h>
  13.  
  14. extern "C" {
  15. static int xsignal (int sig, void (*handler)(int))
  16. {
  17. struct sigaction sa;
  18. sa.sa_handler = handler;
  19. sigemptyset(&sa.sa_mask);
  20. sa.sa_flags = 0;
  21. return sigaction(sig, &sa, NULL);
  22. }
  23.  
  24. void sigalarm_handler (int) {}
  25. }
  26.  
  27. int main ()
  28. {
  29. if (xsignal(SIGALRM, sigalarm_handler) != 0) {
  30. perror("sigaction");
  31. exit(EXIT_FAILURE);
  32. }
  33.  
  34. struct itimerval it = { { 0, 200000 }, { 0, 150000 } };
  35. if (setitimer(ITIMER_REAL, &it, 0) != 0) {
  36. perror("setitimer");
  37. exit(EXIT_FAILURE);
  38. }
  39.  
  40. struct timeval tv;
  41. for (int i = 0; i < 10; ++i) {
  42. ssize_t select_result = select(0, 0, 0, 0, 0);
  43. assert(select_result < 0 && errno == EINTR);
  44. // get a sample
  45. //...
  46. gettimeofday(&tv, 0);
  47. std::cerr << tv.tv_usec/1000 << std::endl;
  48. }
  49. }
  50.  
Success #stdin #stdout 0s 2892KB
stdin
Standard input is empty
stdout
Standard output is empty