Linux c 中的 execl 函数
execl function in Linux c
我开始Linux内部概念。在下面的程序中,为什么第二个printf
没有被执行?
#include <unistd.h>
#include <stdio.h>
int main(void) {
printf("Before execl");
execl("/bin/ls", "ls", "-l", NULL);
printf("After execl");
return 0;
}
第二个 printf
未执行,因为 execl
接管了您的应用程序进程并将其替换为新的指定进程。根据手册页:
The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for execve(2) for further details about the replacement of the current process image.)
并且来自 execve(2)
手册页:
execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded. If the current program is being ptraced, a SIGTRAP is sent to it after a successful execve().
如果您希望您的程序继续执行并显示第二个 printf
,您必须在 execl
(或 exec
系列中的任何函数)中调用 fork
child。 C标准库函数system()
就是干这个的,可以查看源码here.
我开始Linux内部概念。在下面的程序中,为什么第二个printf
没有被执行?
#include <unistd.h>
#include <stdio.h>
int main(void) {
printf("Before execl");
execl("/bin/ls", "ls", "-l", NULL);
printf("After execl");
return 0;
}
第二个 printf
未执行,因为 execl
接管了您的应用程序进程并将其替换为新的指定进程。根据手册页:
The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for execve(2) for further details about the replacement of the current process image.)
并且来自 execve(2)
手册页:
execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded. If the current program is being ptraced, a SIGTRAP is sent to it after a successful execve().
如果您希望您的程序继续执行并显示第二个 printf
,您必须在 execl
(或 exec
系列中的任何函数)中调用 fork
child。 C标准库函数system()
就是干这个的,可以查看源码here.