在Linux中更改进程名称而不更改argv [0] [英] Change process name without changing argv[0] in Linux
问题描述
我需要用C语言修改程序的进程名称.
我精确地说,这不是我要更改的线程的名称.
我想更改程序的名称,但是找到的唯一解决方案是修改argv[0]
的值.
我还找到了prctl(PR_SET_NAME, "newname")
的另一种解决方案,但是该解决方案不起作用.
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.
推荐答案
调用prctl
和修改argv[0]
之间的区别是:
The differences between invoking prctl
and modify argv[0]
are:
- 修改
argv[0]
更改/proc/$pid/cmdline
中的信息
- 调用
prctl(PR_SET_NAME)
会更改/proc/$pid/status
中的信息
- modify
argv[0]
changes information in/proc/$pid/cmdline
- invoking
prctl(PR_SET_NAME)
changes information in/proc/$pid/status
这意味着您将获得发布ps -a
和ps -ax
的进程的名称.
That means you will get difference name of your process issuing ps -a
and ps -ax
.
如果在执行ps时期望不同的参数具有相同的进程名称,则可以同时使用它们(即,更改argv[0]
并调用prctl
).
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
).
希望答案会有所帮助.
这篇关于在Linux中更改进程名称而不更改argv [0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!