Advertisement
Guest User

timer

a guest
Nov 17th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. /*
  2.  * compile gcc main.c -lrt -o test
  3. */
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>
  10.  
  11. pthread_t t;
  12.  
  13. struct object {
  14.     timer_t *timerid;
  15.     struct sigevent *sev;
  16.     struct itimerspec *its;
  17.     int count;
  18.     void **func;
  19. };
  20.  
  21. struct thread_func {
  22.     void (*func) ( void *data );
  23.     void *data;
  24. };
  25.  
  26. void func_hello ( void *data ) {
  27.     printf ( "func_hello\n" );
  28. }
  29.  
  30. void func_neho ( void *data ) {
  31.     printf ( "func_neho\n" );
  32. }
  33.  
  34. void func_fuck ( void *data ) {
  35.     printf ( "func_fuck %s\n", (char *) data );
  36. }
  37.  
  38. void hh ( union sigval val ) {
  39.     struct thread_func *thf = ( struct thread_func * ) val.sival_ptr;
  40.  
  41.     thf->func ( thf->data );
  42. }
  43.  
  44. void *handle ( void *data ) {
  45.     struct object *obj = ( struct object * ) data;
  46.  
  47.     char ch = getchar ( );
  48. }
  49.  
  50. struct object *obj_init ( ) {
  51.     struct object *obj = calloc ( 1, sizeof ( struct object ) );
  52.     obj->timerid = calloc ( 0, sizeof ( timer_t ) );
  53.     obj->sev = calloc ( 0, sizeof ( struct sigevent ) );
  54.     obj->its = calloc ( 0, sizeof ( struct itimerspec ) );
  55.     obj->func = calloc ( 0, sizeof ( void * ) );
  56.     obj->count = 0;
  57.     return obj;
  58. }
  59.  
  60. void obj_set_timer ( struct object *obj, int num, void (*func ) ( void *data ), void *data ) {
  61.     int index = 0;
  62.     obj->count++;
  63.     obj->timerid = realloc ( obj->timerid, sizeof ( timer_t ) * obj->count );
  64.     obj->sev = realloc ( obj->sev, sizeof ( struct sigevent ) * obj->count );
  65.     obj->its = realloc ( obj->its, sizeof ( struct itimerspec ) * obj->count );
  66.     obj->func = realloc ( obj->func, sizeof ( void * ) * obj->count );
  67.  
  68.     obj->sev[index].sigev_notify = SIGEV_THREAD;
  69.     obj->sev[index].sigev_notify_function = hh;
  70.     obj->sev[index].sigev_signo = SIGALRM;
  71.     struct thread_func *thf = calloc ( 1, sizeof ( struct thread_func ) );
  72.     thf->func = func;
  73.     thf->data = data;
  74.     obj->sev[index].sigev_value.sival_ptr = thf;
  75.  
  76.     if ( timer_create ( CLOCK_REALTIME, &obj->sev[index], &obj->timerid[index] ) == -1 ) fprintf ( stderr, "error timer_create.\n" );
  77.  
  78.     obj->its[index].it_value.tv_sec = num;
  79.     obj->its[index].it_value.tv_nsec = 0;
  80.     obj->its[index].it_interval.tv_sec = num;
  81.     obj->its[index].it_interval.tv_nsec = 0;
  82.     if ( timer_settime ( obj->timerid[index], 0, &obj->its[index], NULL ) == -1 ) fprintf ( stderr, "error timer settimer.\n" );
  83. }
  84.  
  85. int main ( ) {
  86.  
  87.     struct object *obj = obj_init ( );
  88.    
  89.     obj_set_timer ( obj, 1, func_hello, NULL );
  90.     obj_set_timer ( obj, 4, func_neho, NULL );
  91.     obj_set_timer ( obj, 6, func_fuck, "test" );
  92.  
  93.     char ch = getchar ( );
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement