scc_lib_test_timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. //#include <thread.h>
  8. #include <time.h>
  9. #include <sys/wait.h>
  10. #define MS 1000000
  11. #define SIG_TIMER SIGRTMIN
  12. //#define _GNU_SOURCE
  13. void sig_TIMER_handler(int signo, siginfo_t *info, void *context)
  14. {
  15. printf("timer went off and i don't know what to do\n");
  16. }
  17. int main(int argc, char *argv[]){
  18. time_t cur_time;
  19. struct tm *cur_t;
  20. struct sigevent sev;
  21. struct itimerspec its;//, chk_timer;
  22. timer_t timerid;
  23. sigset_t sigset;
  24. struct sigaction sa;
  25. sev.sigev_notify = SIGEV_SIGNAL;
  26. sev.sigev_signo = SIG_TIMER;
  27. sev.sigev_value.sival_ptr = &timerid;
  28. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  29. sa.sa_flags = SA_RESTART | SA_SIGINFO;
  30. sigemptyset(&sigset);
  31. //sigaddset(&sigset, SIG_TIMER);
  32. sa.sa_mask = sigset;
  33. sa.sa_sigaction = sig_TIMER_handler;
  34. if (sigaction(SIG_TIMER, &sa, NULL) < 0) {
  35. perror("sigaction: SIG_TIMER");
  36. exit(1);
  37. }
  38. sigaddset(&sigset, SIG_TIMER);
  39. if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) < 0) {
  40. perror("signals_enable: sigprocmask");
  41. exit(1);
  42. }
  43. its.it_interval.tv_sec = 0;
  44. its.it_interval.tv_nsec = 0;
  45. its.it_value.tv_sec = 0;
  46. its.it_value.tv_nsec = 10 * MS;
  47. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error9");
  48. pause();
  49. printf("So far so good\n");
  50. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error9");
  51. pause();
  52. cur_time = time(NULL);
  53. cur_t = localtime(&cur_time);
  54. printf("[%d:%d:%d]: Everything went well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  55. /*FILE *app_input, *log_file;
  56. char app_input_file_name[64], log_file_name[64];
  57. int time_next;
  58. strcpy(app_input_file_name,"/home/herc/app_input.txt");
  59. //strcat(app_input_file_name, argv[1]);
  60. //strcat(app_input_file_name, "/app_input.txt");
  61. //printf("file path = %s\n",app_input_file_name);
  62. if ((app_input = fopen(app_input_file_name, "r")) == NULL){
  63. printf("Cannot open input file with file path = %s ",app_input_file_name);
  64. perror("open app_input");
  65. }
  66. fscanf(app_input,"%d",&time_next);
  67. printf("time next = %d\n",time_next);
  68. //strcpy(log_file_name,"./scenaria/");
  69. //strcat(log_file_name, scen_num);
  70. strcpy(log_file_name,"/home/herc/log_file_test.txt");
  71. if ((log_file = fopen(log_file_name, "w")) == NULL){
  72. //printf("open log my id is %d ",node_id);
  73. perror("open log");
  74. }
  75. fprintf(log_file,"poutanes\n");
  76. fclose(app_input);
  77. fclose(log_file);
  78. */
  79. return 0;
  80. }