繁体   English   中英

如何从 C 程序中调用 ps -u Linux/Unix 命令

[英]How to call the ps -u Linux/Unix command from within a C program

命令“ps -u myusername”显示我的帐户名下所有当前正在运行的进程。 我如何在 C 程序中调用它?

x = fork(); //Create a new process fork.
if(x == 0) {
  //Then the fork was created successfully.  use the new fork to call ps.
  char *argv[2] = {"-u" ,"myusername"};
  execv("/bin/ps", argv);
}
char *argv[3] = {"ps", "-l", NULL};
execv("/bin/ps", argv); 

记得在 args 的末尾和开头分别添加 NULL 和命令。 (这里是 ps 和 NULL)

http://linux.die.net/man/3/system
http://linux.die.net/man/3/popen
尝试其中之一。 系统执行 shell 命令,除了重定向到文件或 fifo 之外,无法获得输出。 popen 是您想要的,您将创建一个 /bin/ps 进程并将所有数据通过管道传输到您的程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM