Guest User

Untitled

a guest
Sep 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. C Seconds ticking failing when need to process queries?
  2. void * reader_thread (void * arg) {
  3. while (1) {
  4. if (flag) {
  5. struct timeval tv;
  6. char timeBuf[10],secondBuf1[100],queryBuf1[500],queryBuf2[500];
  7. char buff[20] = {0};
  8. gettimeofday (&tv, NULL);
  9. //fprintf (stderr, "[%d.%06d] Flag set to 1 on ", tv.tv_sec, tv.tv_usec);
  10. tv.tv_sec -= 5;
  11. strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec));
  12. printf("nTime is %s", buff);
  13.  
  14. //select and insert queries
  15.  
  16. fprintf (stderr, " %sn", buff);
  17. flag = 0;
  18. }
  19. usleep (100); // will skew the processing but not signal delivery
  20. }
  21. return NULL;
  22. }
  23.  
  24. void callback (int sig) {
  25. flag = 1; // this is the only thing the callback does
  26. }
  27.  
  28. int main () {
  29. timer_t tid = 0;
  30. pthread_t thread;
  31. struct itimerspec it;
  32. char *localServer = "localhost", *remoteServer = "localhost";
  33. char *localUser = "user1", *remoteUser = "user2";
  34. char *localPassword = "****", *remotePassword = "*****";
  35. char *localDatabase = "db1", *remoteDatabase = "db1";
  36. localConn = mysql_init(NULL), remoteConn = mysql_init(NULL);
  37. if (!mysql_real_connect(localConn, localServer,
  38. localUser, localPassword, localDatabase, 0, NULL, 0)) {
  39. fprintf(stderr, "%sn", mysql_error(localConn));
  40. exit(1);
  41. }
  42.  
  43.  
  44.  
  45. pthread_create (&thread, NULL, reader_thread, NULL);
  46.  
  47. signal (SIGALRM, callback);
  48.  
  49. it.it_value.tv_sec = 1;
  50. it.it_value.tv_nsec = 0;
  51. it.it_interval.tv_sec = 1;
  52. it.it_interval.tv_nsec = 0;
  53. timer_create (CLOCK_REALTIME, NULL, &tid);
  54. timer_settime (tid, 0, &it, NULL);
  55.  
  56. while (1) sleep (100);
  57. return 0;
  58. }
  59.  
  60. sigset_t sigset;
  61. sigfillset(&sigset);
  62.  
  63. if (pthread_sigmask(
  64. SIG_BLOCK,
  65. &sigset,
  66. NULL))
  67. {
  68. perror("pthread_sigmask");
  69. }
  70.  
  71. pthread_create (&thread, NULL, reader_thread, NULL);
  72.  
  73. //sigset_t sigset;
  74. //sigemptyset(&sigset);
  75. sigaddset(&sigset, SIGALRM);
  76.  
  77. if (pthread_sigmask(
  78. SIG_UNBLOCK,
  79. &sigset,
  80. NULL))
  81. {
  82. perror("pthread_sigmask");
  83. }
  84.  
  85. sigset_t sigset;
  86. sigfillset(&sigset);
  87.  
  88. if (pthread_sigmask(
  89. SIG_BLOCK,
  90. &sigset,
  91. NULL))
  92. {
  93. perror("pthread_sigmask");
  94. }
  95.  
  96. sigset_t sigset;
  97. sigemptyset(&sigset);
  98. sigaddset(&sigset, SIGALRM);
  99.  
  100. if (pthread_sigmask(
  101. SIG_UNBLOCK,
  102. &sigset,
  103. NULL))
  104. {
  105. perror("pthread_sigmask");
  106. }
Add Comment
Please, Sign In to add comment