POSIX API调用来列出一个进程中运行的所有的pthreads [英] POSIX API call to list all the pthreads running in a process

查看:84
本文介绍了POSIX API调用来列出一个进程中运行的所有的pthreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个POSIX / Linux环境中的多线程应用程序 - 我有超过code,创建与pthreads没有控制权。在某些点的过程 - 与pthreads的所有者 - 接收信号

I have a multi-threaded application in a POSIX/Linux environment - I have no control over the code that creates the pthreads. At some point the process - owner of the pthreads - receives a signal.

这信号的处理程序应中止,取消或停止所有与pthreads和日志多少的pthreads哪里运行。

The handler of that signal should abort,cancel or stop all the pthreads and log how many pthreads where running.

我的问题是,我无法找到如何列出进程中运行的所有的pthreads。

My problem is that I could not find how to list all the pthreads running in process.

推荐答案

似乎没有要枚举线程进程中的任何可移植的方式。

There doesn't seem to be any portable way to enumerate the threads in a process.

Linux具有 pthread_kill_other_threads_np ,它看起来像从原来单纯的,用户空间pthreads实现,可能会或可能不会像今天记录工作的后遗症。它不会告诉你多少线程有。

Linux has pthread_kill_other_threads_np, which looks like a leftover from the original purely-userland pthreads implementation that may or may not work as documented today. It doesn't tell you how many threads there were.

您可以通过查看得到了很多关于你的过程信息的/ proc / $ ,其中 $ 是你的进程ID。尽管许多Unix系统有一个同名文件或目录,布局是完全不同的,因此,使用任何code 的/ proc 将是Linux的特定的。的的/ proc 的文档在文档/文件系统/在内核源代码proc.txt 。尤其是,的/ proc / $$ /任务为每个线程的子目录。子目录的名称是LWP ID;不幸的是, [1] [2] <一个href=\"http://groups.google.com/group/comp.programming.threads/browse_thread/thread/be8b6968e97ecc9a\">[3]似乎没有为是LWP IDS与pthread的IDS相关联的方法(但你可以用 gettid得到自己的线程ID(2)如果你为它工作)。当然,阅读的/ proc / $$ /任务不是原子;线程的数量可通过原子的/ proc / $$ /状态(当然你采取行动之前,它可能会改变)。

You can get a lot of information about your process by looking in /proc/$$ where $$ is your process ID. Although many unices have a file or directory with that name, the layout is completely different, so any code using /proc will be Linux-specific. The documentation of /proc is in Documentation/filesystems/proc.txt in the kernel source. In particular, /proc/$$/task has a subdirectory for each thread. The name of the subdirectory is the LWP id; unfortunately, [1][2][3] there doesn't seem to be a way to associate LWP ids with pthread ids (but you can get your own thread id with gettid(2) if you work for it). Of course, reading /proc/$$/task is not atomic; the number of threads is available atomically through /proc/$$/status (but of course it might change before you act on it).

如果你不能达到你想和你的Linux从的pthreads得到的支持有限的东西,另一种策略是发挥动态链接的技巧,提供自己的在pthread_create 版本它记录到数据结构,你可以事后检查。

If you can't achieve what you want with the limited support you get from Linux pthreads, another tactic is to play dynamic linking tricks to provide your own version of pthread_create that logs to a data structure you can inspect afterwards.

这篇关于POSIX API调用来列出一个进程中运行的所有的pthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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