2

Since a task_struct is allocated for each thread in Linux, how to I find the threads that belong to the same process?

So, that was the general question. To elaborate, I need to write a kernel function that traverses the threads that belong to a process (p), given a pointer to its task_struct or pid, and do something with them (e.g. collect some information).

1
  • Added an update, you can list the subdirectories of /proc/[pid]/task. Commented Jan 12, 2014 at 12:11

3 Answers 3

6

linux/sched.h has this function:

struct task_struct *next_thread(const struct task_struct *p); 

And other supporting functions, such as get_nr_threads().

You'll have to iterate like e.g.

struct task_struct *t = task; do { /*....*/ t = next_thread(t); } while (t != task); 

See also code in fs/proc/ which has a lot of code to traverse processes and threads (whose runtime info is seen in the /proc/<pid>/ directory tree)

Sign up to request clarification or add additional context in comments.

3 Comments

Would task be declared and instantiated as struct task_struct *task = pid_task(find_vpid(pid), PIDTYPE_PID);?
@Keeler That'd likely be one way, yes.
Thanks for the effort. This is the best answer.
3

Actually, you don't need task_struct at all.

See my gist for a example code on printing the threads of a process with a given pid.

Reference

Type man proc at the command line (online version), and find the entry for /proc/[pid]/task.

Quoting from the man page:

This is a directory that contains one subdirectory for each thread in the process. The name of each subdirectory is the numerical thread ID ([tid]) of the thread.

Note that one of the subdirectories in /proc/[pid]/task is whatever [pid] is (the pid of the program you're inspecting).

You can then gather other information using the pseudo-files in the directory /proc/[pid]/task/[tid] for each thread pid [tid].

Without C Code

ps -mo THREAD -p <pid> should work.

7 Comments

I was thinking more within the linux source code. I was searching for a command line kind'a thing. So, something like 'task->threads_list'
Did that work for you, though? You tagged no languages on your question, and didn't ask for code, hence why I answered with ps. If you're actually after a way to do this in code, specify what language, and edit your question to show code you've tried. If not, mark this as the answer if it works for you. :)
Linux kernel is written in C. Yes, I did not clearly state it. But I kind of hinted at it when I mentioned the task_struct.
Thank you for the tip though. I edited the question to better explain what I need.
Well, I know that. :) Now I see where you're coming from.
|
0

The answer lies in the field

 struct list_head ptrace_list; 

contained in task_struct

This link describes it as

list of parent that traces the process

You would then traverse it with

 list_for_each_entry() 

.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.