I'm trying to output the results of 'ps' for an assignment, but I've ran into some slight quirks that I'd like to figure out.
This works correctly, and displays ps's output in the shell:
//Create "ps -u laser"
int pid_ps = fork();
char *argv[1] = {"-u laser"};
if (pid_ps == 0) {
execv("/bin/ps", argv);
}
Now, this is the format that was given in my slides, but just gives the "usage:" and "format is one or more of:" notifications for ps when it's run:
//Create "ps -u laser"
int pid_ps = f开发者_如何学JAVAork();
char *argv[2] = {"-u", "laser"};
if (pid_ps == 0) {
execv("/bin/ps", argv);
}
I also tried putting 0 and NULL on the end of the array, which was suggested in other answers, but no luck.
I'm probably overlooking something minor, but I'm not too familiar with c. Any insight is appreciated.
Use char *argv[] = {"/bin/ps", "-u", "laser", 0};
- the first argument is the executable name.
EDIT: Sample.
int pid_ps = fork();
if (pid_ps == 0) {
char *argv[] = {"/bin/ps", "-u", "laser", 0};
execv("/bin/ps", argv);
}
The above snippet is tested and working.
精彩评论