How does execvp run a command?

后端 未结 4 1221
温柔的废话
温柔的废话 2020-12-23 21:54

I know execvp can be used to execute simple commands as follows:

char* arg[] = {\"ls\", \"-l\", NULL};
execvp(arg[0],arg);

I w

4条回答
  •  佛祖请我去吃肉
    2020-12-23 22:34

    My intuition is I have to read the file and may be parse it to store the arguments in the arg. However I want to make sure.

    Your intuition is largely correct. The cat utility that you're using as an example has two separate code paths:

    • If there are filenames specified as arguments, it will open and read each one in turn.
    • If there are no filenames specified, it will read from standard input.

    This behavior is specifically implemented in the cat utility -- it is not implemented at any lower level. In particular, it is definitely not part of the exec system call. The exec system calls do not "look at" arguments at all; they just pass them straight on to the new process in argv, and that process gets to handle them however it sees fit.

提交回复
热议问题