How to set the name of a thread in Linux pthreads?
Use the prctl(2)
function with the option PR_SET_NAME
(see the docs).
Note that old versions of the docs are a bit confusing. They say
Set the process name for the calling process
but since threads are light weight processes (LWP) on Linux, one thread is one process in this case.
You can see the thread name with ps -o cmd
or with:
cat /proc/$PID/task/$TID/comm
or in between the ()
of cat /proc/$PID/task/$TID/stat
:
4223 (kjournald) S 1 1 1 0...
or from GDB info threads
between double quotes:
* 1 Thread 0x7ffff7fc7700 (LWP 6575) "kjournald" 0x00007ffff78bc30d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
How to name a thread in Linux?
Posix Threads?
This evidently won't compile, but it will give you an idea of where to go hunting. I'm not even sure its the right PR_
command, but i think it is. It's been a while...
#include <sys/prctl.h>
prctl(PR_SET_NAME,"<null> terminated string",0,0,0)
Is it possible to name a QThread?
Yes just give the name to the QThread object before starting it:
QThread* thr = new QThread(this);
thr->setObjectName("worker thread");
in the docs:
To choose the name that your thread will be given (as identified by
the command ps -L on Linux, for example), you can call setObjectName()
before starting the thread. If you don't call setObjectName(), the
name given to your thread will be the class name of the runtime type
of your thread object (for example, "RenderThread" in the case of the
Mandelbrot Example, as that is the name of the QThread subclass). Note
that this is currently not available with release builds on Windows.
Name a thread spun off by a library
One solution that works is as follows:
pthread_setname_np(pthread_self(), "SpunOffName");
init_the_library(...);
pthread_setname_np(pthread_self(), "OriginalName");
We just rename the main thread and the name is inherited by whatever thread(s) the library spins off. Then we rename the main thread back to its original name (or any other name we want).
Using prctl PR_SET_NAME to set name for process or thread?
Yes, you may use PR_SET_NAME
in the first argument and the name as the second argument to set the name of the calling thread(or process). prctl
returns 0
on success. Remember, it depends where you call this prctl
. If you call it inside your process, it will change the name of that process and all of its belonging threads. If you call it inside a specific thread, it will change only the name of that thread.
Example:
int s;
s = prctl(PR_SET_NAME,"myProcess\0",NULL,NULL,NULL); // name: myProcess
Now, if you are running your process in Linux, type:
top
or
ps
To see the name attached to your process id.
Change process name without changing argv[0] in Linux
The differences between invoking prctl
and modify argv[0]
are:
- modify
argv[0]
changes information in/proc/$pid/cmdline
- invoking
prctl(PR_SET_NAME)
changes information in/proc/$pid/status
That means you will get difference name of your process issuing ps -a
and ps -ax
.
If you expects same process name for different arguments while executing ps, you can do them both (i.e., change argv[0]
and invoke prctl
).
Hope the answer helps.
Change process name in Linux
I think this should work, to illustrate the principle...
#include <stdio.h>
int main(int argc, char *argv[]) {
argv[0][0] = 65;
sleep(10);
}
will change the name, and put an "A" instead of the first letter. CtrlZ to pause, then run ps
to see the name changed. I have no clue, but it seems somewhat dangerous, since some things might depend on argv[0]
.
Also, I tried replacing the pointer itself to another string; no cigar. So this would only work with strcpy
and strings shorter or equal than the original name.
There might or might not be a better way for this. I don't know.
EDIT: nonliteral solution: If you're forking, you know the child's PID (getpid()
in the child, result of fork()
in the parent). Just output it somewhere where you can read it, and kill the child by PID.
another nonliteral solution: make softlinks to the executable with another name (ln -s a.out kill_this_a.out
), then when you exec, exec the link. The name will be the link's name.
Related Topics
Add Month to a Variable Date in Shell Script
Which Capabilities Are Needed for Statx to Stop Giving Eperm
How to Inform Gcc to Not Use a Particular Register
Using Sed Replace Line in File with Another File
Script Produces Different Result When Executed by Bash Than by Cron
How to Find Files with Same Size
Why Doesn't Set -E Cause a Failure with 'False || False && True'
Ada Program Works in Linux But Not in Gps Windows 10
Fail If a Script Expects Input or Entering Passwords
Shuffle Output of Find with Fixed Seed
How to Install Rpy2 via Conda Using Default R Installation
"Bad Interpreter" Error Message When Trying to Run Awk Executable
How to Fix Numpy Dependencies Path on a Python 3.7.3 Script on Linux Frozen with Cx_Freeze 6.0B1
How to Determinate Destination MAC Address
Osx/Linux, Slow Down the Output from Terminal
Nasm Gcc Command Error with Subprogram as Seperate File
Bash Script to Remove Directories Based on Modified File Date