Linux doesn’t actually distinguish between processes and threads, and implements both as a data structure task_struct.
So what does Linux provide to some programs for them to tell threads of a process from its child processes? For example, Is there a way to see details of all the threads that a process has in Linux?
Thanks.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
From a task_struct perspective, a process’s threads have the same thread group leader (group_leader in task_struct), whereas child processes have a different thread group leader (each individual child process).
This information is exposed to user space via the /proc file system. You can trace parents and children by looking at the ppid field in /proc/${pid}/stat or .../status (this gives the parent pid); you can trace threads by looking at the tgid field in .../status (this gives the thread group id, which is also the group leader’s pid). A process’s threads are made visible in the /proc/${pid}/task directory: each thread gets its own subdirectory. (Every process has at least one thread.)
In practice, programs wishing to keep track of their own threads would rely on APIs provided by the threading library they’re using, instead of using OS-specific information. Typically on Unix-like systems that means using pthreads.
Method 2
-
This runs the
topcommand with some extra options:top -H -b -n 1
- The
-Hargument instructs top to display each individual thread. Normally top summarizes all threads under their parent process. - The
-bargument makes top run in batch mode – the information is gathered, displayed, and then dumped to stdout as opposed to running in an interactive mode and refreshing the data displayed. - With the
-boption, the user must tell top how many times to run, this is done with the-nargument and a final argument with how many times to run.
So
top -H -b -n 1instructs the system to “run top, display individual threads, run in batch mode, and only run once”. - The
-
The
pscommand reports a snapshot of currently running processes.ps -eLf
The
-eLfargument (can be used as-e -L -fas well) breaks down as follows:etellspsto display all processes regardless of who owns them or their current status – active, sleeping, paused, waiting for I/O, etc.Ltellspsto show individual threads- the
ftellspsto format the output as a full-format listing, and in conjunction with theLargument the NLWP (number of threads) and LWP (thread ID) columns are added to the output.
Method 3
Consider a process with PID p1
The task_struct object of a child process will have PPID (parent PID) as p1, and it’s PID and TGID set to, say, p2.
The task_struct object of a thread of p1 will have PID set as, say p3, but TGID set to p1.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0