gpt4 book ai didi

c - 关于sighandler的频率

转载 作者:太空宇宙 更新时间:2023-11-04 04:56:47 25 4
gpt4 key购买 nike

我有一个带有固定时间片 1 的计时器,setitimer(SIGPROF, &timeslice, NULL);。当我用一个线程运行该进程时,一秒钟调用一次 sighandler。但是,如果进程中有两个或更多线程,它们将是每秒两次调用 sighandler。为什么会这样?或者如何拥有固定频率的定时器,独立于运行线程的数量。

附上可运行代码。打印输出显示 sighandler 每秒调用两次...(当 NUM_THREADS 更改为一个时情况并非如此)

#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 上并发运行。

最佳答案

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

关于c - 关于sighandler的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6359496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com