




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)七 編寫多進(jìn)程程序?qū)W生姓名: 李亞軍 學(xué) 號: 6100412196 專業(yè)班級: 卓越計(jì)科121班 1實(shí)驗(yàn)?zāi)康耐ㄟ^編寫多進(jìn)程程序,使讀者熟練掌握fork()、exec()、wait()和waitpid()等函數(shù)的使用,進(jìn)一步理解在Linux中多進(jìn)程編程的步驟。2實(shí)驗(yàn)內(nèi)容該實(shí)驗(yàn)有3個(gè)進(jìn)程,其中一個(gè)為父進(jìn)程,其余兩個(gè)是該父進(jìn)程創(chuàng)建的子進(jìn)程,其中一個(gè)子進(jìn)程運(yùn)行“l(fā)s -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)流程圖如圖所示。圖
2、 實(shí)驗(yàn)7.1流程圖(2)實(shí)驗(yàn)源代碼(multi_proc.c)先看一下下面的代碼,這個(gè)程序能得到我們所希望的結(jié)果嗎,它的運(yùn)行會產(chǎn)生幾個(gè)進(jìn)程?請讀者回憶一下fork()調(diào)用的具體過程。答:會產(chǎn)生四個(gè)進(jìn)程/* 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; /*創(chuàng)
3、建兩個(gè)子進(jìn)程*/ child1 = fork(); child2 = fork(); /*子進(jìn)程1的出錯(cuò)處理*/ if (child1 = -1) printf("Child1 fork errorn"); exit(1); else if (child1 = 0) /*在子進(jìn)程1中調(diào)用execlp()函數(shù)*/ printf("In child1: execute 'ls -l'n"); if (execlp("ls", "ls","-l", NULL)<0) printf
4、("Child1 execlp errorn"); if (child2 = -1) /*子進(jìn)程2的出錯(cuò)處理*/ printf("Child2 fork errorn"); exit(1); else if( child2 = 0 ) /*在子進(jìn)程2中使其暫停5s*/ printf("In child2: sleep for 5 seconds and then exitn"); sleep(5); exit(0); else /*在父進(jìn)程中等待兩個(gè)子進(jìn)程的退出*/ printf("In father process:n&q
5、uot;); child = waitpid(child1, NULL, 0); /* 阻塞式等待 */ if (child = child1) printf("Get child1 exit coden"); 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
6、(child = 0); if (child = child2) printf("Get child2 exit coden"); else printf("Error occured!n"); exit(0);編譯和運(yùn)行以上代碼,并觀察其運(yùn)行結(jié)果。它的結(jié)果是我們所希望的嗎?答:不是看完前面的代碼之后,再觀察下面的代碼,它們之間有什么區(qū)別,會解決哪些問題。答:將child2進(jìn)程的產(chǎn)生放在判斷不是child1進(jìn)程的執(zhí)行代碼中,在第一個(gè)例子中由于進(jìn)程1在執(zhí)行的時(shí)候又fork出一個(gè)進(jìn)程,導(dǎo)致產(chǎn)生了第四個(gè)進(jìn)程,這個(gè)進(jìn)程是child1的子進(jìn)程,它會將ls命令在執(zhí)行
7、一遍,而在第二個(gè)例子中由于產(chǎn)生child代碼放在判斷中,故而可以避免在child1中重復(fù)fork新進(jìn)程。/*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; /*創(chuàng)建兩個(gè)子進(jìn)程*/ child1 = fork(); /*子進(jìn)程1的出錯(cuò)處理*/ if (child1 = -1
8、) printf("Child1 fork errorn"); exit(1); else if (child1 = 0) /*在子進(jìn)程1中調(diào)用execlp()函數(shù)*/ printf("In child1: execute 'ls -l'n"); if (execlp("ls", "ls", "-l", NULL) < 0) printf("Child1 execlp errorn"); else /*在父進(jìn)程中再創(chuàng)建進(jìn)程2,然后等待兩個(gè)子進(jìn)程的退出*/
9、 child2 = fork(); if (child2 = -1) /*子進(jìn)程2的出錯(cuò)處理*/ printf("Child2 fork errorn"); exit(1); else if(child2 = 0) /*在子進(jìn)程2中使其暫停5s*/ printf("In child2: sleep for 5 seconds and then exitn"); sleep(5); exit(0); printf("In father process:n"); child = waitpid(child1, NULL, 0); /* 阻
10、塞式等待 */ if (child = child1) printf("Get child1 exit coden"); 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(&q
11、uot;Get child2 exit coden"); else printf("Error occured!n"); exit(0);(3)編譯并運(yùn)行程序例子1運(yùn)行結(jié)果:In father process:In child2: sleep for 5 seconds and then exitIn child1: execute 'ls -l'In child1: execute 'ls -l'總用量 12-rwxrwxr-x. 1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_
12、proc-rw-rw-r-. 1 chengchangfu chengchangfu 1756 5月 23 15:59 muti_proc.c總用量 12-rwxrwxr-x. 1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_proc-rw-rw-r-. 1 chengchangfu chengchangfu 1756 5月 23 15:59 muti_proc.cGet child1 exit codeThe child2 process has not exited!The child2 process has not exited!Th
13、e child2 process has not exited!The child2 process has not exited!The child2 process has not exited!Get child2 exit code例子2運(yùn)行結(jié)果:In father process:In child2: sleep for 5 seconds and then exitIn child1: execute 'ls -l'總用量 28-rwxrwxr-x. 1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_proc-rwx
14、rwxr-x. 1 chengchangfu chengchangfu 7531 5月 23 16:12 muti_proc2-rw-rw-r-. 1 chengchangfu chengchangfu 1845 5月 23 16:11 muti_proc2.c-rw-rw-r-. 1 chengchangfu chengchangfu 1843 5月 23 16:10 muti_proc2.c-rw-rw-r-. 1 chengchangfu chengchangfu 1756 5月 23 15:59 muti_proc.cGet child1 exit codeThe child2 pro
15、cess has not exited!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 code4完成實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)總結(jié):答:整個(gè)fork函數(shù)可以這樣理解:它復(fù)制當(dāng)前進(jìn)程內(nèi)所有的狀態(tài),并在其復(fù)制處的代碼開始執(zhí)行子進(jìn)程,在例子中child1產(chǎn)生的時(shí)候child2還沒有產(chǎn)生,故而在它的進(jìn)程里又重復(fù)fork了child2,從而造成錯(cuò)誤。5思考問題在目
16、標(biāo)板上運(yùn)行的結(jié)果如下所示(具體內(nèi)容與各自的系統(tǒng)有關(guān)):因?yàn)閹讉€(gè)子進(jìn)程的執(zhí)行有競爭關(guān)系,因此,結(jié)果中的順序是隨機(jī)的。讀者可以思考,怎樣才可以保證子進(jìn)程的執(zhí)行順序呢?答:使用信號同步,wait()與signal()2 編寫守護(hù)進(jìn)程1實(shí)驗(yàn)?zāi)康耐ㄟ^編寫一個(gè)完整的守護(hù)進(jìn)程,使讀者掌握守護(hù)進(jìn)程編寫和調(diào)試的方法,并且進(jìn)一步熟悉如何編寫多進(jìn)程程序。2實(shí)驗(yàn)內(nèi)容在該實(shí)驗(yàn)中,讀者首先建立起一個(gè)守護(hù)進(jìn)程,然后在該守護(hù)進(jìn)程中新建一個(gè)子進(jìn)程,該子進(jìn)程暫停10s,然后自動(dòng)退出,并由守護(hù)進(jìn)程收集子進(jìn)程退出的消息。在這里,子進(jìn)程和守護(hù)進(jìn)程的退出消息都在系統(tǒng)日志文件(例如“/var/log/messages”,日志文件的全路徑名
17、因版本的不同可能會有所不同)中輸出。子進(jìn)程退出后,守護(hù)進(jìn)程循環(huán)暫停,其間隔時(shí)間為10s。3實(shí)驗(yàn)步驟(1)畫出該實(shí)驗(yàn)流程圖。該程序流程圖如圖所示。圖 實(shí)驗(yàn)7.2流程圖(2具體代碼設(shè)置如下:/* daemon_proc.c */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>#include <syslog.h>int main(void) pid_t child1,child2
18、; int i; /*創(chuàng)建子進(jìn)程1*/ child1 = fork(); if (child1 = 1) perror("child1 fork"); exit(1); else if (child1 > 0) exit(0); /* 父進(jìn)程退出*/ /*打開日志服務(wù)*/ openlog("daemon_proc_info", LOG_PID, LOG_DAEMON); /*以下幾步是編寫守護(hù)進(jìn)程的常規(guī)步驟*/ setsid(); chdir("/"); umask(0); for(i = 0; i < getdtable
19、size(); i+) close(i); /*創(chuàng)建子進(jìn)程2*/ child2 = fork(); if (child2 = 1) perror("child2 fork"); exit(1); else if (child2 = 0) /* 進(jìn)程child2 */ /*在日志中寫入字符串*/ syslog(LOG_INFO, " child2 will sleep for 10s "); sleep(10); syslog(LOG_INFO, " child2 is going to exit! "); exit(0); else /* 進(jìn)程child1*/ waitpid(child2, NULL, 0); syslog(LOG_INFO, " child
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 05《人應(yīng)當(dāng)堅(jiān)持正義》【大單元教學(xué)】高二語文同步備課系列(統(tǒng)編版選擇性必修中冊)(課件)
- 文明守則活動(dòng)方案
- 時(shí)尚單品調(diào)研活動(dòng)方案
- 春季口才活動(dòng)方案
- 文旅公司創(chuàng)意活動(dòng)方案
- 旅游景區(qū)早操活動(dòng)方案
- 旭城花園鄰居活動(dòng)方案
- 文明禮貌班會活動(dòng)方案
- 新廠投產(chǎn)活動(dòng)方案
- 新公司拓展活動(dòng)方案
- GB/T 16451-2008天然脂肪醇
- GB 5013.2-1997額定電壓450/750V及以下橡皮絕緣電纜第2部分:試驗(yàn)方法
- 山東省中小學(xué)校檔案管理暫行辦法
- 普通高中物理課程標(biāo)準(zhǔn)
- 國家開放大學(xué)《監(jiān)督學(xué)》形考任務(wù)( 1-4)試題和答案解析
- 完工付款最終付款申請表
- 人工動(dòng)靜脈內(nèi)瘺
- 新版(七步法案例)PFMEA
- 慢阻肺隨訪記錄表正式版
- 廣西大學(xué)數(shù)學(xué)建模競賽選拔賽題目
- 受戒申請表(共3頁)
評論
0/150
提交評論