- 论坛徽章:
- 0
|
fork的一个例子,好像人家是讲得很详细了,我还是不明白
#include <unistd.h>;
#include <sys/types.h>;
main ()
{ int i=5;
pid_t pid;
pid=fork();
for(;i>;0;i--){
if (pid < 0)
printf("error in fork!"
else if (pid == 0)
printf("i am the child process, my process id is %d and i=%d\n",getpid(),i);
else
printf("i am the parent process, my process id is %d and i=%d\n",getpid(),i);
}
}
i am the child process, my process id is 11879 and i=5
i am the child process, my process id is 11879 and i=4
i am the child process, my process id is 11879 and i=3
i am the child process, my process id is 11879 and i=2
i am the child process, my process id is 11879 and i=1
i am the parent process, my process id is 11878 and i=5
i am the parent process, my process id is 11878 and i=4
i am the parent process, my process id is 11878 and i=3
i am the parent process, my process id is 11878 and i=2
i am the parent process, my process id is 11878 and i=1
我觉得这样改写一下就更好理解了 |
|