linux - setitimer, SIGALRM & 多线程进程 (linux, c)

标签 linux multithreading linux-kernel profiling signals

我想在 linux 2.6+ 的多线程进程中使用 setitimer()(或者不太可能,alarm())和支持 NPTL 的 libc。哪个线程将从内核接收 sigalarm (SIGALRM)

谢谢。

2014-04 更新:如果我想编写像 gperftools 的 cpuprofile 这样的分析工具,我应该如何在多线程程序中设置 setitimer();但在我的工具中,我想同时支持动态链接程序(因此可以将我自己的库注入(inject)到初始化分析中)和静态链接程序(不可能做 ^^^^^^)。

我当前的分析工具在 fork() 之后和 exec() 之前设置 setitimer,它还使用 ptrace 来控制目标程序并劫持 setitimer 生成的 SIGPROF/SIGVPROF/SIGALRM。我不知道它如何与多线程程序一起工作。

最佳答案

来自 signal(7)手册页:

A process-directed signal may be delivered to any one of the threads that does not currently have the signal blocked. If more than one of the threads has the signal unblocked, then the kernel chooses an arbitrary thread to which to deliver the signal.

现在,alarm(2)手册页说:

alarm() arranges for a SIGALRM signal to be delivered to the process in seconds seconds.

因此,信号被传送到一个进程(一个信号也可能被定向到某个线程),因此您不知道哪个线程会收到它。

setitimer(2)相同:

When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.

您可以在除一个线程之外的所有线程中阻止 SIGALARM,然后您可以确定它会被传送到唯一的线程。假设您正在使用 pthreads,您可以使用 pthread_sigmask() 来阻止信号.

关于linux - setitimer, SIGALRM & 多线程进程 (linux, c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2586926/

相关文章:

python - 如何在其他设备/磁盘上安装和运行 rodeo-python?

linux - linux系统中的低cpu空闲时间

Linux 内核 API 更改/添加

linux-kernel - Linux 内核可以作为 ARM TrustZone 安全操作系统运行吗?

java - 无法将 Java 客户端连接到 C 服务器

c++ - 为什么 gcc 不报告跟随重复符号的错误?

从 linux 主机控制 Jabra 710 USB 扬声器音量

c# - 寻找一些 UI 线程和工作线程教程

c# - 这是状态消息显示的最佳方式吗?

java - 我无法从 AsyncTask 获得结果?