1

In linux, when a process is running, it call pthread_create. This will cause the process to generate a thread with the same PID, but different thread group ID. How can I query the thread group ID of this new thread?

NOTE: I cannot read it by adding some logging from the source code. Is there anything on the system that I can read to get those information? E.g., some where in /proc?

drdot
  • 3,215
  • 9
  • 46
  • 81
  • Maybe related because its performing similar introspection: [How to get thread id of a pthread in linux c program?](https://stackoverflow.com/q/21091000/608639) For completeness, the man pages don't appear to discuss thread group id's. – jww Oct 11 '17 at 01:31
  • NO. I dont have access to source code modification. – drdot Oct 11 '17 at 20:37

1 Answers1

2

The "go to" file will be /proc/PID/status. Most information you ever wanted to know about a Linux process is usually there. A random example (an actual thread group id is either Tgid or NStgid, which are usually the same, lacking container environment):

# cat /proc/8646/status

Name:   udevd
Umask:  0022
State:  S (sleeping)
Tgid:   8646
Ngid:   0
Pid:    8646
PPid:   1584
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 64
Groups:
NStgid: 8646
NSpid:  8646
NSpgid: 1584
NSsid:  1584
// skipped
oakad
  • 6,945
  • 1
  • 22
  • 31
  • hi, i can only see one Tgid and one NStgid although I have spawn two child threads in the main program. – drdot Oct 11 '17 at 19:20
  • There's one Tgid per application, unless you're doing something special with `clone`. That's why it's called "group id". – oakad Oct 12 '17 at 05:21
  • It suddenly occurred to me that your terminology may not be correct. At any rate, if you want to find all the running threads for a program, those will be visible in `/proc/PID/task` subdirectory, which will appear for all multi-threaded applications. You can list that subdirectory, compare the `status` files and find out how the PIDs, PPIDs and TGID's work out. – oakad Oct 12 '17 at 05:43
  • Yes. I already found that. If you can edit the answer with this. I will accept it. – drdot Oct 12 '17 at 06:22