• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            旭++

            張旭的C++學(xué)習(xí)筆記
            posts - 5, comments - 8, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            linux fork函數(shù)學(xué)習(xí)

            Posted on 2007-12-02 15:20 張旭 閱讀(11617) 評論(7)  編輯 收藏 引用
            在編寫socket ftp之前,我對fork函數(shù)進(jìn)行了學(xué)習(xí)。
            先看這段范例代碼:
            #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 %dn",getpid()); 


                    
            else 


                            printf(
            "i am the parent process, my process id is %dn",getpid()); 



            }
             

            這段代碼寫了一個使用fork函數(shù)創(chuàng)建子進(jìn)程,父子進(jìn)程同時運行而產(chǎn)生交錯的,不一樣的運行結(jié)果。
            運行結(jié)果如下:
            [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 
                  fork在英文中是叉子,分叉的意思,在函數(shù)fork中,取后面的意思。很形象的表示程序從這里分叉,fork函數(shù)創(chuàng)建了子進(jìn)程,子進(jìn)程和父進(jìn)程同時(其實是cpu分時處理)開始運行分叉之后的程序。
                  我把程序改寫了一下: 

             

            #include <unistd.h>
            #include 
            <sys/types.h>
            main()
            {
                    pid_t pid;
                    printf(
            "\n[%d]not fork pid=%d\n",getpid(),pid);
                    pid
            =fork();
                    printf(
            "\n[%d]forked pid=%d\n",getpid(),pid);
                    
            if(pid<0)
                    
            {
                            printf(
            "error in fork!\n");
                            getchar();
                            exit(
            1);
                    }

                    
            else if(pid==0)
                            printf(
            "\n[%d]in child process,p_id=%d\n",getpid(),getpid());
                    
            else
                    
            {
                            printf(
            "\n[%d]in parent process,my pid=%d\n",getpid(),pid);
                            printf(
            "\n[%d]in parent process,my getpid=%d\n",getpid(),getpid());

                    }

            }


            程序運行結(jié)果如下:
            [hardy@localhost fork]$ ./fork

            [3819]not fork

            [3820]forked pid=0

            [3820]in child process,p_id=3820

            [3819]forked pid=3820

            [3819]in parent process,my pid=3820

            [3819]in parent process,my getpid=3819

            可以清楚的看到 not fork只打印了一次,其中[3819]是父進(jìn)程的進(jìn)程號,創(chuàng)建fork以后,fork函數(shù)返回給父進(jìn)程的值pid是子進(jìn)程的進(jìn)程號[3820],而在子進(jìn)程中,pid值為零。也就是說子進(jìn)程中,pid被置零。

            引用網(wǎng)上一位網(wǎng)友的解釋“其實就相當(dāng)于鏈表,進(jìn)程形成了鏈表,父進(jìn)程pid(p 意味point)指向子進(jìn)程的進(jìn)程id, 因為子進(jìn)程沒有子進(jìn)程,所以其pid為0. 

            下面有一個很有意思的程序:
            #include <sys/types.h>
            #include 
            <unistd.h>

            int main()
            {
                    
            int i;
                    
            for( i= 0; i< 3; i++)
                    
            {
                            
            int pid= fork();
                            
            if(pid== 0)
                            
            {
                                    printf(
            "son\n");
                            }

                            
            else
                            
            {
                                    printf(
            "father\n");
                            }

                    }

                    
            return 0;
            }


            大家想想看最后將出現(xiàn)幾個son 幾個father呢?








            對一下答案吧:
            [hardy@localhost fork]$ ./fork
            father
            son
            son
            son
            father
            father
            son
            father
            son
            son
            father
            father
            son
            father

            總共7個son7個father。你答對了么?

            這道題需要在紙上畫畫才好理解
            for            i=0         1           2
                           father     father     father
                                                       son
                                          son       father
                                                        son
                           son       father        father
                                                         son
                                          son         father
                                                         son
            其中每一行分別代表一個進(jìn)程的運行打印結(jié)果。
            當(dāng)產(chǎn)生子進(jìn)程的時刻,子進(jìn)程打印son,當(dāng)子進(jìn)程調(diào)用fork的生成子子進(jìn)程,他就提升為father。
            總結(jié)來說,father永遠(yuǎn)打印father,son在fork之前是son,fork之后就為father,同時生成新的son。
            這個比喻就像真正的父子,孩子長大了生了小孩,孩子就成了父親。而父親永遠(yuǎn)是父親。

            Feedback

            # re: linux fork函數(shù)學(xué)習(xí)  回復(fù)  更多評論   

            2008-08-06 12:55 by zcc
            xhu,2舍的張旭?不會真的是你吧?呵呵,我搜索fork()的時候搜到了這里,如果真你是你該知道我是哪個了吧,呵呵,我是zcc

            # re: linux fork函數(shù)學(xué)習(xí)  回復(fù)  更多評論   

            2008-08-06 13:00 by 張旭
            @zcc
            抱歉,我想不起來你是誰。可能認(rèn)錯了吧。

            # re: linux fork函數(shù)學(xué)習(xí)  回復(fù)  更多評論   

            2009-02-21 18:39 by
            很強(qiáng)大。。。

            # re: linux fork函數(shù)學(xué)習(xí)  回復(fù)  更多評論   

            2012-01-06 17:21 by er
            gcc怎么寫

            # re: linux fork函數(shù)學(xué)習(xí)[未登錄]  回復(fù)  更多評論   

            2012-04-10 20:56 by noname
            good!

            # re: linux fork函數(shù)學(xué)習(xí)[未登錄]  回復(fù)  更多評論   

            2012-04-10 20:58 by noname
            請問最后一個程序,是不是father和son的輸出沒有規(guī)律,第一組是father和son的組合,第二組是兩個father和兩個son的組合,最后一組是四個father和四個son的組合就是了嗎?

            # re: linux fork函數(shù)學(xué)習(xí)  回復(fù)  更多評論   

            2012-08-23 18:55 by 過客
            你知道fork后系統(tǒng)是怎么分清那個該子進(jìn)程運行哪些是父進(jìn)程運行嗎?

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            色诱久久av| 久久99精品综合国产首页| 少妇人妻88久久中文字幕| 伊人久久大香线蕉亚洲| 久久亚洲春色中文字幕久久久 | 99久久夜色精品国产网站| 久久亚洲中文字幕精品一区| 精品久久亚洲中文无码| 久久精品一区二区国产| 国产精品热久久毛片| 久久精品无码一区二区WWW| 97久久精品午夜一区二区| 国产香蕉久久精品综合网| 成人久久久观看免费毛片| 久久免费香蕉视频| 久久99热只有频精品8| 无码精品久久久久久人妻中字| 久久久久人妻精品一区二区三区| 久久亚洲AV无码精品色午夜| 91久久精品91久久性色| 精品伊人久久久| 久久久久久久综合综合狠狠| 久久97久久97精品免视看| 丁香五月综合久久激情| 久久精品成人欧美大片| 人妻少妇精品久久| 麻豆久久| 久久久免费观成人影院| 色综合久久久久网| 久久精品国产99国产精品澳门 | 性欧美丰满熟妇XXXX性久久久 | 2021久久国自产拍精品| 亚洲综合精品香蕉久久网| 日产久久强奸免费的看| 欧美午夜A∨大片久久| 久久精品国产欧美日韩| 久久播电影网| 亚洲欧美国产精品专区久久| 99久久精品国产一区二区| 中文精品99久久国产| 亚洲国产精品嫩草影院久久 |