我在ubuntu下的exec()函数有问题.有没有可能回到主程序?
例:
printf("Some instructions at beginning\n");
execlp("ls","ls", "-l", NULL);
// i want to continue this program after exec and see the text below
printf("Other instructions\n");
Run Code Online (Sandbox Code Playgroud)
否.成功的exec呼叫将当前程序替换为另一个程序.如果你想同时得到父母和孩子留下来,你需要调用fork(2)之前exec:
pid_t childpid = fork();
if(childpid < 0)
{
// Handle error
}
else if(childpid == 0)
{
// We are the child
exec(...);
}
else
{
// We are the parent: interact with the child, wait for it, etc.
}
Run Code Online (Sandbox Code Playgroud)
请注意,失败的exec调用(例如,给定的可执行文件不存在)确实会返回.如果exec返回,则总是因为错误,所以要准备好处理错误.
| 归档时间: |
|
| 查看次数: |
7099 次 |
| 最近记录: |