我是 C 的新手,想知道当子进程调用 exec 系统调用来执行新程序时会发生什么?
exec()
函数系列将current process
替换为指定为其第一个参数的new process image
。
int execl(const char *path, const char *arg, ...);
例如
main() {
execl("/bin/ls","ls",NULL);
}
当你执行上面的代码时,你的current running process(a.out
(将被替换为名为ls的new process
。
您可以使用fork()
并探索更多内容。
main() {
if(fork()==0){ /** child process is in sleep for 5 second**/
sleep(5);
exit(0);/** once job is done child need to send it's status to parent process using exit **/
}
else { /** parent process **/
wait(0);/** parent waits upto child got done, then it replaces whole child child pcb with parent PCB **/
execl("/bin/ls","ls",NULL);
}
}
我希望它有所帮助。
从exec
手册页,
exec(( 系列函数替换了当前进程映像 使用新的进程映像。
因此,子进程将替换为执行进程。