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;
}
4

1 に答える 1

0

プログラムをどのくらいの期間実行させますか? ITIMER_VIRTUAL は、プログラムが実際にプロセッサ時間を使用している場合にのみ減少します。あなたのプログラムはほとんどスリープしているだけなので、多くのプロセッサー時間を使用することはありません。確認するには、UNIX の「time」コマンド (または OS の同等のコマンド) を使用して、プログラムが使用する実際のユーザー時間とシステム時間を確認します。タイマーを有効にするには、リアルタイムだけで十分だと思います。

VIRTUAL および PROF タイマー間隔を (はるかに) 小さくするか、無限ループでブロックされないことを試すことができます (つまり、 sleep(1) を削除します)。

于 2014-02-12T13:36:48.370 に答える