9

I have process id 1234. This process contains thread id 1235.

When I use ls -l /proc I see only the pid (1234) but when I open the thread status file using cat /proc/1235/status I can see data.

Why is that?

Can I access with C code directly with tid? /proc/1235/mem without knowing the process id?

1 Answer 1

13

Yes, /proc “contains” directory entries for thread identifiers as well as process identifiers, but only the latter are enumerated by getdents, so ls only shows process identifiers. This is described in man 5 proc, in the “Overview” section, since release 5.00 of the man-pages project:

/proc/[pid] subdirectories

The /proc/[pid] subdirectories are visible when iterating through /proc with getdents(2) (and thus are visible when one uses ls(1) to view the contents of /proc).

/proc/[tid] subdirectories

The /proc/[tid] subdirectories are not visible when iterating through /proc with getdents(2) (and thus are not visible when one uses ls(1) to view the contents of /proc).

Why is that?

I suspect it’s to preserve backwards-compatibility (for programs written before threads existed in their current form on Linux), and to limit scalability problems.

Can I access with C code directly with tid? /proc/1235/mem without know the process id?

Yes, if you know the tid you can access /proc/${tid} directly, without going through the pid.

If you want to enumerate threads, you can list the directory entries under /proc/${tid}/task/ (this works with any thread identifier, not just process identifiers).

4
  • Is there any other command to view thread Id in /proc as ls command doesn't work? Commented Mar 5, 2022 at 11:16
  • "…I suspect it's to preserve compatibility…" My opinion is that it is linked to the fact that The thread ID of the initial (main) thread is the same as the process ID of the entire process. Commented Mar 5, 2022 at 11:43
  • @MC68020 exactly, so programs expecting to find “pids” only in /proc don’t break since tids don’t appear there. Commented Mar 5, 2022 at 12:00
  • @Prvt_Yadav ls /proc/*/task/ Commented Mar 5, 2022 at 12:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.