c – How do I set the name of the thread in Linux PThreads?

Is there a way to set the thread name in Linux?

My main purpose is to help during debugging, if you pass for example /proc/$PID/task/$TID/…

Use the prctl(2) function together with the PR_SET_NAME option (see the docs).

Please note that the documentation is a bit orderly People are confused. They say

Set the process name for the calling process

But because threads on Linux are lightweight Level Process (LWP), so in this case, a thread is a process.

You can use ps -o cmd or use the following command to view the thread name:

p>

cat /proc/$PID/task/$TID/comm

Or between cat / proc / $PID / task / $TID / stat ():

4223 (kjournald) S 1 1 1 0...

Or from the GDB information thread between double quotes:

* 1 Thread 0x7ffff7fc7700 (LWP 6575) "kjournald" 0x00007ffff78bc30d in nanosleep () at ../sysdeps/unix/syscall-template.S:84

Is there a way to set the name of the thread in Linux?

My main purpose is to help during debugging, if you pass e.g. /proc/$PID/task/$TID/…

Use the prctl(2) function with the PR_SET_NAME option (see the docs).

Please note that the documentation is a bit confusing. They say

Set the process name for the calling process

But because the thread on Linux is a lightweight process (LWP), so in this case , A thread is a process.

You can use ps -o cmd or use the following command to view the thread name:

cat /proc/$PID/ task/$TID/comm

Or between cat / proc / $PID / task / $TID / stat ():

4223 ( kjournald) S 1 1 1 0...

Or from the GDB message thread between double quotes:

* 1 Thread 0x7ffff7fc7700 (LWP 6575) "kjournald" 0x00007ffff78bc30d in nanosleep () at ../sysdeps/unix/syscall-template.S:84

Leave a Comment

Your email address will not be published.