- 论坛徽章:
- 0
|
进程
不知道你在什么环境下测的,我在solaris上测了,按照你上面的写法是可以的。源程序:
#include<stdio.h>;
#include<unistd.h>;
#include<stdlib.h>;
#include<time.h>;
main()
{
pid_t child_1,child_2,child_3;
time_t t_time1,t_time2;
char file_file[64];
int ii;
time(&t_time1);
if((child_1=fork())==-1)
{
printf("FORK ERROR:%d\n",child_1);
exit(0);
}
else if(child_1==0){
/*操作*/
printf("child_1,first\n" ;
sleep(1);
printf("child_1,second\n" ;
sleep(3);
printf("child_1 third\n" ;
exit(1);
}
else {
if((child_2=fork())==-1)
{
printf("FORK ERROR:%d\n",child_2);
exit(0);
}
else if(child_2==0){
/*操作*/
printf("child_2 first\n" ;
sleep(2);
printf("child_2 second\n" ;
sleep(1);
printf("child_2 third\n" ;
exit(2);
}
else {
/*操作*/
printf("parent\n" ;
sleep(6);
}
}
}
第一次输出为:
child_1,first
child_2 first
parent
child_1,second
child_2 second
child_2 third
child_1 third
第二次输出为:
parent
child_1,first
child_2 first
child_1,second
child_2 second
child_2 third
child_1 third
由多次结果看出,子进程1和子进程2是同时的。 |
|