3120150901209夏霜3進程控制_第1頁
3120150901209夏霜3進程控制_第2頁
3120150901209夏霜3進程控制_第3頁
3120150901209夏霜3進程控制_第4頁
3120150901209夏霜3進程控制_第5頁
已閱讀5頁,還剩4頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、捧4計算機與軟件工程學院實驗報告(2017/ 2018學年第1 學期)課程名稱操作系統實驗課程代碼150901189實驗時間2017.11.27指導單位軟件工程系指導教師譚平學生姓名夏霜年級2015 級學 號3120150901209專業計算機科學與技術實驗名稱進程控制實驗地點8321實驗類型驗證實驗學時4一、實驗目的和要求1. 加深對進程概念的理解,進一步認識并發執行的實質2. 掌握Linux操作系統的進程創建和終止操作3. 利用Linux操作系統提供的信號量工具實現進程間的同步4. 掌握對共享內存的相關操作二、實驗環境(實驗設備)硬件:微型計算機軟件:Linux操作系統二、實驗內容及過程1

2、.編寫一段程序,實現進程的軟中斷通信。要求:使用系統調用fork()創建兩個子進程,再用系統調用signal()讓父進程捕捉鍵盤上 來的中斷信號(即按 DEL鍵);當捕捉到中斷信號后,父進程用系統調用Kill()向兩個子進程發出信號,子進程捕捉到信號后分別輸出下列信息后終止:Child Processll is Killed by Pare nt!Child Processl2 is Killed by Pare nt!父進程等待兩個子進程終止后,輸出如下的信息后終止Pare nt Process is Killed! 在上面的程序中增加語句signal (SIGNAL, SIG-IGN)和

3、signal (SIGQUIT, SIG-IGN),觀察執行結果,并分析原因。解答: 源代碼#in clude#in cludevsig nal.h#in cludev uni std.hvoid wait in g(), stop(), alarmi ng();int wait_mark;void mai n()in t p1, p2 ;if(p1=fork()if(p2=fork() wait mark=1 ;sig nal(SIGINT, stop);sig nal(SIGALRM, alarmi ng); wait in g();kill(p1, 16);kill(p2, 17);wa

4、it(0);wait(0);prin tf(pare nt process is killed! n); exit(0);elsewait_mark=1;sig nal(17, stop);sig nal(SIGINT,SIGGN); while(wait_mark!=O);lockf(1, 1,0);prin tf(childre n process2 is killed by pare ntn); lockf(1,0, 0);exit(0);elsewait_mark=1;sig nal(16, stop);sig nal(SIGINT,SIG_I GN);while(wait_mark!

5、=0);lockf(1, 1,0);prin tf(childre n process1 is killed by pare ntn);lockf(1, 0, 0);exit(0);void wait in g()sleep(5);if(wait_mark!=0)kill(getpid(),SIGALRM);void alarmi ng()wait_mark=O;void stop()wait_mark=O;相關操作界面和運行結果界面截圖不加入 signal (SIGINT, SIG-IGN) 和signal (SIGQUIT, SIG-IGN) 運行結果host 1 f gedtt sign

6、altest.c d gcc signaltest.c -o stgnaltestsignaltestIn functton *natn1:signaltest.c;19:7;Lnplicit declaration of function rwalt* -Winpliclt-functlan-declarationwatt(e);Asign al testc:22:7:-inpli cit declaration of function SxVt WinpHcitfjdction declarationslgnaltKtc:22:7s onstjnattest,c:22:7: slfri a

7、ltest c;34:7; on *exLt*extt(e);signal test.c:34:7: signalte(t,c:47:5: onexlt(c);extt(O);“yrg: incoRpatible inplicit declaration of built*tn functinot: intlude f*5tdlib*h* h or provide a decla ration of 1 exit * c r - inconpttble implicit declaration of built - tn functinat- include1 or providp a dec

8、laration of 1 exitJ-incompatible inpltcit declaration of built-in functixslccathost 擊 + /signa Ltestcnnoren processl i.s killed by parert children process2 ts killed by parent parent process ts killed!加入 signal (SIGINT, SIG-IGN) 相關操作界面和運行結果界面截圖| xs*5ocalho$t I stgnaltestl.c *t stgrialtestl stgnaltes

9、tlc: In function * main *:slgnaltestl;19: ?; jr n in -j; inpLicit dEclaration of function 1 wait1 -Wlnpliclt -tunctton-decLarattonwatt(6):Aisignaltestlc:22;7: arn lh : impticit declaration of function exit1 -Uinplicit -ru nc t ton -decla ra ttostdltb.hH or protle a declaration or pexttisignaltestl.c

10、:547: rnlng: incoipatible tnplicit declaration of built-in funct ion *erlt*exit(e);signdltst241;34;7;tnclude stdlib*hd or provide a declaration of dexitj xslocalhost TI ./tgraltet2ACparent processts killed!原因:signal(SIGINT,SIG_IGN)是忽略鍵入信號,signal (SIGQUIT, SIG-IGN)是忽略中斷 信號。在子進程中加入 sig nal(SIGINT,SIG_

11、IGN),子進程會忽略中斷信號,在子進程中加 入sig nal (SIGQUIT, SIG-IGN),相應的中斷信號就被忽略,只有父進程被中斷2.求100000個浮點數(精確小數點右4位)的平均值(和、最大值、最小值)。要求: 隨機生成100000個浮點數(父進程); 創建4個子進程,分別求 25000個浮點數之和; 父進程完成100000個浮點數之和并打印結果; 統計順序計算的時間和多個進程采用多道程序設計完成計算的時間。 解答:源代碼#in elude #in elude #in clude #in clude #in clude vuni std.hstatic double allsu

12、m=0.0;int mai n() int i,j;double a425000;double sum4=0.0;double readBuf4; time_t start,e nd;pid_t pid;struct timeval js;int fp42;sra nd(u nsig ned)time(NULL); for(i=0;i4;i+)for(j=0;j25000;j+) aij=(double)ra nd()/(double)RAND_MAX;gettimeofday(&js,NULL);start=js.tv_usec;for(i=0;i4;i+)if(pipe(fpi)0)pri

13、n tf(pipe error); exit(1); pid=fork();if(pid0)pri ntf(fork error!); exit(1);else if(pid=0)for(j=0;j25000;j+)sumi+=aij;write(fpi1, &sumi,sizeof(double);/ prin tf(sum%d=%fn,i,sumi); exit(1);elseif(!read(fpiO,&readBufi,sizeof(double) exit(1);prin tf(sum%d=%fn,i,readBufi);for(i=0;i4;i+) allsum+=readBufi

14、;prin tf(sum=%fn,allsum); gettimeofday(&js,NULL);en d=js.tv_usec;prin tf(time=%dn,e nd-start);return 0;相關操作界面和運行結果界面截圖等M用祥序垃肯文本編輕著tMlLCDocumentsinclude #1nclud include #inc lude til lib. h#include static dauhle all5un=( Oj ini max1()int i.j;double a42OUt J; double sum 1=(S fi double rcadBuf4; time t

15、 start,end; pid_t pld;struct tiinVdl js; int tpl4j 12;srandr (unsigned) time (ill LL); for(i-0; J-=4;L+)for (j =13; j2b0ec; j+)a L i J J -(double) rand ()/(daubl RAND I4AX; gettlmeoTday(&J s NUU);sLart=j s *tv uecjfor(i=0;i47i+-)if(pipe(fpliJu)prlrtf ( pipf1 rrrnr ); exit(l);)pid=1c rk();If(pid0) pr

16、lntfCTork error!); exlt(l):xslocalhost Documents$ gedit test.cxslacalhost Documents$ gcc test.c - o testxslocalhost DocumentsS ./testsum0=12555. 205041sun=12480.645962sum2sum3=3 2507.80316SUnt=50068.S43522tinted G01xsMocalhost Documents $四、實驗小結(包括問題和解決方法、心得體會、意見與建議等)本次實驗在編寫程序時,主要是應掌握創建進程,以及對于進程各個操作。使用 fork()函數創建進程,fork函數在父進程中調用一次,在

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論