<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

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

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

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

          本文引用地址:http://www.ex-cimer.com/article/257129.htm

          7.4內(nèi)容

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

          1.目的

          通過(guò)編寫(xiě)多進(jìn)程程序,使讀者熟練掌握f(shuō)ork()、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)畫(huà)出該實(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)程?請(qǐng)讀者回憶一下fork()調(diào)用的具體過(guò)程。

          /*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)文章專(zhuān)題:linux操作系統(tǒng)詳解(linux不再難懂)

          上一頁(yè) 1 2 3 下一頁(yè)

          評(píng)論


          相關(guān)推薦

          技術(shù)專(zhuān)區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();