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

          新聞中心

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

          進程控制開發(fā)之:實驗內(nèi)容

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

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

          編譯和運行以上代碼,并觀察其運行結(jié)果。它的結(jié)果是我們所希望的嗎?

          看完前面的代碼之后,再觀察下面的代碼,它們之間有什么區(qū)別,會解決哪些問題。

          /*multi_proc.c*/

          #includestdio.h>

          #includestdlib.h>

          #includesys/types.h>

          #includeunistd.h>

          #includesys/wait.h>

          intmain(void)

          {

          pid_tchild1,child2,child;

          /*創(chuàng)建兩個子進程*/

          child1=fork();

          /*子進程1的出錯處理*/

          if(child1==-1)

          {

          printf(Child1forkerrorn);

          exit(1);

          }

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

          {

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

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

          {

          printf(Child1execlperrorn);

          }

          }

          else/*在父進程中再創(chuàng)建進程2,然后等待兩個子進程的退出*/

          {

          child2=fork();

          if(child2==-1)/*子進程2的出錯處理*/

          {

          printf(Child2forkerrorn);

          exit(1);

          }

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

          {

          printf(Inchild2:sleepfor5secondsandthenexitn);

          sleep(5);

          exit(0);

          }

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

          }

          (3)首先在宿主機上編譯調(diào)試該程序:

          $gccmulti_proc.c–omulti_proc(或者使用Makefile)

          (4)在確保沒有編譯錯誤后,使用交叉編譯該程序:

          $arm-linux-gccmulti_proc.c–omulti_proc(或者使用Makefile)

          (5)將生成的可執(zhí)行程序下載到目標板上運行。

          4.結(jié)果

          在目標板上運行的結(jié)果如下所示(具體內(nèi)容與各自的系統(tǒng)有關(guān)):

          $./multi_proc

          Inchild1:execute'ls-l'/*子進程1的顯示,以下是“ls–l”的運行結(jié)果*/

          total28

          -rwxr-xr-x1davidroot2322008-07-1804:18Makefile

          -rwxr-xr-x1davidroot87682008-07-2019:51multi_proc

          -rw-r--r--1davidroot14792008-07-2019:51multi_proc.c

          -rw-r--r--1davidroot34282008-07-2019:51multi_proc.o

          -rw-r--r--1davidroot14632008-07-2018:55multi_proc_wrong.c

          Inchild2:sleepfor5secondsandthenexit/*子進程2的顯示*/

          Infatherprocess:/*以下是父進程顯示*/

          Getchild1exitcode/*表示子進程1結(jié)束(阻塞等待)*/

          Thechild2processhasnotexited!/*等待子進程2結(jié)束(非阻塞等待)*/

          Thechild2processhasnotexited!

          Thechild2processhasnotexited!

          Thechild2processhasnotexited!

          Thechild2processhasnotexited!

          Getchild2exitcode/*表示子進程2終于結(jié)束了*/

          因為幾個子進程的執(zhí)行有競爭關(guān)系,因此,結(jié)果中的順序是隨機的。讀者可以思考,怎樣才可以保證子進程的執(zhí)行順序呢?

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


          評論


          相關(guān)推薦

          技術(shù)專區(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); })();