问题描述
|
我有一个计时器,计时器的时间片为
1
s,setitimer(SIGPROF,×lice,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,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,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,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,time = 1308168237.276228
sig 27,tid 21549,time = 1308168237.528200
sig 27,tid 21545,core 7,time = 1308168237.781441
sig 27,tid 21551,time = 1308168238.037210
sig 27,tid 21554,time = 1308168238.294218
sig 27,time = 1308168238.951221
sig 27,tid 21546,core 0,time = 1308168239.064665
sig 27,tid 21561,core 1,time = 1308168239.335642
sig 27,tid 21558,time = 1308168240.122650
sig 27,tid 21555,time = 1308168240.148192
sig 27,tid 21556,time = 1308168240.445638
sig 27,tid 21543,time = 1308168240.702424
sig 27,tid 21559,time = 1308168240.958635
sig 27,time = 1308168241.210182
sig 27,tid 21550,time = 1308168241.464426
sig 27,tid 21553,time = 1308168241.725650
sig 27,time = 1308168241.982178
sig 27,tid 21547,time = 1308168242.234422
sig 27,tid 21560,time = 1308168242.503647
sig 27,time = 1308168242.766902
sig 27,time = 1308168243.018414
sig 27,tid 21552,time = 1308168243.270643
sig 27,time = 1308168243.556412
编辑:我已经解决了问题(另请参阅@ n.m。\的答案)。 sighandler在多处理器计算机上运行频率更高的原因是,SIGPROF是根据cpu时间而不是实时发送的。 cpu时间可能大于实时时间,例如,如果cpu时间为2秒,则可能是一个进程在1秒内有两个线程在2个cpu上同时运行。
解决方法
您可以使用
pthread_sigmask(...)
阻止创建的线程中的SIGPROF
。实际上,建议仅使用一个线程来处理所有信号。