2016-06-16 3 views
0
int main() 
{ 
     int i=0; 
     timer_t t_id[100]; 
     for(i=0; i<100; i++) 
     makeTimer(NULL, &t_id[i], i+1, 0); 

     /* Do busy work. */ 
     while (1); 
} 





static int makeTimer(char *name, timer_t *timerID, int sec, int msec) 
    { 
     struct sigevent   te; 
     struct itimerspec  its; 
     struct sigaction  sa; 
     int      sigNo = SIGRTMIN; 

     /* Set up signal handler. */ 
     sa.sa_flags = SA_SIGINFO; 
     sa.sa_sigaction = timer_handler; 
     sigemptyset(&sa.sa_mask); 

     if (sigaction(sigNo, &sa, NULL) == -1) 
     { 
      printf("sigaction error\n"); 
      return -1; 
     } 

     /* Set and enable alarm */ 
     te.sigev_notify = SIGEV_SIGNAL; 
     te.sigev_signo = sigNo; 
     te.sigev_value.sival_ptr = timerID; 
     timer_create(CLOCK_REALTIME, &te, timerID); 

     its.it_interval.tv_sec = sec; 
     its.it_interval.tv_nsec = msec * 1000000; 
     its.it_value.tv_sec = sec; 

     its.it_value.tv_nsec = msec * 1000000; 
     timer_settime(*timerID, 0, &its, NULL); 

     return 0; 
    } 

    static void timer_handler(int sig, siginfo_t *si, void *uc) 
    { 
     timer_t *tidp; 
     tidp = si->si_value.sival_ptr; 

     /* how to know the timer_id with index? */ 
    } 

이 코드에서는 배열 구조로 만든 타이머를 여러 개 만들었습니다.여러 타이머 신호를 처리하는 방법

시간 초과 이벤트가 발생하면 모든 타이머가 하나의 timer_handler를 공유합니다. 그러나 어떤 인덱스 (t_id [index]) 시간 초과가 발생했는지 어떻게 알 수 있습니까?

가능합니까? 나는 어떤 인덱스의 타임 아웃이 발생했는지 알고 싶다.

+2

당신은 해고시킨 타이머를 식별하는 멤버를 포함하는 구조체에'sival_ptr' 점을 가질 수 있습니다. – EOF

+0

그러나 처리기에서 타이머 인덱스를 어떻게 알 수 있습니까? '경우 (* tidp == t_id [0]) 다른 경우 (* tidp == t_id [1]) 또 경우 (* tidp == t_id [2]) ... ' 너무 크다 많은 타이머를 다루는 것 – allen

+1

@EOF가 말했듯이 구조체를 사용하십시오. 구조체는'timer_t' 식별자와 배열 인덱스 (또는 특정 타이머에 대한 다른 고유 한 식별자)를 포함 할 수 있습니다. 그런 다음'timer_t' 배열 대신에이 구조의 배열을 만듭니다. –

답변

1

간단한 예 :

struct timer_event_data 
{ 
    timer_t id; 
    size_t index; // Array index, not really needed IMO 
    // Other possible data 
}; 

#define NUMBER_TIMER_EVENTS 100 

int main(void) 
{ 
    struct timer_event_data timer_data[NUMBER_TIMER_EVENTS]; 

    for (size_t i = 0; i < NUMBER_TIMER_EVENTS; ++i) 
    { 
     // Initialize structure 
     timer_data[i].index = i; 

     // Start timer 
     makeTimer(NULL, &timer_data[i], i + 1, 0); 
    } 

    // Main processing loop 
    for (;;) 
    { 
     // ... 
    } 

    return 0; 
} 

void makeTimer(const char *name, struct timer_event_data *timer_data, unsigned sec, unsigned msec) 
{ 
    ... 
    te.sigev_value.sival_ptr = timer_data; 
    timer_create(CLOCK_REALTIME, &te, &timer_data->id); 
    ... 
} 

static void timer_handler(int sig, siginfo_t *si, void *uc) 
{ 
    struct timer_event_data *timer_data = si->si_value.sival_ptr; 
    // Use data in structure any way you want 
} 
0

당신 t_id의 첫번째 요소에 대한 글로벌 포인터를 정의 할 수 있습니다 :

timer_t t_id[100]; 
pt_id = t_id; 

을 그리고 타이머 함수 내에서 복용이 필요로하는 지수를 계산 :

timer_t * pt_id; 

main()에서 초기화를을 이런 배열의 차이 :

size_t tidx = tidp - pt_id - 1; 
구조를 사용
+1

* Eww *, 전역. 그리고 'const'일 수도 없습니다. – EOF

+0

@ EOF : "* could *"이라고 썼습니다 ... ;-) – alk

관련 문제