中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 進(jìn)程控制開發(fā)之:實(shí)驗(yàn)內(nèi)容

        進(jìn)程控制開發(fā)之:實(shí)驗(yàn)內(nèi)容

        作者: 時(shí)間:2013-09-13 來源:網(wǎng)絡(luò) 收藏

        本文引用地址:http://www.antipu.com.cn/article/257129.htm

        7.4內(nèi)容

        7.4.1編寫多進(jìn)程程序

        1.目的

        通過編寫多進(jìn)程程序,使讀者熟練掌握fork()、exec()、wait()和waitpid()等函數(shù)的使用,進(jìn)一步理解在中多進(jìn)程編程的步驟。

        2.內(nèi)容

        該實(shí)驗(yàn)有3個(gè)進(jìn)程,其中一個(gè)為父進(jìn)程,其余兩個(gè)是該父進(jìn)程創(chuàng)建的子進(jìn)程,其中一個(gè)子進(jìn)程運(yùn)行“ls-l”指令,另一個(gè)子進(jìn)程在暫停5s之后異常退出,父進(jìn)程先用阻塞方式等待第一個(gè)子進(jìn)程的結(jié)束,然后用非阻塞方式等待另一個(gè)子進(jìn)程的退出,待收集到第二個(gè)子進(jìn)程結(jié)束的信息,父進(jìn)程就返回。

        3.實(shí)驗(yàn)步驟

        (1)畫出該實(shí)驗(yàn)流程圖。

        該實(shí)驗(yàn)流程圖如圖7.8所示。

        圖7.8實(shí)驗(yàn)7.4.1流程圖

        (2)實(shí)驗(yàn)源代碼。

        先看一下下面的代碼,這個(gè)程序能得到我們所希望的結(jié)果嗎,它的運(yùn)行會(huì)產(chǎn)生幾個(gè)進(jìn)程?請讀者回憶一下fork()調(diào)用的具體過程。

        /*multi_proc_wrong.c*/

        #includestdio.h>

        #includestdlib.h>

        #includesys/types.h>

        #includeunistd.h>

        #includesys/wait.h>

        intmain(void)

        {

        pid_tchild1,child2,child;

        /*創(chuàng)建兩個(gè)子進(jìn)程*/

        child1=fork();

        child2=fork();

        /*子進(jìn)程1的出錯(cuò)處理*/

        if(child1==-1)

        {

        printf(Child1forkerrorn);

        exit(1);

        }

        elseif(child1==0)/*在子進(jìn)程1中調(diào)用execlp()函數(shù)*/

        {

        printf(Inchild1:execute'ls-l'n);

        if(execlp(ls,ls,-l,NULL)0)

        {

        printf(Child1execlperrorn);

        }

        }

        if(child2==-1)/*子進(jìn)程2的出錯(cuò)處理*/

        {

        printf(Child2forkerrorn);

        exit(1);

        }

        elseif(child2==0)/*在子進(jìn)程2中使其暫停5s*/

        {

        printf(Inchild2:sleepfor5secondsandthenexitn);

        sleep(5);

        exit(0);

        }

        else/*在父進(jìn)程中等待兩個(gè)子進(jìn)程的退出*/

        {

        printf(Infatherprocess:n);

        child=waitpid(child1,NULL,0);/*阻塞式等待*/

        if(child==child1)

        {

        printf(Getchild1exitcoden);

        }

        else

        {

        printf(Erroroccured!n);

        }

        do

        {

        child=waitpid(child2,NULL,WNOHANG);/*非阻塞式等待*/

        if(child==0)

        {

        printf(Thechild2processhasnotexited!n);

        sleep(1);

        }

        }while(child==0);

        if(child==child2)

        {

        printf(Getchild2exitcoden);

        }

        else

        {

        printf(Erroroccured!n);

        }

        }

        exit(0);

        }

        linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

        上一頁 1 2 3 下一頁

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉