全部博文(133)
分类: LINUX
2011-07-08 15:36:05
2 #include
3 void main(void)
4 {
5 pid_t pid;
6
7 pid=fork();
8
9 if(pid < 0)
10 {
11 printf("error in fork!\n");
12 }
13 else if(pid == 0)
14 {
15 printf("in child return pid value is = %d\n",(int)pid);
16 printf("i am the child process,my process id is %d\n",getpid());
17 printf("i am the child process,my parent id is %d\n",getppid());
18 }
19 else
20 {
21 printf("in father return pid value is = %d\n",(int)pid);
22 printf("i am the parent process,my process id is %d\n",getpid());
23 printf("i am the parent process,my parent id is %d\n",getppid());
24 }
25 }
结果:
1,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1293
i am the parent process,my process id is 1292
i am the parent process,my parent id is 1229
root@darkstar:/home/zhangl/unixtest/chapter8# in child return pid value is = 0
i am the child process,my process id is 1293
i am the child process,my parent id is 1
2,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1328
i am the parent process,my process id is 1327
i am the parent process,my parent id is 1301
in child return pid value is = 0
i am the child process,my process id is 1328
i am the child process,my parent id is 1
3,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1373
in child return pid value is = 0
i am the child process,my process id is 1373
i am the child process,my parent id is 1372
i am the parent process,my process id is 1372
i am the parent process,my parent id is 1354