简体   繁体   中英

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

The command "ps -u myusername" shows all of the currently running processes under my account name. How do I call this in a C program?

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); 

Remember to add NULL and the command in the end and beginning of args respectivly. (Here ps and NULL)

http://linux.die.net/man/3/system
http://linux.die.net/man/3/popen
try one of these. System executes a shell command with no way to get the output except to redirect to a file or fifo. popen is what you want you will create a /bin/ps process and pipe all the data to your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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