




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、操作系統(tǒng)實驗三報告實驗題目:進程管理及進程通信實驗環(huán)境:虛擬機Linux操作系統(tǒng)實驗目的:1. 利用Linux提供的系統(tǒng)調用設計程序,加深對進程概念的理解。2. 體會系統(tǒng)進程調度的方法和效果。3. 了解進程之間的通信方式以及各種通信方式的使用。實驗內(nèi)容:例程1:利用fork()創(chuàng)建子進程#in clude<stdio.h> #in clude<stdlib.h>#in clude< uni std.h>main ()int i;if (forkO)i=wait(0);/*父進程執(zhí)行的程序段*/*等待子進程結束*/prin tf("It is par
2、ent p rocess.n");prin tf("The child p rocess,ID nu mber %d, is fini shed.n",i); elseprin tf("It is child pro cess.n");slee p(10);/*子進程執(zhí)行的程序段*/exit(1);/*向父進程發(fā)出結束信號*/運行結果:ubuntukyltnubuntukyltn./a.out It is child process.It is parent process.The child process,ID number 6628, i
3、s finished- ubuntukyltnubuntukyltn:-$ |思考:子進程是如何產(chǎn)生的?又是如何結束的?子進程被創(chuàng)建后它的運行環(huán)境是怎樣建立的?答:子進程是通過函數(shù)fork()創(chuàng)建的,通過exit()函數(shù)自我結束的,子進程被創(chuàng)建后核心檢查同時運行的進程數(shù)目,并且拷貝進程表項的數(shù)將為其分配一個進程表項和進程標識符, 據(jù),由子進程繼承父進程的所有文件。例程2:循環(huán)調用fork()創(chuàng)建多個子進程#in clude<stdio.h>#in clude<stdlib.h>#in clude< uni std.h>main () int i,j;'
4、;nS'pides%)pnntf( “ My pid is %d, my father,get pp id();for(i=0; i<3; i+)if(fork()=0)prints“ %d pid=%d ppid=%d , i,getpid(),getppid();else j=wait(O);Printf( “ %d:The chile %d is fin ished. ,get pid(),j);運行結果:ubuntukyItnubuntukyXin:$ /aout My pld IS 6652, ny father s ptd is 6476 0 pid=6653 ppi
5、d=66521 pid=6654 pptd=6653finished, finished.finished finished.2 ptd=6655 ppld=6654 6654:The child 6655 is 6653:The child 6654 is 2 pid=6656 ppid=6653 6653:The child 6656 is 6652:The child 6653 is1 pid=6657 ppid=6652finished. finished-finished-2 pid=6658 ppid=6657 6657:The child 6658 is 6652:The chi
6、ld 6657 is 2 pid=6659 ppid=6652 6652:The child 6659 isubuntukyllnubuntukyltn|思考:畫出進程的家族樹。子進程的運行環(huán)境是怎樣建立的?反復運行此程序 看會有什么情況?解釋一下。答:2144021445(2144?(145(247(21442(244子進程的運行環(huán)境是由將其創(chuàng)建的父進程而建立的,反復運行程序會發(fā)現(xiàn)每個進程標識號在不斷改變,這是因為同一時間有許多進程在被創(chuàng)建。例程3:創(chuàng)建子進程并用execi p()系統(tǒng)調用執(zhí)行程序的實驗#in clude<stdio.h> #in clude< uni st
7、d.h>main ()in t child_ pid1,child_pid2,child_ pid3;int p id,status;setbuf(stdout,NULL);child_pid1=fork(); /* 創(chuàng)建子進程 1*/if(child_pid1=0) execlp("echo","echo","child process 1",(char *)0); /* 子進程 1 啟動其它程序 */p error("exec1 error.' n ");exit(1);child_pid2=fo
8、rk(); /* 創(chuàng)建子進程 2*/if(child_pid2=0) execlp("date","date",(char *)0); /* 子進程 2 啟動其它程序 */p error("exec2 error.' n ”);exit(2);childpid3=fork(); /* 創(chuàng)建子進程 3*/if(child_pid3=0) execlp("ls","ls",(char *)0); /* 子進程 3 啟動其它程序 */P error("exec3 error.' n &
9、quot;);exit(3);pu ts(" Parent pro cess is wait ing for chile pro cess return!");while(pid=wait(&status)!=-1) /* 等待子進程結束 */ if(child_pid1=pid)/*若子進程 1 結束 */prin tf("child p rocess 1 term in ated with status %dn ",(status>>8); elseif(child_pid2=pid)/* 若子進程 2 結束*/prin tf(&
10、quot;child p rocess 2 term in ated with status %dn ",(status>>8); else if(child_pid3=pid) /*若子進程 3 結束 */prin tf("child p rocess 3 term in ated with status %dn" ,(status>>8);pu ts("All child p rocesses term in ated.");pu ts(" Parent pro cess term in ated.&quo
11、t;);exit(0);運行結果:ubuntukyItngubuntukyltn:«S -/a.outparent process ts watting fo child process return! child process 1child process 1 terminated with status 02013年 le月 21B 星期13:32:08 UTCchild pocess 2 terminated with status Ql.c l.G a.out Desktop公共的模板視頻圖片文檔下載音樂桌面 child process 3 terminated with
12、status 0All chlld processes terminated*Parent pro匚ess terminated. ubuntukyltnubuntukylin:$思考:子進程運行其他程序后,進程運行環(huán)境怎樣變化的?反復運行此程序看會有什么情 況?解釋一下。答:子進程運行其他程序后, 這個進程就完全被新程序代替。 由于并沒有產(chǎn)生新進程所以進 程標識號不改變,除此之外舊進程的其它信息,代碼段, 數(shù)據(jù)段,棧段等均被新程序的信息 所代替。新程序從自己的 main()函數(shù)開始運行。反復運行此程序發(fā)現(xiàn)結束的先后次序是不可預知的,每次運行結果都不一樣。 原因是當每個子進程運行其他程序時,他
13、們的結束隨著其他程序的結束而結束,所以結束的先后次序在改變。例程4:觀察父、子進程對變量處理的影響#in clude<stdio.h> #in clude<sys/t yp es.h>#in clude< uni std.h> int globa=4;int mai n() pid_t pid;int vari=5;prin tf("before fork.'n"); if (pi d=fork()<0)prin tf("fork error.'n"); exit(0);elseif(pi d=0
14、)/*子進程執(zhí)行*/ globa+;vari-;prin tf("Child %d cha nged the vari and globa.n",get pi d();else/*父進程執(zhí)行*/wait(0);printfC'Parent %d did not cha nged the vari and globa.n",get pi d(); printfC'pi d=%d, globa=%d, vari=%dn",get pi d(),globa,vari);/*都執(zhí)行*/exit(0);運行結果:ubuntukylinubuntuk
15、ylin;-$ ./a.out before fork.Child 6810 changed the vari and globa* pid=6810, globa=5, vari=4Parent 6809 did not changed the vart and globa* ptd=6809, globa=4, vari二5 ubuntukylinubuntukylin:$ |思考:子進程被創(chuàng)建后,對父進程的運行環(huán)境有影響嗎?解釋一下。答:子進程被創(chuàng)建后,對父進程的運行環(huán)境無影響,因為當子進程在運行時,它有自己的代 碼段和數(shù)據(jù)段,這些都可以作修改,但是父進程的代碼段和數(shù)據(jù)段是不會隨著子進程
16、數(shù)據(jù)段 和代碼段的改變而改變的。例程5:管道通信的實驗#in clude<stdlib.h> #in clude<stdio.h>main ()int i,r,j,k,l, p1,p 2,fd2;char buf50,s50;pip e(fd);while( p仁fork()=-1);if(p 1=0)lockf(fd1,1,0);/*子進程1執(zhí)行*/*管道寫入端加鎖*/sprin tf(buf,"Child p rocess P1 is sending messages! n");prin tf("Child process P1! n&
17、quot;);write(fd1,buf,50);lockf(fd1,0,0);/*信息寫入管道*/*管道寫入端解鎖*/slee p( 5);j=get pi d();k=get pp id();printf("P1 %d is weaku p. My pare nt process ID is %d.n",j,k); exit(O);else while( p2=fork()=-1);if(p 2=0)lockf(fd1,1,0);/*創(chuàng)建子進程2*/*子進程2執(zhí)行*/*管道寫入端加鎖*/sprin tf(buf,"Child p rocess P2 is se
18、nding messages! n");prin tf("Child process P2! n");write(fd1,buf,50);lockf(fd1,0,0);/*信息寫入管道*/*管道寫入端解鎖*/slee p( 5);j=get pi d();k=get pp id();printf("P2 %d is weaku p. My pare nt process ID is %d.n",j,k); exit(0);else l=get pi d();wait(0); if(r=read(fd0,s,50)=-1) prin tf(&qu
19、ot;Ca n't read pipe. n"); else printf("Parent %d: %s n",l,s);wait(0); if(r=read(fd0,s,50)=-1) prin tf("Ca n't read pipe. n ”); else printf("Parent %d: %s n",l,s); exit(O);運行結果:Pl 6920 IS weakup. Parent 6919: ChildMy parent process ID is 6919. process P2 is sendi
20、ng messages!My parent process ID is 6919 process Pl is sending messages!ubtjntuky1Anubuntukylin:$ |思考:(1)什么是管道?進程如何利用它進行通信的?解釋一下實現(xiàn)方法。(2)修改睡眠時機、睡眠長度,看看會有什么變化。解釋。(3)加鎖、解鎖起什么作用?不用它行嗎?答:(1)管道是指能夠連接一個寫進程和一個讀進程、并允許他們以生產(chǎn)者一消費者方式進行通信的一個共享文件,又稱pipe文件。由寫進程從管道的入端將數(shù)據(jù)寫入管道,而讀進程則從管道的出端讀出數(shù)據(jù)來進行通信。(2)修改睡眠時機和睡眠長度都會引起進程
21、被喚醒的時間不一,因為睡眠時機決定進程 在何時睡眠,睡眠長度決定進程何時被喚醒。(3) 加鎖、解鎖是為了解決臨界資源的共享問題。不用它將會引起無法有效的管理數(shù)據(jù), 即數(shù)據(jù)會被修改導致讀錯了數(shù)據(jù)。例程7:軟中斷信號實驗#in clude<stdlib.h> #in clude<stdio.h>main ()int i,j,k;int fun c();sig nal(18,fu nc();if(i=fork()j=kill(i,18);/*創(chuàng)建子進程*/*父進程執(zhí)行*/*向子進程發(fā)送信號*/printfC'Parent: sig nal 18 has bee n s
22、ent to child %d,retur ned %d.n",i,j); k=wait();/*父進程被喚醒*/prin tf("After wait %d, Pare nt %d: fin ished.n",k,get pi d();else/*子進程執(zhí)行*/slee p(10);prin tf("Child %d: A sig nal from my parent is recived.n",get pi d(); /*子進程結束,向父進程發(fā)子進程結束信號*/func()/*處理程序*/ int m;m=get pi d();prin t
23、f("I am Process %d: It is sig nal 18 p rocess ing function.'n ",m); 運行結果:ubuntukyltnubuntukylin:-$ ./a.outI an Pocess 6970: It is signal 18 processing function. Parent: signal 18 has been sent to child 6971,returned 0. After wait -1,Parent 6973: finished, ubuntukyltnubuntukylin:$ 思考:討論
24、一下它與硬中斷有什么區(qū)別?答:硬中斷是由外部硬件產(chǎn)生的,而軟中斷是 存器的某個標志位的設置而產(chǎn)生的。CPU根據(jù)軟件的某條指令或者軟件對標志寄研究:什么是進程?如何產(chǎn)生的?答:進程是進程實體的運行過程,是系統(tǒng)進行資源分配和調度的一個獨立單位。一旦操作系統(tǒng)發(fā)現(xiàn)了要求創(chuàng)建新進程的事件后,便調用進程創(chuàng)建原語 CreatO按下述步驟創(chuàng)建一個新進程:(1 )申請空白PCB為新進程申請獲得惟一的數(shù)字標識符,并從PCB集合中索取一個空白PCB(2)為新進程分配資源。為新進程的程序和數(shù)據(jù)以及用戶棧分配必要的內(nèi)存空間。顯然此時操作系統(tǒng)必須知道新進程所需內(nèi)存的大小。對于批處理作業(yè),其大小可在用戶提出創(chuàng)建進程要求時提供。若是為應用進程創(chuàng)建子進程,也應是在該進程提出創(chuàng)建進程的請求中給出所 需內(nèi)存的大小。對于交互型作業(yè),用戶可以不給出內(nèi)存要求而由系統(tǒng)分配一定的空間。如果新進程要共享某個已在內(nèi)在的地址空間(即已裝入內(nèi)存的共享段),則必須建立相應的鏈接。(3) 初始化進程控件塊。 PCB的初始化包括:1.初始化標識信息,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 生態(tài)保護與書畫藝術創(chuàng)作考核試卷
- 藝術品市場規(guī)范考核試卷
- 航班機組人員溝通技巧考核試卷
- 花卉畫法的分類與特點考核試卷
- 一次函數(shù)應用舉例教學課件
- 共建文明社區(qū)共享和諧生活:課件教程
- 中國古代教育長善救失
- 2019-2025年咨詢工程師之工程項目組織與管理能力提升試卷B卷附答案
- 2025年投資項目管理師之投資建設項目決策真題練習試卷A卷附答案
- 扈中平現(xiàn)代教育改革理論與實踐
- 創(chuàng)造心智與創(chuàng)新訓練智慧樹知到期末考試答案2024年
- 創(chuàng)傷性前房積血
- 供水企業(yè)安全生產(chǎn)培訓課件
- 2024年《大學語文》期末考試復習題庫(含答案)
- 早產(chǎn)的護理查房課件
- 國家智慧教育平臺培訓課件
- 針灸科出科個人小結
- 語感與語言習得-【中職專用】高一語文同步課件(高教版2023·基礎模塊上冊)
- 2024年中國石化集團資本有限公司招聘筆試參考題庫含答案解析
- 普通高中地理課程標準(2023年版)
- 檢驗批劃分方案14
評論
0/150
提交評論