3

A Linux thread or forked process may change its name and/or its commandline as visible by ps or in the /proc filesystem.

When using the python-setproctitle package, the same change occurs on /proc/pid/cmdline, /proc/pid/comm, the Name: line of /proc/pid/status and in the second field of /proc/pid/stat, where only cmdline is showing the full length and the other three locations are showing the first 15 chars of the changed name.

When watching multithreaded ruby processes, it looks like the /proc/pid/cmdline remains unchanged but the other three locations are showing a thread name, truncated to 15 chars.

man prctl tells that /proc/pid/comm is modified by the PR_SET_NAME operation of the prctl syscall but it does not say anything about /proc/pid/status and /proc/pid/stat.

man proc says /proc/pid/comm provides a superset of prctl PR_SET_NAME which is not explained anymore.
And it tells that the second field of /proc/pid/stat would still be available even if the process gets swapped out.

When watching JVM processes, all the mentioned locations give identical contents for all threads (the three places other than cmdline all showing java), but jcmd pid Thread.print still shows different thread names for the existing threads, so it looks like Java threads are using some non-standard mechanism to change their name.

Is /proc/pid/comm always identical to the Name: line of /proc/pid/status and the second field of /proc/pid/stat or are there circumstances where one of these three places is offering different contents ?
Please provide an (easy to reproduce) example if differences are possible.

1 Answer 1

3

All three entries are defined close together in the kernel source: comm, stat, and status. Working forwards from there, comm is handled by comm_show which calls proc_task_name to determine the task’s name. stat is handled by proc_tgid_stat, which is a thin wrapper around do_task_stat, which calls proc_task_name to determine the task’s name. status is handled by proc_pid_status, which also calls proc_task_name to determine the task’s name.

So yes, comm, the “Name” line of status and the second field of stat all show the same value. The only variations are whether the value is escaped or not: status escapes it (replacing special characters), the others don’t.

1
  • Thanks for the explanation. This allows me to use the /proc/pid/comm file only and to ignore the others.
    – Juergen
    Commented Feb 23 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .