问题描述
当你输入的ps auxps命令显示程序与运行命令的参数。某些程序更改此为指示状态的一种方式。我试着改变的argv []字段,它似乎并没有工作。有没有让他们出现设置命令行参数的标准方式,当用户键入PS?
这是,这并不工作:
INT主(INT ARGC,字符** argv的)
{
的argv [0] =嗨妈妈!
睡眠(100);
}09:40 IMAC3:〜$ ./x&安培;
[2] 96087
09:40 IMAC3:〜$ ps的UXP 96087
USER PID%CPU%MEM VSZ RSS TT STAT启动时COMMAND
yv32 96087 0.0 0.0 2426560 324 S001小号9:40 AM 0:00.00 ./x
09:40 IMAC3:〜$猫x.c
您有正确的想法,但你没有在修改的指针的的argv [N]
,您必须更改指向的字符串的argv [0]
本身:
的#include<&string.h中GT;
#包括LT&;&unistd.h中GT;INT主(INT ARGC,字符** argv的)
{
为size_t MAXLEN = strlen的(的argv [0]); memset的(argv的[0],0,maxlen的);
strncat函数(的argv [0],妈妈你好!,MAXLEN);
暂停(); 返回0;
}
(请注意,这是否实际上改变由 PS
显示的命令名称取决于系统)。
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
You had the right idea, but you don't change the pointers in argv[n]
, you must change the string pointed to by argv[0]
itself:
#include <string.h>
#include <unistd.h>
int main(int argc,char **argv)
{
size_t maxlen = strlen(argv[0]);
memset(argv[0], 0, maxlen);
strncat(argv[0], "Hi Mom!", maxlen);
pause();
return 0;
}
(Note that whether or not this actually changes the command name shown by ps
is system-dependent).
这篇关于如何设置在C程序的命令行参数,以便当用户键入&QUOT是可见的,的ps aux&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!