setitimer,SIGALRM&多线程进程(Linux,C) [英] setitimer, SIGALRM & multithread process (linux, c)

查看:438
本文介绍了setitimer,SIGALRM&多线程进程(Linux,C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在启用了NPTL的libc的linux 2.6+中的多线程进程中使用setitimer()(或更不可能是alarm()).哪个线程将从内核接收sigalarm (SIGALRM)?

I want to use setitimer() (or less probable, the alarm()) in multithreaded process in linux 2.6+ with NPTL-enabled libc. Which thread will receive sigalarm (SIGALRM) from kernel?

谢谢.

2014-04更新:如果我想编写像gperftools的cpuprofile这样的分析实用程序,应该如何在多线程程序中设置setitimer();但是在我的工具中,我想同时支持动态链接程序(因此可以注入我自己的库以进行初始化配置文件)和静态链接程序(无法执行^^^^^^).

2014-04 update: How should I set the setitimer() in multithreaded program, if I want to write a profiling utility like gperftools's cpuprofile; but in my tool I want to support both dynamically linked programs (so it is possible to inject my own library to init profiling) and statically linked programs (without the possibility of doing ^^^^^^).

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

My current profiling tool works with setting setitimer just after fork() and before exec(), and it also uses ptrace to get control over the target program and to hijack SIGPROF/SIGVPROF/SIGALRM generated by the setitimer. I have no exact idea how it works with multithreaded programs.

推荐答案

来自 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()安排SIGALRM信号 交付给流程 秒秒.

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

因此,信号被传递到进程(信号也可能直接指向特定线程),因此您不知道哪个线程将接收该信号.

So, the signal is delivered to a process (a signal might be directed at certain thread too) and thus you do not know which of the threads will receive it.

setitimer(2)相同:

当任何计时器到期时,会发出一个信号 发送到进程和计时器 (可能)重新启动.

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

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

You could block SIGALARM in all your threads except one, then you could be certain that it will be delivered to that only thread. Assuming you are using pthreads, you can block signals with pthread_sigmask().

这篇关于setitimer,SIGALRM&amp;多线程进程(Linux,C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆