|     1.實驗目的     通過編寫多進程程序,使讀者熟練掌握fork()、exec()、wait()和waitpid()等函(han)數(shu)的(de)使(shi)用,進一(yi)步理(li)解在Linux中(zhong)多進程編程的(de)步驟。     2.實驗內容     該(gai)實驗有3個(ge)(ge)進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng),其中一(yi)個(ge)(ge)為父(fu)進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng),其余(yu)兩個(ge)(ge)是該(gai)父(fu)進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)創建(jian)的子進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng),其中一(yi)個(ge)(ge)子進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)運行“ls -l”指令,另一(yi)個(ge)(ge)子進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)在暫停5s后(hou)(hou)異常退出。父(fu)進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)先(xian)用(yong)阻塞方(fang)式等(deng)待第(di)一(yi)個(ge)(ge)子進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)的結束(shu),然后(hou)(hou)用(yong)非阻塞方(fang)式等(deng)待另一(yi)個(ge)(ge)子進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)的退出,待收集到第(di)二個(ge)(ge)子進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)結束(shu)的信息后(hou)(hou),父(fu)進(jin)(jin)(jin)(jin)(jin)程(cheng)(cheng)就返(fan)回。     3.實驗步驟     (1)畫出該(gai)實驗流程圖。該(gai)實驗流程圖如圖1所示。  圖1  實驗流程圖
     (2)實(shi)驗(yan)源代(dai)碼。先看一下下面的(de)代(dai)碼,這個(ge)程序(xu)能得到我(wo)們所(suo)希望的(de)結(jie)果(guo)嗎(ma)?它的(de)運行會產生幾個(ge)進程?請(qing)讀者回(hui)憶一下fork()調(diao)用的(de)具(ju)體(ti)過程。     /* multi_proc_wrong.c */#include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/wait.h>
 
 int main(void)
 {
 pid_t child1, child2, child;
 /* 創建兩個子進程 */
 child1 = fork();
 child2 = fork();
 /* 子進程1的出錯處理 */
 if (child1 == -1)
 {
 printf("Child1 fork error\n");
 exit(1);
 }
 /* 在子進程1中調用execlp()函數 */
 else if (child1 == 0)
 {
 printf("In child1: execute 'ls -l'\n");
 if (execlp("ls", "ls", "-l", NULL) < 0)
 {
 printf("Child1 execlp error\n");
 }
 }
 
 /* 子進程2的出錯處理 */
 if (child2 == -1)
 {
 printf("Child2 fork error\n");
 exit(1);
 }
 /* 在子進程2中使其暫停5s */
 else if( child2 == 0 )
 {
 printf("In child2: sleep for 5 seconds and then exit\n");
 sleep(5);
 exit(0);
 }
 /* 在父進程中等待兩個子進程的退出 */
 else
 {
 printf("In father process:\n");
 child = waitpid(child1, NULL, 0); /* 阻塞式等待 */
 if (child == child1)
 {
 printf("Get child1 exit code\n");
 }
 else
 {
 printf("Error occured!\n");
 }
 
 do
 {
 child = waitpid(child2, NULL, WNOHANG);/* 非阻塞式等待 */
 if (child == 0)
 {
 printf("The child2 process has not exited!\n");
 sleep(1);
 }
 } while (child == 0);
 
 if (child == child2)
 {
 printf("Get child2 exit code\n");
 }
 else
 {
 printf("Error occured!\n");
 }
 }
 exit(0);
 }
     編譯(yi)和(he)運行(xing)以上(shang)代(dai)碼,并觀(guan)察其運行(xing)結(jie)果(guo)。它的結(jie)果(guo)是我們(men)所希望得到(dao)的嗎?     看(kan)完(wan)前面的代碼后,再觀察下面的代碼,比較(jiao)一(yi)下它(ta)們之(zhi)間有(you)什么區(qu)別,看(kan)看(kan)會解決(jue)哪些問(wen)題(ti)。     /* multi_proc.c */#include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/wait.h>
 
 int main(void)
 {
 pid_t child1, child2, child;
 
 /* 創建兩個子進程 */
 child1 = fork();
 /* 子進程1的出錯處理 */
 if (child1 == -1)
 {
 printf("Child1 fork error\n");
 exit(1);
 }
 /* 在子進程1中調用execlp()函數 */
 else if (child1 == 0)
 {
 printf("In child1: execute 'ls -l'\n");
 if (execlp("ls", "ls", "-l", NULL) < 0)
 {
 printf("Child1 execlp error\n");
 }
 }
 /* 在父進程中再創建進程2,然后等待兩個子進程的退出 */
 else
 {
 child2 = fork();
 /* 子進程2的出錯處理 */
 if (child2 == -1)
 {
 printf("Child2 fork error\n");
 exit(1);
 }
 /* 在子進程2中使其暫停5s */
 else if(child2 == 0)
 {
 printf("In child2: sleep for 5 seconds and then exit\n");
 sleep(5);
 exit(0);
 }
 
 printf("In father process:\n");
 …(以下部分與前面程序的父進程執行部分相同)
 }
 exit(0);
 }
     (3)首先在宿主機上(shang)編譯、調試該程(cheng)序:     $ gcc multi_proc.c –o multi_proc(或(huo)者使用(yong)Makefile)     (4)在確(que)保沒(mei)有編譯(yi)錯誤后(hou),使(shi)用交叉編譯(yi)該程序:     $ arm-linux-gcc multi_proc.c –o multi_proc (或(huo)者使用Makefile)     (5)將生成的可執行程序下(xia)載到目(mu)標板上運(yun)行。     4.實驗結果     在目(mu)標板上運行的(de)結果如下(具體內(nei)容(rong)與各自的(de)系統有關):     $ ./multi_procIn child1: execute 'ls -l'        /* 子進程1的顯示, 以下是“ls –l”的運行結果 */
 total 28
 -rwxr-xr-x 1 david root  232 2008-07-18 04:18 Makefile
 -rwxr-xr-x 1 david root 8768 2008-07-20 19:51 multi_proc
 -rw-r--r-- 1 david root 1479 2008-07-20 19:51 multi_proc.c
 -rw-r--r-- 1 david root 3428 2008-07-20 19:51 multi_proc.o
 -rw-r--r-- 1 david root 1463 2008-07-20 18:55 multi_proc_wrong.c
 In child2: sleep for 5 seconds and then exit /* 子進程2的顯示 */
 In father process:						/* 以下是父進程顯示 */
 Get child1 exit code                        	/* 表示子進程1結束(阻塞等待) */
 The child2 process has not exited!			/* 等待子進程2結束(非阻塞等待) */
 The child2 process has not exited!
 The child2 process has not exited!
 The child2 process has not exited!
 The child2 process has not exited!
 Get child2 exit code						/* 表示子進(jin)程2終于(yu)結束了(le)*/
     本文選自華清遠見嵌入式培訓教材《從實踐中學嵌入式Linux應用程序開發》    熱點鏈接(jie):  
      
         1、Linux下多進程編程之exec函數語法及使用實例2、Linux下多進程編程之fork()函數語法
 3、Linux下多進程編程之fork()函數說明
 4、Linux守護進程
 5、wait()和waitpid()函數
 
 更多新聞>>  |