想了解execvp作品 [英] Trying to understand how execvp works

查看:163
本文介绍了想了解execvp作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 execvp 这么多的解释和答案,但一切都显得扑朔迷离。 execvp 接受命令,并将其发送到内核,然后做一些事情, execvp 创建图像, execvp 是系统调用, execvp ...等。与有限的C语言和Linux的经验,只有以下解释初学者似乎点击我:


  

它执行 LS 以2个参数:> awd.txt 。这等同于运行:

 'ls'的'>' awd.txt


下面是线程:什么execvp实际做

不过,我提出以下问题向谁回答的人,他不回答,所以我在拼命去了解它分开询问。

所以要执行 LS -a execvp ,我们应该做的 execvp( LS,参数);其中, ARGS {LS,-a} ?或 execvp(参数[0],参数)

有人能告诉我,如果这是真的: execvp 会将第一个参数从阵列 ARGS ,然后发送了内核遵循以下数组参数?因此, execvp(参数[0],参数); 是从 ARGS [] = {LS,-a} 阵列 ARGS [0] 需要 LS ,把它发送给内核,然后什么?它去寻找另一个 ARGS [0] 并找到 -a 并发送,内核决定了这两个品牌像 LS -a

命令
解决方案

原型为:

  INT execvp(为const char *文件,char * const的的argv []);

第一个参数,文件是要执行的程序的名称。 execvp 将搜索您的路径,并尝试找到一个匹配。从手册页:


  

的execlp(),execvp(),和execvpe()函数重复
   在搜索如果一个可执行文件中的壳的动作
   指定的文件名不包含斜杠(/)字符。结果
   该文件寻求目录的冒号分隔的列表
   在PATH环境变量指定的路径名​​。


如果/当execvp找到一个匹配,该程序将被加载到内存中,替换当前正在运行的程序。

新方案将看到的参数可在execvp指定的的argv 阵列。您预计将有一个空指针作为最后一个元素,或者你的程序可能会崩溃寻找一个空。

如果你这样做:

 的char * argv的[] = {酒吧,庆典,企鹅,0};
execvp(富,argv的);

再富启动时,就会产生同样的环境作为当前的程序,它的argc将是3和argv的将是上面的列表中。尽管该方案的名字是富,这的argv [0]运转时将显示为栏。

您实际上并不必须有新的程序的argv [0]是名称。预期的,但不是必需的。

顺便说一下,用你的例子

  LS> awd.txt

如果你这样做是在命令行上,您的shell会派生创造自己的新副本。孩子的外壳将开放awd.txt并做了dup2()把孩子FD连接到awd.txt为1一些其他的家务后,子shell会做这样的事情。

  execvp(LS,{LS,0})

所以在创建LS的argc == 1和argv [1] == 0。传统上,我们可以说,LS有0个参数。

I found so many explanations and answers on execvp but it all seems confusing. execvp takes a command and sends it to kernel and then does something, execvp creates an image, execvp is system call, execvp ...etc. As a beginner with limited C language and Linux experience, only the following explanation seemed to click for me:

It's executing ls with 2 arguments: > and awd.txt. It is equivalent to running:

'ls' '>' 'awd.txt'

Here is the thread: What does execvp actually do?

However, I asked the following question to the person who answered and he did not reply, hence I'm asking separately in a desperate attempt to understand it.

So to execute ls -a in execvp, we should do execvp ("ls", args); where args is {"ls", "-a"}? or execvp (args[0], args)?

Can someone tell me if it's true: execvp takes the first argument from the array args and then sends that to kernel to follow the following array arguments? So execvp(args[0],args); would be that from args [] = {"ls", "-a"} array the args[0] takes ls, sends it to the kernel and then what?It goes looking for another args[0] and finds -a and sends that, and the kernel determines that these two makes a command like ls -a?

解决方案

The prototype is:

  int execvp(const char *file, char *const argv[]);

The first argument, file is the name of the program to execute. execvp will search your path and try to find a match. From the manpage:

The execlp(), execvp(), and execvpe() functions duplicate the actions of the shell in searching for an executable file if the specified file name does not contain a slash (/) character.
The file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable.

If/when execvp finds a match, that program will be loaded into memory and replace your current running program.

The arguments the new program will see are the argv array specified in execvp. You are expected to have a null pointer as the last element, or your program may crash looking for a null.

If you do:

char *argv[]={"bar","bash","penguin",0};
execvp("foo",argv);

then when foo starts, it will have the same environment as your current program, its argc will be 3 and its argv will be the above list. Even though the program name is "foo", it's argv[0] while running will show up as "bar".

You don't actually have to have argv[0] be the name of the new program. It is expected, but not required.

Incidentally, with your example of

ls > awd.txt

If you did this on the command line, your shell would fork creating a new copy of itself. The child shell would open awd.txt and do a dup2() to put the child fd attached to awd.txt as 1. After some other housekeeping, the child shell would do something like.

execvp("ls", {"ls", 0})

so in the created "ls" argc==1 and argv[1] ==0. Traditionally , we would say that ls has 0 arguments.

这篇关于想了解execvp作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆