- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个带有固定时间片 1
的计时器,setitimer(SIGPROF, ×lice, 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, ×lice, 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, ×lice, 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/
我有一个包含 34 个变量和大约 25,000 个观测值的数据集。每个观察都涉及一个特定事件。它的格式如下: no id date .... 1 363 006
我已将 R 连接到 Twitter 并使用 R 中的 searchTwitter 函数进行抓取,并清除标点符号、小写字母等结果数据。现在我正在尝试执行以下操作: 计算自 2015 年 1 月 至 20
我正在研究项目,需要可视化频谱分析以设置一些精确参数。现在,我将垃圾箱转换为屏幕空间,因为在线性空间中,较低频率的幅度被压缩在一起。这是我在C++中的代码: float windowSize = 64
我正在尝试使用MATLAB导入WAV文件并创建如下所示的图表类型。我基本上是在尝试获取频率信息并根据分贝对其进行绘制。这是我正在使用的代码,但似乎无法正确提取频率信息: [x fs]=wavread(
我有一个 GUI,可以计算字符串中第一个字母的出现次数。我希望它以列格式计算所有字母,例如: 这是我到目前为止所拥有的: import java.awt.BorderLayout; import ja
我有一个由许多变量组成的全国调查,就像这个(为了简单起见,我省略了一些变量): year id y.b sex income married pens weight 2002
我被要求报告我们客户的联系频率,即每周、每月、每季度或每年看到多少客户。 当在论坛中讨论“频率”时,它们通常是指某个值在表中存在的次数。 我可以获得客户的联系人数量:- select A.cl
我正在尝试制作一款游戏,当麦克风发出足够响亮的声音时,我的角色会射击(在 Unity 中)。但是我不知道如何开始。 感谢您的帮助! 最佳答案 您可以通过使用 AudioSource.GetOutput
尝试计算字符数并改进我的代码,我做了一些更改,而不是使用 while 循环。好奇是否有人对我如何改进我的代码以使其更专业且更便宜有任何建议? #include int countingCharact
我正在创建一个 MySQL 数据库,其中包含大量带有时间戳的条目。这些条目将附加到特定用户和另一个索引(例如博客作者和他的几个网站)。计算用户/全局每日条目图表的最佳方法是什么。 我的两种方法是使用
我创建了一项调查并将其发送出去。该调查要求用户提供电子邮件,然后要求他们从包含 8 个不同选项的下拉菜单中选择要吃哪顿饭。有些人使用同一封电子邮件多次填写调查,但食物选择不同。 我有一个如下所示的 M
我有一个 MySQL 数据库: Date Customer_ID 我怎样才能把它变成: Customer_ID | Count_Visits_Past_Week | Count_Visits_
对于非常大的数据集,如何使用 gnuplot 仅在第一个和最后一个数据点的 x 轴上放置标记/标签? 最佳答案 在 gnuplot 4.6 及更高版本中,您可以使用命令 stats 'data.dat
我正在寻找一种方法来为具有共同词根/含义的单个词生成数值概率值。 用户将使用“舞者”、“跳舞”、“跳舞”等词生成内容。 如果“dancer”被提交了 30 次,跳舞了 5 次,我只需要一个值“danc
给定一个包含如下内容的数据集: [2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 65, 75, 85, 86, 87,
我想将声音的音高绘制成图表。 目前我可以绘制幅度。下图是由 getUnscaledAmplitude() 返回的数据创建的: AudioInputStream audioInputStream = A
在 Javascript 中,我试图获取一个初始的数值数组并计算其中的元素。理想情况下,结果将是两个新数组,第一个指定每个唯一元素,第二个包含每个元素出现的次数。不过,我愿意接受有关输出格式的建议。
我正在编写一个多线程OpenMPI应用程序,使用来自多个线程的MPI_Isend和MPI_Irecv在InfiniBand RDMA的各个列之间每秒交换数百条消息。 传输量约为400-800KByte
这个站点上有很多问题,询问如何在给定频率下创建简单的正弦波。我想做的是获取阵列或列表或任何频率,然后连续连续播放它们(而不是和弦),听起来有点像旧PC扬声器。我尝试使用Console.Beep,但是它
我使用我的App捕获声音。假设此声音是正弦1 KHz声音,并且存在背景声音。如何识别此1 KHz声音出现在声音上? 我的意思是,我可以想象如何在图像中找到元素,例如,如果您要在图像上寻找黄色正方形,那
我是一名优秀的程序员,十分优秀!