我有一个问题:有没有办法从 C++ 应用程序获取 linux 2.6 中的 SPID?当我执行“ps -amT”时,我可以看到进程中的线程:
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dba9b4b4af9beaebf5edecf5eaebebf5e9" rel="noreferrer noopener nofollow">[email protected]</a>:~# ps -amT
PID SPID TTY TIME CMD
1120 - pts/1 00:00:20 sncmdd
- 1120 - 00:00:00 -
- 1125 - 00:00:00 -
- 1126 - 00:00:00 -
- 1128 - 00:00:00 -
- 1129 - 00:00:09 -
- 1130 - 00:00:00 -
- 1131 - 00:00:09 -
1122 - pts/1 00:00:00 snstatusdemuxd
- 1122 - 00:00:00 -
- 1127 - 00:00:00 -
- 1132 - 00:00:00 -
- 1133 - 00:00:00 -
然后在文件系统中我可以看到线程:
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d7cacad1e594958b93928b9495958b97" rel="noreferrer noopener nofollow">[email protected]</a>:~# ls /proc/1120/task/
1120 1125 1126 1128 1129 1130 1131
那么有什么方法可以从我的应用程序中获取 SPID,以便我可以以某种方式识别每个正在运行的线程中的 SPID 是什么?
谢谢!
/迈克
编辑:我应该补充一点,从 getpid() 返回的 PID 在每个线程中都是相同的。
当我将此代码添加到我的线程时:
// Log thread information to syslog
syslog(LOG_NOTICE, "ibnhwsuperv: gettid()= %ld, pthread_self()=%ld", (long int)syscall(224), pthread_self());
我得到这个结果:
Jan 1 01:24:13 10 ibnhwsupervd[1303]: ibnhwsuperv: gettid()= -1, pthread_self()=839027488
两者都不像 ps 或 proc 文件系统中给出的 SPID。
另外,请注意 gettid does not return the SPID .
最佳答案
怎么样gettid() ?
编辑:如果您的 libc 没有 gettid() 函数,您应该像这样运行它:
#include <sys/syscall.h>
syscall(SYS_gettid);
...或参见example on this manual page .
关于c++ - 如何从C++获取linux 2.6中的SPID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/700426/