在Linux中更改进程名称而不更改argv [0] [英] Change process name without changing argv[0] in Linux

查看:255
本文介绍了在Linux中更改进程名称而不更改argv [0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用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 -aps -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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆