![](/img/trans.png)
[英]What happens to the child process, when the parent process calls an exec command
[英]What happens to the child process after calling exec?
我是系統編程的新手,並對exec函數的工作方式感到好奇。 我的第一個問題是,為什么孩子在打電話給exec之后從不打印“我是孩子”。 exec如何替換子進程? 我的第二個問題是,為什么程序(在調用exec之后)繼續運行,並在完全終止之前要求另外一個命令行參數。 我不確定這是怎么回事。 任何人都可以解釋發生了什么將不勝感激。 這是代碼:
#include <stdio.h>
#include <unistd.h>
int main(void) {
if(fork() == 0){
printf("Hello from child!\n");
execl("/usr/bin/sort", "sort", "talk.c",NULL);
printf("I'm the child\n");
}
else{
printf("Hello from parent!\n");
printf("Iam the parent\n");
}
return 0;
}
您可以在https://linux.die.net/man/3/execl中了解有關execl
的信息
exec()系列函數將當前過程映像替換為新的過程映像。 本手冊頁中描述的功能是execve(2)的前端。 (有關替換當前過程映像的更多詳細信息,請參見execve(2)的手冊頁。)
exec
系列用新的過程映像替換當前過程映像,因此execl
之后什么也不會發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.