3120150901209夏霜3進(jìn)程控制_第1頁(yè)
3120150901209夏霜3進(jìn)程控制_第2頁(yè)
3120150901209夏霜3進(jìn)程控制_第3頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、計(jì)算機(jī)與軟件工程學(xué)院實(shí)驗(yàn)報(bào)告(2017/2018 學(xué)年第 1 學(xué)期)課程名稱操作系統(tǒng)實(shí)驗(yàn)課程代碼150901189實(shí)驗(yàn)時(shí)間2017.11.27指導(dǎo)單位軟件工程系指導(dǎo)教師譚平學(xué)生姓名夏霜年級(jí)2015 級(jí)學(xué) 號(hào)312015090120專業(yè)計(jì)算機(jī)科學(xué)與技9術(shù)實(shí)驗(yàn)名稱進(jìn)程控制實(shí)驗(yàn)地點(diǎn)8321實(shí)驗(yàn)類型驗(yàn)證實(shí)驗(yàn)學(xué)時(shí)4一、實(shí)驗(yàn)?zāi)康暮鸵?. 加深對(duì)進(jìn)程概念的理解,進(jìn)一步認(rèn)識(shí)并發(fā)執(zhí)行的實(shí)質(zhì)2. 掌握Linux操作系統(tǒng)的進(jìn)程創(chuàng)建和終止操作3. 利用Linux操作系統(tǒng)提供的信號(hào)量工具實(shí)現(xiàn)進(jìn)程間的同步4. 掌握對(duì)共享內(nèi)存的相關(guān)操作二、實(shí)驗(yàn)環(huán)境(實(shí)驗(yàn)設(shè)備)硬件:微型計(jì)算機(jī)軟件:Linux操作系統(tǒng)三、實(shí)驗(yàn)內(nèi)容及過程1.

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

3、signal (SIGQUIT, SIG-IGN),觀察執(zhí)行結(jié)果,并分析原因。解答:源代碼#in clude<stdio.h>#in cludevsig nal.h>#in cludev uni std.h>void 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);signal(SIGALRM, alarming);wait in g();kill(p1, 1

4、6);kill(p2, 17);wait(0);wait(0);prin tf("pare nt process is killed!n");exit(0);elsewait_mark=1;sig nal(17, stop);sig nal(SIGINT,SIG_I GN);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);

5、sig nal(SIGINT,SIG_I GN);while(wait_mark!=O);lockf(1, 1,0);prin tf("childre n processl is killed by pare ntn");lockf(1,0, 0);exit(0);void wait in g()sleep(5);if(wait_mark!=O)kill(getpid(),SIGALRM);void alarmi ng()wait_mark=0;void stop()wait_mark=0;相關(guān)操作界面和運(yùn)行結(jié)果界面截圖不加入 signal (SIGINT, SIG-IGN

6、)和signal (SIGQUIT, SIG-IGN)運(yùn)行結(jié)果X5Rlocalho«t *1J oedtt signal test, cKs®localhost 亠£ get signaltest.c -osign attest .c: In f urictton "rimin':signaltest.c;Id;7;Lnplicit declaration of function fwait* -Wtnplicit-fjnctlon-declarattonwatt(e);Asignalt«t,c:22:7:rxnplicit decla

7、ration of function Pxit' 'WiipHcit'fjnction declaration鼻stjnaltestrc:22:7:i tncompatible lnplicit declaration of built-in funetton -«dt*sigri altest:7t otv include J <£tdltbh> h or provide a decla ration of * exit1 sign attest»c;34;7:Inconpettble implicit declaration

8、 of built in function fexttfextt(e);At - nrludp GstdlAb.h,' or provid? atian of 1 exitJsvjnaltest,c:47:5: rxnconptible implicit declaration of built-in function "exit*exlt(e);AxsSlccalhost i缶 + /signa Ltestcnnoren processl is killed by parent children process2 ts killed by parent parent pro

9、cess ts killed!加入 signal (SIGINT, SIG-IGN)相關(guān)操作界面和運(yùn)行結(jié)果界面截圖X5®1 ocalhost I gec signaltestl.c s stgnaltttl slgnaltestl: In function f main *:slgnaltestl »c ;19: ?: jr n in -| - inplicit declardtion of function * wait1 -Uinplicit -tunctton-dec Larattonjsignaltestl:Z2:7; "n tn j: implicit

10、declaration of function *cxit1 Winplicit -runccton decldretUnextt(O);$<9nalte«tlTc:2Z:7: ntnj: inconpatible i-Apltcit declarattori of buvlt'i.D funct ion * exit *stgnaltesu.c:22:T:include stdltb-h?' or provide a oeclararton of 'exitJsignaltMtl.ci34:7! -l-ninj: incompatibl電 tmplic

11、lt declaration of built-in funct ton 'exit*extt(ft);stg-ialtestl.c:34:7:includeor provide a declaration of 'exit|xsiocalhost S ./signaltestlCchiLdren processl is killed by parent children processZ is killed by parent parent process is ktlled!加入 signal (SIGQUIT, SIG-IGN)xsiMocalhost £ qe

12、dtt stgnaltest2.cxsfilocalliost $ gcc stgnaltest2.c y ilgnaltestlstgna LtestZ.c: Ln function 'naln':signaltestZ. c: 19:7:,'j rni.r) inplicit declaratton of functior *waitr -hirplicit-fu ncttawatt(O);slgnaltcstZ»c:Z2:7: Harming inpltcit declaration of functior 'exitR -WinpHcit -f

13、unction'declaratiopexit(e):sigheltest2rC:22:7: rf-amirj Anconpatible inplicit declardtion of built-in funct ton "exit*signaltestz.c:22:7iinclude stdltb.h*' or provide a declaration or -exttsignaltest2»c:34i7: -ti.- i tncompatible inpltcit declaration of built-in funct ion»xtt(

14、e);signlstZ!t;34;7;include f<5tdlib*h>d or provide a declaration of cexitj X5®localhost * S ./stgfialtestz ACparent process ts killed!原因:sig nal(SIGINT,SIG_IGN) 是忽略鍵入信號(hào),sig nal (SIGQUIT, SIG-IGN) 是忽略中斷信號(hào)。在子進(jìn)程中加入 sig nal(SIGINT,SIG_IGN),子進(jìn)程會(huì)忽略中斷信號(hào),在子進(jìn)程中加入sig nal (SIGQUIT, SIG-IGN),相應(yīng)的中斷信號(hào)就

15、被忽略,只有父進(jìn)程被中斷2求100000個(gè)浮點(diǎn)數(shù)(精確小數(shù)點(diǎn)右 4位)的平均值(和、最大值、最小值)。要求: 隨機(jī)生成100000個(gè)浮點(diǎn)數(shù)(父進(jìn)程); 創(chuàng)建4個(gè)子進(jìn)程,分別求 25000個(gè)浮點(diǎn)數(shù)之和; 父進(jìn)程完成100000個(gè)浮點(diǎn)數(shù)之和并打印結(jié)果; 統(tǒng)計(jì)順序計(jì)算的時(shí)間和多個(gè)進(jìn)程采用多道程序設(shè)計(jì)完成計(jì)算的時(shí)間。 解答:源代碼#in elude <stdio.h>#in elude <sys/types.h>#in elude <time.h>#in elude <stdlib.h>#in elude vuni std.h>static dou

16、ble allsum=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;sran d(u nsig ned)time(NULL);for(i=0;i<4;i+)for(j=0;j<25000;j+)aij=(double)ra nd()/(double)RAND_MAX; gettimeofday(&js,NULL);start=js.tv_usec;for(i=0;i<

17、4;i+)if(pipe(fpi)<0)prin tf("pipe error");exit(1);pid=fork();if(pid<0)prin tf("fork error!");exit(1);else if(pid=0)for(j=0;j<25000;j+)sumi+=aij;write(fpi1, &sumi,sizeof(double);/prin tf("sum%d=%fn",i,sumi);exit(1);elseif(!read(fpiO,&readBufi,sizeof(doub

18、le) exit(1);prin tf("sum%d=%fn",i,readBufi);for(i=0;i<4;i+)allsum+=readBufi;prin tf("sum=%fn",allsum);gettimeofday(&js,NULL);en d=js.tv_usec;prin tf("time=%dn",e nd-start);return 0;相關(guān)操作界面和運(yùn)行結(jié)果界面截圖等曲用祥序樓背文本騙鐺著testx*/DO:IJT#ntEinclude #includ #J.Mlud 它 tincludetstd

19、io.h> <sys/types.h> <tlni*h> “ tdlib.hA#include <unistd.h> static douhle allsun-G.0; ini max1(int i.j; double a4Jl250a6J; double sum-1=fl fi double rcadBuF4; tijne t start, end; pid_t pid;structj s;In* tp!4j2;srandr(unsigned)time(JULL); ford-O; J<4;1+)for( j=t);j<2bOO0;j+)a L i J J -(double) rand ()/(daubl« RAND I4AX; geTtlneoTday(&j s «NULL);sLart=j s *Iv usecj for(i=0;i<47i+)if(pipe(fplL<u)prlritf ( pipe error'); exit(l);

溫馨提示

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

評(píng)論

0/150

提交評(píng)論