#include<stdio.h>
main()
{
int i;
while ((i=fork())==-1);
printf("i=%d\n",i);
if (i)
printf("It is a parent process!"); /
else
printf("It is a child process!");
}
编译执行输出如下:
i=0
It is a child process!
i=6980
It is a parent process!
我又修改了一下代码:
#include<stdio.h>
main()
{
int i;
while ((i=fork())==-1);
printf("i=%d\n",i);
if(i)
{
int j;
while ((j=fork())==-1);
printf("j=%d\n",j);
if(j)
printf(" a in the parent process\n"
else
printf(" b in another child process
}
else
printf(" c in a child process\n");
}
输出结果如下:
i=0
c in a child process
i=18734
j=0
b in another child process
j=18735
a in the parent process