写了两个程序1.c和2.c
1.c中vfork了一个子进程,在子进程中使用execlp去执行2.c
希望实现的执行顺序是父进程-->子进程-->2.c-->父进程
结果却是发现实际的执行顺序是父进程-->子进程-->父进程;最后才是2.c执行完成 (整个1.c执行完成后,execlp才执行完成。)
请高手们帮忙看看,应该怎样修改?谢谢!
以下是源代码:
[2.c]
#include
main()
{
printf("2.c %ld\n",getpid());
}
[1.c]
#include
#include
int main(int argc,char ** argv)
{
int child;
printf("ID PID F/S PID\n"); // (F/S:Father/Son)
printf("--------------------------------------\n");
printf("father %ld\n",getpid());
if(!(child=vfork()))
{
printf("son %ld %ld\n",getpid(),getppid());
execlp("./2","./2",NULL);
//exit(1);
}
else
printf("father %ld\n",getpid());
}
实际执行的结果是:
[abc@linux:/home/abc]./1
ID PID F/S PID
---------------------------------------
father 727
son 728 727
father 727 728
[abc@linux:/home/abc]2.c 728
期望的执行结果是
[abc@linux:/home/abc]./1
ID PID P/S PID
---------------------------------------
father 727
son 728 727
2.c 728
father 727 728
[abc@linux:/home/abc]
(呵呵,实际上我想要实现的功能就是system函数,但比system函数要多一点功能,就是要知道system函数在fork时创建的子进程PID,以向子程序传递调用参数)
stop 于 2009-09-11 02:12:45发表:
谢谢!
ubuntulover 于 2009-09-10 21:48:44发表:
我试了。按我上面说的就行。
ubuntulover 于 2009-09-10 21:44:20发表:
用fork,然后父进程中调用wait等待自进程先完成就行了(我猜的,你可试试)