資源描述:
《操作系統(tǒng)軟中斷實(shí)驗(yàn).doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、軟中斷通信一、實(shí)驗(yàn)內(nèi)容編寫(xiě)一段程序,使其現(xiàn)實(shí)進(jìn)程的軟中斷通信。二、實(shí)驗(yàn)?zāi)康模?)加深對(duì)進(jìn)程概念的理解,明確進(jìn)程和程序的區(qū)別。(2)進(jìn)一步認(rèn)識(shí)并發(fā)執(zhí)行的實(shí)質(zhì)。(3)分析進(jìn)程競(jìng)爭(zhēng)資源現(xiàn)象,學(xué)習(xí)解決進(jìn)程互斥的方法。(4)了解Linux系統(tǒng)中進(jìn)程通信的基本原理。三、實(shí)驗(yàn)題目要求:使用系統(tǒng)調(diào)用fork()創(chuàng)建兩個(gè)子進(jìn)程,再用系統(tǒng)調(diào)用signal()讓父進(jìn)程捕捉鍵盤上來(lái)的中斷信號(hào)(即按DEL鍵);當(dāng)捕捉到中斷信號(hào)后,父進(jìn)程用系統(tǒng)調(diào)用Kill()向兩個(gè)子進(jìn)程發(fā)出信號(hào),子進(jìn)程捕捉到信號(hào)后分別輸出下列信息后終止:ChildProcessll
2、isKilledbyParent!ChildProcessl2isKilledbyParent!父進(jìn)程等待兩個(gè)子進(jìn)程終止后,輸出如下的信息后終止ParentProcessisKilled!②在上面的程序中增加語(yǔ)句signal(SIGNAL,SIG-IGN)和signal(SIGQUIT,SIG-IGN),觀察執(zhí)行結(jié)果,并分析原因。<程序流程圖>三、源程序及注釋#include#include#includevoidwaiting(),stop(),alarming(
3、);intwait_mark;main(){intp1,p2;if(p1=fork())/*創(chuàng)建子進(jìn)程p1*/{if(p2=fork())/*創(chuàng)建子進(jìn)程p2*/{wait_mark=1;signal(SIGINT,stop);/*接收到^c信號(hào),轉(zhuǎn)stop*/signal(SIGALRM,alarming);/*接受SIGALRMwaiting();kill(p1,16);/*向p1發(fā)軟中斷信號(hào)16*/kill(p2,17);/*向p2發(fā)軟中斷信號(hào)17*/wait(0);/*同步*/wait(0);printf("pare
4、ntprocessiskilled!");exit(0);}else{wait_mark=1;signal(17,stop);signal(SIGINT,SIG_IGN);/*忽略^c信號(hào)*/while(wait_mark!=0);lockf(1,1,0);printf("childprocess2iskilledbyparent!");lockf(1,0,0);exit(0);}}else{wait_mark=1;signal(16,stop);signal(SIGINT,SIG_IGN);/*忽略^c信號(hào)*/w
5、hile(wait_mark!=0)lockf(1,1,0);printf("childprocess1iskilledbyparent!");lockf(1,0,0);exit(0);}}voidwaiting(){sleep(5);if(wait_mark!=0)kill(getpid(),SIGALRM);}voidalarming(){wait_mark=0;}voidstop(){wait_mark=0;}〈任務(wù)2〉在上面的任務(wù)1中,增加語(yǔ)句signal(SIGINT,SIG_IGN)和語(yǔ)句signal(SI
6、GQUIT,SIG_IGN),觀察執(zhí)行結(jié)果,并分析原因。這里,signal(SIGINT,SIG_IGN)和signal(SIGQUIT,SIG_IGN)分別為忽略鍵信號(hào)以及忽略中斷信號(hào)。<程序>#include#include#includeintpid1,pid2;intEndFlag=0;intpf1=0;intpf2=0;voidIntDelete(){kill(pid1,16);kill(pid2,17);}voidInt1(){printf("child
7、process1iskilled!byparent");exit(0);}voidInt2(){printf("childprocess2iskilled!byparent");exit(0);}main(){intexitpid;if(pid1=fork()){if(pid2=fork()){signal(SIGINT,IntDelete);waitpid(-1,&exitpid,0);waitpid(-1,&exitpid,0);printf("parentprocessiskilled");exit(0)
8、;}else{signal(SIGINT,SIG_IGN);signal(17,Int2);pause();}}else{signal(SIGINT,SIG_IGN);signal(16,Int1);pause();}}三、程序運(yùn)行時(shí)初值和運(yùn)行結(jié)果程序一運(yùn)行結(jié)果:實(shí)驗(yàn)二運(yùn)行結(jié)果:不做任何操作等待五秒鐘父