简体   繁体   中英

Preformant way to get a PID from a TID

This question is not about pthread IDs, but about values typically obtained by gettid or ptrace .

Does the linux kernel provide any mechanism to obtain the thread group id (usually obtained by getpid ) from a given tid in a relatively efficient manner? Something that doesn't require io or parsing?

The only method seems to open /proc/TID/status , read the file into a buffer, scan line-by-line for Tgid , and then parse the string as an unsigned integer.

I'm hoping that there is a syscall I missed that returns the tgid/pid given the pid, even if the id is returned indirectly within a some data structure.

There seems to be no such system call. The information can only be retrieved from /proc , and your current solution looks like the most efficient way to do so.

just adding to the already available answers. Indeed there is no better alternative on Linux. I was looking for something better myself, and I found this project which was attempting to offer something more usable programmatically:

https://criu.org/Task-diag

Nothing of that however is upstream, probably since too few people seem to actually care about the problem at the moment. It is mostly a concern for high performance OS-level tracing applications and real time, which are just in their infancy in Linux.

An interesting fact, other similar OSes do not have this problem (OpenBSD, QNX, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM