Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

c - Change process name without changing argv[0] in Linux

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...