P、V-原語的模擬實(shí)現(xiàn).docx_第1頁
P、V-原語的模擬實(shí)現(xiàn).docx_第2頁
P、V-原語的模擬實(shí)現(xiàn).docx_第3頁
P、V-原語的模擬實(shí)現(xiàn).docx_第4頁
P、V-原語的模擬實(shí)現(xiàn).docx_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

P、V 原語的模擬實(shí)現(xiàn)一、實(shí)驗?zāi)康谋菊n題實(shí)習(xí)的目的是,加深對進(jìn)程概念及進(jìn)程管理各部分內(nèi)容的理解;熟悉進(jìn)程管理中主要數(shù)據(jù)結(jié)構(gòu)的設(shè)計及進(jìn)程調(diào)度算法,進(jìn)程控制機(jī)構(gòu)、同步結(jié)構(gòu)、通迅機(jī)構(gòu)的實(shí)施。要求設(shè)計一個允許n個進(jìn)程并發(fā)運(yùn)行的進(jìn)程管理模擬糸統(tǒng)。該糸統(tǒng)包括有簡單的進(jìn)程控制、同步及通迅機(jī)構(gòu),其進(jìn)程調(diào)度算法可任意選擇。每個進(jìn)程用一個PCB表示,其內(nèi)容可根據(jù)具體情況設(shè)置。各進(jìn)程之間應(yīng)有一定的同步關(guān)糸。糸統(tǒng)在運(yùn)行過程中應(yīng)能顯示或打印各進(jìn)程的狀態(tài)及有關(guān)參數(shù)的變化情況,以便觀察諸進(jìn)程的運(yùn)行過程及糸統(tǒng)的管理過程。1) 理解信號量相關(guān)理論;2) 掌握記錄型信號量結(jié)構(gòu);3) 掌握 P、V 原語實(shí)現(xiàn)機(jī)制。二、實(shí)驗要求1) 輸入給定代碼;2) 進(jìn)行功能測試并得出正確結(jié)果。3) 分析P和V函數(shù)功能模塊;4) 在實(shí)驗報告中畫出P和V函數(shù)流程圖;5) 撰寫實(shí)驗報告。三、實(shí)驗內(nèi)容本實(shí)驗針對操作系統(tǒng)中信號量相關(guān)理論進(jìn)行實(shí)驗,要求實(shí)驗者輸入實(shí)驗指導(dǎo)書提供的代碼并進(jìn)行測試。代碼主要模擬信號量的 P 操作和 V 操作。1) 信號量信號量也稱為信號鎖,主要應(yīng)用于進(jìn)程間的同步和互斥,在用于互斥時,通常作為資源鎖。信號量通常通過兩個原子操作P和 V來訪問。P操作使信號量的值+1,V操作使信號量的值-1。2) 記錄型信號量記錄型信號量采用了“讓權(quán)等待”的策略,存在多個進(jìn)程等待訪問同一臨界資源的情況,所以記錄型信號量需要一個等待鏈表來存放等待該信號量的進(jìn)程控制塊或進(jìn)程號。在本實(shí)驗中,使用記錄型信號量。四、功能測試五#include#define TRUE 1#define FALSE 0#define MAXPRI 100#define NIL -1struct int id;char status;int nextwr;int priority;pcb1;struct int value;int firstwr; sem2;char savearea13,addr;int i,s1,s2,seed,exe=NIL;init( ) int j; for (j=0;j1;j+)pcbj.id=j;pcbj.status=r;pcbj.nextwr=NIL;printf(n process%d priority?,j+1);scanf (%d,&i);pcbj.priority=i;sem0.value=1;sem0.firstwr=NIL;sem1.value=1;sem1.firstwr=NIL;for (i=1;i1;i+)for (j=0;j3;j+)saveareaij=0;float random ( )int m; if (seed0) m=-seed;else m=seed;seed=(24151*seed+11839)%64416;return(m/12565.0);timeint(char addr)float x;x=random();if (x0.11)&(exe=0)return(FALSE);if (x0.66)&(exe=1)return(FALSE);if (x1.0)&(exe=2)return(FALSE);saveareaexe0=i;saveareaexe1=addr;pcbexe.status=t;printf(Times silce interruptnprocess%d enter into ready.n, exe+1);exe=NIL;return (TRUE);find()int j,pd=NIL, w=MAXPRI;for (j=0;j1;j+)if (pcbj.status=r)if (pcbj.priorityw)w=pcbj.priority; pd=j; if (pd=NIL)for (j=0; j1;j+)if (pcbj.status=t)if (pcbj.priorityw) w=pcbj.priority; pd=j; return(pd) ;scheduler() int pd; if(pd=find()=NIL& exe=NIL)return(NIL); if (pd!=NIL) if(exe=NIL)pcbpd.status=e;exe=pd;printf(process%d is executing.n, exe+1);else if (pcbpd.priority=0) return(FALSE);block(se);saveareaexe0=i;saveareaexe1=ad;exe=NIL;return(TRUE);wakeup(int se)int w;w=semse.firstwr;if(w!=NIL) semse.firstwr=pcbw.nextwr; pcbw.status=r; printf(process%d is waken upn,w+1);v(int se,char ad)if(+semse.value0) return(FALSE);wakeup(se);saveareaexe1=ad;saveareaexe0=i;return(TRUE);eexit(int n)pcbn.status=c;printf(process%d is completed!n, n+1); exe=NIL;processl() if(addr=a)goto a1; if(addr=b)goto b1; if(addr=c)goto c1; if(addr=d)goto d1; if(addr=e)goto e1; if(addr=f)goto f1;for(i=1;i6;i+) printf(process1 calls P on the semaphore1n); if(p(0,a)break; a1:printf(process1 is executing in the cretical section 1n); if(timeint(b) break; b1:printf(s1=%dn,+s1); printf(process1 calls V on semaphorel and quit cretical section1n); if(v(0,c)break; c1:printf(process1 calls P on esemaphorel 2n); if(p(1,d)break; d1:printf(process1 is execting cretical section 2n); if(timeint(e)break; e1:printf(s2=%dn,+s2); printf(process1 calls V on semaphore2 and quit cretical section2n);if(v(1,f)break; f1:printf(process1 cycle count=%dn,i); if(i6) return; eexit(0);process2()if(addr=a)goto a2;if(addr=b)goto b2;if(addr=c)goto c2;if(addr=d)goto d2;if(addr=e)goto e2;if(addr=f)goto f2;for(i=1;i6;+i)printf(process2 calls P on the semaphore2n);if(p(1,a)break;a2:printf(process2 is executing in the cretical section2n);if(timeint(b)break;b2:printf(s2=%dn,+s2);printf(process2 calls V on semaphore2 and quit cretical section2.n);if(v(1,c)break;c2:printf(process2 calls P on esemaphore1.n);if(p(0,d)break;d2:printf(process2 is execting cretical section1.n);if(timeint(e)break;e2:printf(s1=%dn,+s1);printf(process2 calls V on semaphore1 and quit cretical section1.n);if(v(0,f)break;f2:printf(process2 cycle count=%dn,i); if(i6) return;eexit(1);process1()if (addr=a) goto a1;if (addr=b) goto b1;if (addr=c) goto c1;for (i=1;i6; +i)printf(process1,calls P on semaphore2n);if(p(1,a) break; / * process 1 is biocked * /a1: printf(process 1 is execcuting on its cretical sectionn);if(timeint(b) break;b1: printf (s2=%dn, +s2); printf (process1 calls V on semapore2 and quit cretical sectionn);if(v(1,c) break; / * wake up a biocked process * /c1: printf(process1 cycien count=%dn,i); if(i6) return;eexit(2); main ( )int k;printf (* * * * process management * * * * nn);init();printf(s1=%d, s2=%dn, s1, s2);printf(process1, process2, process1 are all in ready ! n);for ( ; ;)if (k=scheduler()

溫馨提示

  • 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論