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.
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
The differences between invoking
prctl
and modifyargv[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
andps -ax
.If you expects same process name for different arguments while executing ps, you can do them both (i.e., change
argv[0]
and invokeprctl
).Hope the answer helps.
try this:
/* 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
*/