#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
pid = fork();
if(0 == pid)
{
execl("/home/me/hello","./hello",NULL);
printf("this is child\n"); //这句代码不会被执行,因为exec执行的代码将会替代子进程所有的空间内容
exit(0);
}
sleep(1);
printf("this is parent\n");
return 0;
}
调用exec函数,直接执行hello程序,这样就避免了子进程和父进程执行相同的代码,故子进程执行自己的,父进程执行自己的。