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.
Asked
Active
Viewed 1.4k times
8

Alexis Wilke
- 19,179
- 10
- 84
- 156

Jérémy Magrin
- 1,182
- 1
- 9
- 15
-
possible duplicate of [How to name a thread in Linux?](http://stackoverflow.com/questions/778085/how-to-name-a-thread-in-linux) – sehe Apr 23 '13 at 09:33
-
_"The documentation says PR_SET_NAME sets the process name; but that documentation is wrong - it does actually set the thread name. Now "top" and "ps -L" show the thread name."_ -- [user9876 Apr 22 '09 at 17:39](http://stackoverflow.com/questions/778085/how-to-name-a-thread-in-linux#comment588964_778124) – sehe Apr 23 '13 at 09:34
-
2PR_SET_NAME flag is supported since Linux 2.6.9 – akhil Apr 23 '13 at 09:55
2 Answers
18
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.

Caterpillar
- 599
- 6
- 20

Wayne
- 227
- 2
- 4
-2
try this:
char *process_name = "aaa\0";
memcpy((void *)argv[0], process_name, sizeof(process_name));
/* explain: The space allocated for argv[0] could be smaller than the name that you want to give and then you will be overwritting some other unrelated memory. argv[0] size could be just 2 and if your process name is "averylongprocessname" you will be overflowing argv[0]. You need to strlen(argv[0]) and use that in memcpy. thx @ecerulm
*/

lingyfh
- 1,363
- 18
- 23
-
3I think that the \0 is unnecessary - isn't it already silently appended to a literal string of characters? – Chap Apr 30 '14 at 04:09
-
-
1Seems that you have to overwrite all characters of origin `argv[0]`, is it true? So you can't have process names bigger than original name – Luca Marturana Feb 24 '15 at 10:05
-
The space allocated for `argv[0]` could be smaller than the name that you want to give and then you will be overwritting some other unrelated memory. `argv[0]` size could be just 2 and if your process name is "averylongprocessname" you will be overflowing `argv[0]`. You need to `strlen(argv[0])` and use that in `memcpy`. – RubenLaguna Mar 09 '15 at 10:21
-
7The `\0` is unnecessary. Also, `sizeof` is incorrect here as it would return the size of a pointer (typically 4 or 8 bytes). You want something more like `strncpy(argv[0], process_name, strlen(process_name)` or `memcpy(argv[0], process_name, (strlen(argv[0]) < strlen(process_name)+1) ? strlen(argv[0]) : strlen(process_name)+1)` – craig65535 Mar 20 '15 at 17:07