linux 关闭setitimer_setitimer,SIGALRM&多线程进程(Linux,C)

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?

Thanks.

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 ^^^^^^).

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.

解决方案

From signal(7) man page:

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.

Now, alarm(2) man page says that:

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.

The same with setitimer(2):

When any timer expires, a signal is

sent to the process, and the timer

(potentially) restarts.

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().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值