fork()

 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是多少,都應該只有一行纔對

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章