本文介绍了在TID状态更改时获取信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以通过 waitpid 更改pid/tid状态,但这是阻止功能.

我想监视特定pid中的所有线程,并在其中一个线程更改并打印tid时获得信号.

现在,我以该进程中的线程数打开线程,并在1个tid上每执行1个 waitpid ,然后在阻塞功能完成后,我将更改的tid打印出来.

如何获得tid更改的信号,以便可以在1个线程中监视所有tid.

我不想只监视系统特定pid/tid中的所有pid.

那些小玩意儿/小玩意儿不是我过程中的孩子.

解决方案

如果您使用的是POSIX线程,则可以使用 pthread_cleanup_push pthread_cleanup_pop 在线程退出时调用清理"功能.

此清理"功能随后可以将用户信号之一( SIGUSR1 SIGUSR2 )发送到进程,然后捕获该进程并将其视为关于线程的信号终止.

如果您使用 sigqueue 您可以为信号处理程序添加线程ID,以便知道刚刚退出的线程.

您可以使用 pthread_sigmask 阻止所有线程中的用户信号,以确保仅将其传递给主进程线程(或使用 pthread_sigqueue 专门发送到主进程线程).

There is a way to look when pid/tid status change with waitpid but this is blocking function.

I want to monitor all threads in specific pid and get signal when one of them change and print the tid.

For now I open threads as count of threads in that process and each 1 make waitpid on 1 tid and after that blocking function finish I print that tid that changed.

How can I get a signal that tid change so I can monitor all tid's in 1 thread.

I didn't want to monitor all pid in system only specific pid/tid.

Those tids/pids are not children of my process.

解决方案

If you're using POSIX threads, then you could use pthread_cleanup_push and pthread_cleanup_pop to call a "cleanup" function when your thread is exiting.

This "cleanup" function could then send one of the user signals (SIGUSR1 or SIGUSR2) to the process which then catches it and treats it as a signal about thread termination.

If you use sigqueue you can add the thread-id for the signal handler so it knows which thread just exited.

You can use pthread_sigmask to block the user signal in all threads, to make sure it's only delivered to the main process thread (or use pthread_sigqueue to send to the main process thread specifically).

这篇关于在TID状态更改时获取信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:15