如何在 C 程序中设置命令行参数,以便在用户键入“ps aux”时可见?
当您键入“ps aux”时,ps 命令会显示程序运行时使用的命令参数。某些程序会更改此方式作为指示状态的方式。我尝试过更改 argv[] 字段,但似乎不起作用。是否有标准方法来设置命令行参数,以便它们在用户键入 ps 时出现?
也就是说,这不起作用:
int main(int argc,char **argv)
{
argv[0] = "Hi Mom!";
sleep(100);
}
09:40 imac3:~$ ./x &
[2] 96087
09:40 imac3:~$ ps uxp 96087
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
yv32 96087 0.0 0.0 2426560 324 s001 S 9:40AM 0:00.00 ./x
09:40 imac3:~$ cat x.c
When you type "ps aux" the ps command shows command arguments that the program was run with. Some programs change this as a way of indicating status. I've tried changing argv[] fields and it doesn't seem to work. Is there a standard way to set the command line arguments so that they appear when the user types ps?
That is, this doesn't work:
int main(int argc,char **argv)
{
argv[0] = "Hi Mom!";
sleep(100);
}
09:40 imac3:~$ ./x &
[2] 96087
09:40 imac3:~$ ps uxp 96087
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
yv32 96087 0.0 0.0 2426560 324 s001 S 9:40AM 0:00.00 ./x
09:40 imac3:~$ cat x.c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的想法是正确的,但是你没有改变
argv[n]
中的指针,你必须改变argv[0]<指向的字符串。 /code> 本身:(
请注意,这是否实际上更改了
ps
显示的命令名称取决于系统)。You had the right idea, but you don't change the pointers in
argv[n]
, you must change the string pointed to byargv[0]
itself:(Note that whether or not this actually changes the command name shown by
ps
is system-dependent).