I need to modify the process name of my program in C language.
I precise, this is not the name of a thread that I want to change.
I want to change the name of my program, but the only solution I found, is to modify the value of argv[0]
.
I also found another solution with prctl(PR_SET_NAME, "newname")
, but this solution doesn't work.
With recent kernels you can also do prctl(PR_SET_NAME, (unsigned long) "My Process", 0, 0, 0); - but not all utilities use that name as the default display name (top does, ps does not) and it has a limit of 16 characters.
The differences between invoking prctl
and modify argv[0]
are:
argv[0]
changes information in /proc/$pid/cmdline
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With