2014-02-12 2 views
0

다른 신호를 처리하는 데 사용하려는이 코드가 있습니다. 왜 timer_handler2()로 이동하지 않을지 모르겠다. 그냥 timer_handler()를 사용합니다. 누군가 내가 친절하게 나에게 무엇을하고 있는지 말해 줄 수 있습니까?하나 이상의 신호 처리기를 사용하지 않는 코드

#include <stdio.h> 
#include <signal.h> 
#include <unistd.h> 
#include <sys/time.h> 
#include <string.h> 

struct timeval theTime; 
static int count = 0; 

void timer_handler2(int signum) { 
    printf("timer 2 expired %d times\n", ++count); 
} 

void timer_handler(int signum) {  
    printf("timer 1 expired %d times\n", ++count); 
} 

void timer_handler3(int signum) { 

    printf("timer 3 expired %d times\n", ++count); 
} 

int main() { 
    struct itimerval timer, timer2, timer3, got;  

    signal(SIGVTALRM, timer_handler2); 
    signal(SIGALRM, timer_handler); 
    signal(SIGPROF, timer_handler3); 

    /* ... and every 1000 msec after that. */ 
    timer2.it_interval.tv_sec = 1; 
    timer2.it_interval.tv_usec = 0; 
    /* Configure the timer to expire after 1000 msec... */ 
    timer2.it_value.tv_sec = 1; 
    timer2.it_value.tv_usec = 0; 

    /* ... and every 1000 msec after that. */ 
    timer.it_interval.tv_sec = 0; 
    timer.it_interval.tv_usec = 0; 
    /* Configure the timer to expire after 1000 msec... */ 
    timer.it_value.tv_sec = 1; 
    timer.it_value.tv_usec = 250000; 

    /* ... and every 1000 msec after that. */ 
    timer3.it_interval.tv_sec = 1; 
    timer3.it_interval.tv_usec = 0; 
    /* Configure the timer to expire after 1000 msec... */ 
    timer3.it_value.tv_sec = 1; 
    timer3.it_value.tv_usec = 0; 

    /* Start a real timer. It counts down whenever this process is 
    executing. */ 
    setitimer(ITIMER_VIRTUAL, &timer2, NULL); 
    setitimer(ITIMER_REAL, &timer, NULL); 
    setitimer(ITIMER_PROF, &timer3, NULL); 

    int counter = 0; 
    while (1) { 
     sleep(1); 
     counter++; 
    } 

    return 0; 
} 
+0

너무 많은 코드! 간단한 예제를 만들 수 있습니까? –

+0

@OliCharlesworth 코드를 줄였습니다 –

+0

멋지 네요. 이제는 더 쉽게 읽을 수 있습니다! 감사. –

답변

0

프로그램을 실행시키는 데 얼마나 걸립니까? ITIMER_VIRTUAL은 프로그램이 실제로 프로세서 시간을 사용할 때만 감소합니다. 프로그램이 대부분 잠자고 있기 때문에 많은 프로세서 시간을 사용하지 않을 것입니다. 확인하려면 unix 'time'명령 (또는 OS 상당)을 사용하여 프로그램에서 사용한 실제, 사용자 및 시스템 시간을 확인하십시오. 타이머 만 활성화하면 실시간으로 충분할 것입니다.

VIRTUAL 및 PROF 타이머 간격을 많이 줄이거 나 무한 루프에서 차단하지 않는 것을 시도 할 수 있습니다 (예 : 수면 제거 (1)).