开发者

execv() quirks concerning arguments

开发者 https://www.devze.com 2023-02-25 02:11 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号