- 论坛徽章:
- 0
|
为什么两个结果差异这样大呢?哪位高手给个指点嘛?
源程序MyFork.c
#include "test.h"
main(void)
{
pid_t pid;
/*static*/ int iCount = 0;
printf("Before fork: pid=%d \n", getpid());
/* Fork MAXP precesses */
for (iCount=0; iCount<MAXP; iCount++)
{
pid = fork();
/* Something wrong when fork a precess */
if (pid < 0)
{
printf("ERROR\n";
}
else if (0 == pid) /* Child precess */
{
printf("Child: \tiCount=%d, pid=%d, \tParent: pid=%d\n",
iCount, getpid(), getppid());
/* break;
*/
}
else /* Parent precess */
{
sleep(1);
wait();
printf("arent: \tiCount=%d, pid=%d, \tParent: pid=%d\n",
iCount, getpid(), getppid());
/* break;
*/
}
}
printf("The end of precess: pid=%d \n\n", getpid());
}
头文件test.h
#define MAXP = 3
#include <sys/types.h>
#include <unistd.h>
编译该程序:cc -s MyFork.c -o fork_test
运行fork_test,输出如下:
Before fork: pid=7402
Child: iCount=0, pid=7403, Parent: pid=7402
Child: iCount=1, pid=7404, Parent: pid=7403
Child: iCount=2, pid=7405, Parent: pid=7404
Child: iCount=3, pid=7406, Parent: pid=7405
The end of precess: pid=7406
Parent: iCount=3, pid=7405, Parent: pid=7404
The end of precess: pid=7405
Parent: iCount=2, pid=7404, Parent: pid=7403
Child: iCount=3, pid=7407, Parent: pid=7404
The end of precess: pid=7407
Parent: iCount=3, pid=7404, Parent: pid=7403
The end of precess: pid=7404
Parent: iCount=1, pid=7403, Parent: pid=7402
Child: iCount=2, pid=7408, Parent: pid=7403
Child: iCount=3, pid=7409, Parent: pid=7408
The end of precess: pid=7409
Parent: iCount=3, pid=7408, Parent: pid=7403
The end of precess: pid=7408
Parent: iCount=2, pid=7403, Parent: pid=7402
Child: iCount=3, pid=7411, Parent: pid=7403
The end of precess: pid=7411
Parent: iCount=3, pid=7403, Parent: pid=7402
The end of precess: pid=7403
Parent: iCount=0, pid=7402, Parent: pid=6214
Child: iCount=1, pid=7412, Parent: pid=7402
Child: iCount=2, pid=7413, Parent: pid=7412
Child: iCount=3, pid=7414, Parent: pid=7413
The end of precess: pid=7414
Parent: iCount=3, pid=7413, Parent: pid=7412
The end of precess: pid=7413
Parent: iCount=2, pid=7412, Parent: pid=7402
Child: iCount=3, pid=7415, Parent: pid=7412
The end of precess: pid=7415
Parent: iCount=3, pid=7412, Parent: pid=7402
The end of precess: pid=7412
Parent: iCount=1, pid=7402, Parent: pid=6214
Child: iCount=2, pid=7417, Parent: pid=7402
Child: iCount=3, pid=7418, Parent: pid=7417
The end of precess: pid=7418
Parent: iCount=3, pid=7417, Parent: pid=7402
The end of precess: pid=7417
Parent: iCount=2, pid=7402, Parent: pid=6214
Child: iCount=3, pid=7422, Parent: pid=7402
The end of precess: pid=7422
Parent: iCount=3, pid=7402, Parent: pid=6214
The end of precess: pid=7402
运行fork_test>result.log
vi result.log,显示如下:
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Child: iCount=1, pid=6866, Parent: pid=6865
Child: iCount=2, pid=6867, Parent: pid=6866
Child: iCount=3, pid=6868, Parent: pid=6867
The end of precess: pid=6868
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Child: iCount=1, pid=6866, Parent: pid=6865
Child: iCount=2, pid=6867, Parent: pid=6866
Parent: iCount=3, pid=6867, Parent: pid=6866
The end of precess: pid=6867
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Child: iCount=1, pid=6866, Parent: pid=6865
Parent: iCount=2, pid=6866, Parent: pid=6865
Child: iCount=3, pid=6869, Parent: pid=6866
The end of precess: pid=6869
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Child: iCount=1, pid=6866, Parent: pid=6865
Parent: iCount=2, pid=6866, Parent: pid=6865
Parent: iCount=3, pid=6866, Parent: pid=6865
The end of precess: pid=6866
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Parent: iCount=1, pid=6865, Parent: pid=6864
Child: iCount=2, pid=6870, Parent: pid=6865
Child: iCount=3, pid=6871, Parent: pid=6870
The end of precess: pid=6871
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Parent: iCount=1, pid=6865, Parent: pid=6864
Child: iCount=2, pid=6870, Parent: pid=6865
Parent: iCount=3, pid=6870, Parent: pid=6865
The end of precess: pid=6870
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Parent: iCount=1, pid=6865, Parent: pid=6864
Parent: iCount=2, pid=6865, Parent: pid=6864
Child: iCount=3, pid=6872, Parent: pid=6865
The end of precess: pid=6872
Before fork: pid=6864
Child: iCount=0, pid=6865, Parent: pid=6864
Parent: iCount=1, pid=6865, Parent: pid=6864
Parent: iCount=2, pid=6865, Parent: pid=6864
Parent: iCount=3, pid=6865, Parent: pid=6864
The end of precess: pid=6865
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Child: iCount=1, pid=6873, Parent: pid=6864
Child: iCount=2, pid=6874, Parent: pid=6873
Child: iCount=3, pid=6875, Parent: pid=6874
The end of precess: pid=6875
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Child: iCount=1, pid=6873, Parent: pid=6864
Child: iCount=2, pid=6874, Parent: pid=6873
Parent: iCount=3, pid=6874, Parent: pid=6873
The end of precess: pid=6874
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Child: iCount=1, pid=6873, Parent: pid=6864
Parent: iCount=2, pid=6873, Parent: pid=6864
Child: iCount=3, pid=6876, Parent: pid=6873
The end of precess: pid=6876
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Child: iCount=1, pid=6873, Parent: pid=6864
Parent: iCount=2, pid=6873, Parent: pid=6864
Parent: iCount=3, pid=6873, Parent: pid=6864
The end of precess: pid=6873
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Parent: iCount=1, pid=6864, Parent: pid=6214
Child: iCount=2, pid=6877, Parent: pid=6864
Child: iCount=3, pid=6878, Parent: pid=6877
The end of precess: pid=6878
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Parent: iCount=1, pid=6864, Parent: pid=6214
Child: iCount=2, pid=6877, Parent: pid=6864
Parent: iCount=3, pid=6877, Parent: pid=6864
The end of precess: pid=6877
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Parent: iCount=1, pid=6864, Parent: pid=6214
Parent: iCount=2, pid=6864, Parent: pid=6214
Child: iCount=3, pid=6879, Parent: pid=6864
The end of precess: pid=6879
Before fork: pid=6864
Parent: iCount=0, pid=6864, Parent: pid=6214
Parent: iCount=1, pid=6864, Parent: pid=6214
Parent: iCount=2, pid=6864, Parent: pid=6214
Parent: iCount=3, pid=6864, Parent: pid=6214
The end of precess: pid=6864 |
|