关于sighandler的频率

时间:2011-06-15 14:39:08

标签: c multithreading operating-system posix signals

我有一个具有固定时间片1 s,setitimer(SIGPROF, &timeslice, NULL);的计时器。当我用一个线程运行进程时,sighandler会在一秒钟内调用一次。但是,如果进程中有两个或更多线程,则它们将每秒调用两次sighandler。为什么会这样?或如何使用固定频率的计时器,与运行的线程数无关。

附加了可运行代码。打印输出显示sighandler每秒调用两次...(当NUM_THREADS更改为1时不是这种情况)

#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>//itimerval
#include <signal.h>//sigaction, SIGPROF...
#include <pthread.h>//pthread_create()/join()
#include <stdint.h>//uint32_t
#include <stdlib.h>//exit()
using namespace std;

#ifndef NUM_THREADS
#define NUM_THREADS 20
#endif

struct sigaction old_act_;

int init_timer(int which, int musec) {
    struct itimerval timeslice;
    timeslice.it_interval.tv_sec = musec / 1000000;
    timeslice.it_interval.tv_usec = musec % 1000000;
    timeslice.it_value.tv_sec = musec / 1000000;
    timeslice.it_value.tv_usec = musec % 1000000;
    setitimer(which, &timeslice, NULL);
    return 0;
}

void remove_timer(int which)
{
    struct itimerval timeslice;
    timeslice.it_interval.tv_sec = 0;
    timeslice.it_interval.tv_usec = 0;
    timeslice.it_value.tv_sec = 0;
    timeslice.it_value.tv_usec = 0;
    setitimer(which, &timeslice, NULL);
}

void install_handler(int signo, void(*handler)(int))
{
    sigset_t set;
    struct sigaction act;
    act.sa_handler = handler;
    act.sa_flags = SA_RESTART;
    sigaction(signo, &act, &old_act_);
    sigemptyset(&set);
    sigaddset(&set, signo);
    sigprocmask(SIG_UNBLOCK, &set, NULL);
    return;
}

void uninstall_handler(int signo, bool to_block_signal)
{
    sigaction(signo, &old_act_, 0);
    if(to_block_signal)
    {
        // block the signal
        sigset_t set;
        sigemptyset(&set);
        sigaddset(&set, signo);
        sigprocmask(SIG_BLOCK, &set, NULL);
    }
}

void handler(int signo)
{
    pid_t tid = syscall(SYS_gettid);
    pid_t pid = syscall(SYS_getpid);
    struct timeval now;
    gettimeofday(&now, NULL);
    printf("sig %d, pid %d, tid %d, time = %u.%06u\n", signo, static_cast<int>(pid), static_cast<int>(tid), static_cast<uint32_t>(now.tv_sec), static_cast<uint32_t>(now.tv_usec));
}

void * threadRun(void * threadId)
{
    for(uint64_t i = 0; i < 10000000000; i++)
    {
        //sleep(1);
    }
}

int main()
{
    int tick_musec = 1000000;
    init_timer(ITIMER_PROF, tick_musec);
    install_handler(SIGPROF, handler);

    pthread_t threads[NUM_THREADS];
    long t;
    for (t = 0; t < NUM_THREADS; t++) {
        printf("In main: creating thread %ld\n", t);
        if (pthread_create(&threads[t], NULL, threadRun, (void *) t)) {
            printf("ERROR; return from pthread_create()\n");
            exit(-1);
        }
    }
    void * status;
    for (t = 0; t < NUM_THREADS; t++) {
        if(pthread_join(threads[t], &status)) {
          printf("ERROR; return from pthread_join()\n");
          exit(-1);
        }
        printf("Main: completed join with thread %ld having a status of %ld\n", t, (long) status);
    }

    remove_timer(ITIMER_PROF);
    uninstall_handler(SIGPROF, true);
    printf("Main: program completed. Exiting.\n");
    return 0;
}

以下是在具有4个双核处理器的计算机上运行时的输出。

sig 27, pid 21542, tid 21544, core 6, time = 1308168237.023219
sig 27, pid 21542, tid 21544, core 6, time = 1308168237.276228
sig 27, pid 21542, tid 21549, core 6, time = 1308168237.528200
sig 27, pid 21542, tid 21545, core 7, time = 1308168237.781441
sig 27, pid 21542, tid 21551, core 6, time = 1308168238.037210
sig 27, pid 21542, tid 21554, core 6, time = 1308168238.294218
sig 27, pid 21542, tid 21551, core 6, time = 1308168238.951221
sig 27, pid 21542, tid 21546, core 0, time = 1308168239.064665
sig 27, pid 21542, tid 21561, core 1, time = 1308168239.335642
sig 27, pid 21542, tid 21558, core 1, time = 1308168240.122650
sig 27, pid 21542, tid 21555, core 6, time = 1308168240.148192
sig 27, pid 21542, tid 21556, core 1, time = 1308168240.445638
sig 27, pid 21542, tid 21543, core 7, time = 1308168240.702424
sig 27, pid 21542, tid 21559, core 1, time = 1308168240.958635
sig 27, pid 21542, tid 21555, core 6, time = 1308168241.210182
sig 27, pid 21542, tid 21550, core 7, time = 1308168241.464426
sig 27, pid 21542, tid 21553, core 0, time = 1308168241.725650
sig 27, pid 21542, tid 21555, core 6, time = 1308168241.982178
sig 27, pid 21542, tid 21547, core 7, time = 1308168242.234422
sig 27, pid 21542, tid 21560, core 0, time = 1308168242.503647
sig 27, pid 21542, tid 21546, core 0, time = 1308168242.766902
sig 27, pid 21542, tid 21550, core 7, time = 1308168243.018414
sig 27, pid 21542, tid 21552, core 0, time = 1308168243.270643
sig 27, pid 21542, tid 21550, core 7, time = 1308168243.556412

编辑:我有点解决了这个问题(另见@ n.m.的回答)。 sighandler在多处理器机器上运行频率更高的原因是SIGPROF基于CPU时间而不是实时发送出去。 CPU时间可能比实时大,如果CPU时间为2秒,则可能是一个进程在1秒内在2个CPU上同时运行两个线程。

1 个答案:

答案 0 :(得分:0)

您可以使用pthread_sigmask(...)来阻止创建的主题中的SIGPROF。实际上,建议只有一个线程处理所有信号。