这是我的第一个文件:
int main(int argc, char *argv[]){
printf("\n\nJe suis dans le fils\n\n");
execl("/exec","ii",NULL);
printf("\n\nJe suis revenu dans le fils\n\n");
}
这是可执行文件:
int main(int argc, char *argv[]){
printf("\n\n\n\nJe suis dans le exec\n\n\n\n");
printf("\n\n argc = %d \n\n argv[0] = %s \n\n argv[1] = %s",argc,argv[0],argv[1]);
}
问题:
我在 exec 文件中被阻塞,所以在 1s 文件中 execl
之后的 printf
不显示:/
而且当我正常执行 argv[0] 的 printf 时,它应该向我显示文件路径,但在这里它向我显示“ii”
最佳答案
I'm block in the exec file, so the printf after my execl, in the 1s file, doesn't show
没错,execl
不会返回(除非出现错误)。一旦您 exec
就没有回头路了:实际上您的进程将自己变成另一个进程,执行不同的程序。
when I do the printf of argv[0] normally it should show me the file path but here it shows me "ii"
正如您发现的那样,argv[0]
不应被视为理所当然。父进程可以在其中指定它想要的任何内容(甚至省略它)。在您的情况下,您 将 child 的 argv[0]
指定为 ii
。如果您想正确设置它(无论是什么),请添加第二个 "/exec"
。
旁注:如果您想要一种简单的方法来执行某些操作,您可能需要查看标准的 system(3)
和 popen(3)
函数。
关于c - 在 fork() 中,execl 不会返回父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18540289/