为什么 exec() 系列函数不执行 exec() 之后的代码? [英] why the exec() family of functions doesn't execute the code after exec()?

查看:55
本文介绍了为什么 exec() 系列函数不执行 exec() 之后的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

手册页说exec() 系列函数用新的进程映像替换当前进程映像."但我不太明白用新的过程映像替换当前的过程映像"的含义.例如,如果 exec 成功,则不会到达 perror

the man page says that "The exec() family of functions replaces the current process image with a new process image." but I am not quite understand the meaning of "replaces the current process image with a new process image". For example, if exec succeed, perror would not be reached

execl("/bin/ls", /* Remaining items sent to ls*/ "/bin/ls", ".", (char *) NULL);
perror("exec failed");

推荐答案

正确.如果 exec 有效,则不会调用 perror,因为对 perror 的调用不再存在.

Correct. If the exec works, the perror will not be called, simply because the call to perror no longer exists.

我发现在教育新手了解这些概念时,有时更容易将 UNIX 执行模型视为由进程、程序程序实例组成.

I find it's sometimes easier when educating newcomers to these concepts, to think of the UNIX execution model as being comprised of processes, programs and program instances.

程序是可执行文件,例如 /bin/ls/sbin/fdisk(注意,这不包括诸如 bash或 Python 脚本,因为在这种情况下,实际的可执行文件将是 bashpython 解释器,而不是脚本).

Programs are executable files such as /bin/ls or /sbin/fdisk (note that this doesn't include things like bash or Python scripts since, in that case, the actual executable would be the bash or python interpreter, not the script).

程序实例是已经加载到内存中并且基本在运行的程序.虽然只有一个像 /bin/ls 这样的程序,但在任何给定时间可能会有多个它的实例在运行,例如,如果你和我同时运行它.

Program instances are programs that have been loaded into memory and are basically running. While there is only one program like /bin/ls, there may be multiple instances of it running at any given time if, for example, both you and I run it concurrently.

加载到内存"短语是进程出现的地方.进程只是程序实例可以在其中运行的容器".

That "loaded into memory" phrase is where processes come into the picture. Processes are just "containers" in which instances of programs can run.

因此,当您fork一个进程时,您最终会得到两个不同的进程,但它们仍然各自运行相同的不同实例> 程序.fork 调用通常被称为一个进程调用但两个进程返回的调用.

So, when you fork a process, you end up with two distinct processes but they're still each running distinct instances of the same program. The fork call is often referred to as one which one process calls but two processes return from.

同样,exec 不会对进程本身产生影响,但它丢弃该进程中的当前程序实例并启动所请求程序的新实例.

Likewise, exec will not have an effect on the process itself but it will discard the current program instance in that process and start a new instance of the requested program.

成功的 exec 调用中的这个丢弃决定了它后面的代码(在本例中为 perror)不会被调用.

This discard in a successful exec call is what dictates that the code following it (perror in this case) will not be called.

这篇关于为什么 exec() 系列函数不执行 exec() 之后的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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