• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
            在編寫(xiě)socket ftp之前,我對(duì)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()); 



            }
             

            這段代碼寫(xiě)了一個(gè)使用fork函數(shù)創(chuàng)建子進(jìn)程,父子進(jìn)程同時(shí)運(yùn)行而產(chǎn)生交錯(cuò)的,不一樣的運(yùn)行結(jié)果。
            運(yùn)行結(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)程同時(shí)(其實(shí)是cpu分時(shí)處理)開(kāi)始運(yùn)行分叉之后的程序。
                  我把程序改寫(xiě)了一下: 

             

            #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());

                    }

            }


            程序運(yùn)行結(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)程號(hào),創(chuàng)建fork以后,fork函數(shù)返回給父進(jìn)程的值pid是子進(jìn)程的進(jìn)程號(hào)[3820],而在子進(jìn)程中,pid值為零。也就是說(shuō)子進(jìn)程中,pid被置零。

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

            下面有一個(gè)很有意思的程序:
            #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)幾個(gè)son 幾個(gè)father呢?








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

            總共7個(gè)son7個(gè)father。你答對(duì)了么?

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

            Feedback

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

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

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

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

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

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

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

            2012-01-06 17:21 by er
            gcc怎么寫(xiě)

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

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

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

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

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

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

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


            国产精品久久影院| 久久免费国产精品| 漂亮人妻被中出中文字幕久久| 久久久久久综合一区中文字幕 | 久久人妻少妇嫩草AV无码蜜桃| 国产精品无码久久综合| 亚洲国产精品无码久久一线| 午夜精品久久久久| 久久久久久午夜精品| 久久精品国产只有精品66 | 亚洲国产精久久久久久久| 天堂久久天堂AV色综合| 人妻无码久久一区二区三区免费 | 国产成人精品久久综合| 久久这里只有精品久久| 91视频国产91久久久| 狠狠色丁香婷婷综合久久来| 久久久久久狠狠丁香| 国产高潮国产高潮久久久91 | 国产69精品久久久久9999APGF| 国产精品久久久久久五月尺| 久久精品久久久久观看99水蜜桃| 国产亚洲精品久久久久秋霞| 婷婷伊人久久大香线蕉AV| 久久AV高清无码| 久久99精品国产麻豆| 日本免费一区二区久久人人澡| 91精品久久久久久无码| 色综合久久中文字幕综合网| 最新久久免费视频| 久久久久久久人妻无码中文字幕爆| 俺来也俺去啦久久综合网| 国产成人综合久久综合| 久久精品国产亚洲一区二区三区| 波多野结衣久久精品| …久久精品99久久香蕉国产| 国内精品久久久久久久影视麻豆 | 国产午夜电影久久| 欧美黑人激情性久久| 青青草国产精品久久| 亚洲国产精品嫩草影院久久|