[英]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.