fork()

来源:互联网 发布:广州高勤 java 编辑:程序博客网 时间:2024/05/21 09:38

 http://www.chinaunix.net/jh/23/311067.html

fork的一个例子,好像人家是讲得很详细了,我还是不明白


http://www.chinaunix.net 作者:ccf  发表于:2009-05-26 17:48:54【发表评论】【查看原文】【C/C++讨论区】【关闭】
#include <unistd.h>;#include <sys/types.h>;main (){        pid_t pid;        pid=fork();        if (pid < 0)                printf("error in fork!");        else if (pid == 0)                printf("i am the child process, my process id is %d\n",getpid());        else                printf("i am the parent process, my process id is %d\n",getpid());}


结果是
[root@localhost c]# ./a.out
i am the child process, my process id is 4286
i am the parent process, my process id is 4285


我就想不到为什么两行都打印出来了,在我想来,不管pid是多少,都应该只有一行才对