Does /proc/ contains tid

procprocess

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?

Best Answer

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.

Related Question