操作系統(tǒng)實驗報告實驗一進程管理_第1頁
操作系統(tǒng)實驗報告實驗一進程管理_第2頁
操作系統(tǒng)實驗報告實驗一進程管理_第3頁
操作系統(tǒng)實驗報告實驗一進程管理_第4頁
操作系統(tǒng)實驗報告實驗一進程管理_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、.實驗一 進程管理一、目的進程調(diào)度是處理機管理的核心內(nèi)容。本實驗要求編寫和調(diào)試一個簡單的進程調(diào)度程序。通過本實驗加深理解有關進程控制塊、進程隊列的概念,并體會和了解進程調(diào)度算法的具體實施辦法。二、實驗內(nèi)容及要求1、設計進程控制塊PCB的結構(PCB結構通常包括以下信息:進程名(進程ID)、進程優(yōu)先數(shù)、輪轉(zhuǎn)時間片、進程所占用的CPU時間、進程的狀態(tài)、當前隊列指針等??筛鶕?jù)實驗的不同,PCB結構的內(nèi)容可以作適當?shù)脑鰟h)。為了便于處理,程序中的某進程運行時間以時間片為單位計算。各進程的輪轉(zhuǎn)時間數(shù)以及進程需運行的時間片數(shù)的初始值均由用戶給定。2、系統(tǒng)資源(r1rw),共有w類,每類數(shù)目為r1rw。隨

2、機產(chǎn)生n進程Pi(id,s(j,k),t),0=i=n,0=j=m,0=k=dt為總運行時間,在運行過程中,會隨機申請新的資源。3、每個進程可有三個狀態(tài)(即就緒狀態(tài)W、運行狀態(tài)R、等待或阻塞狀態(tài)B),并假設初始狀態(tài)為就緒狀態(tài)。建立進程就緒隊列。4、編制進程調(diào)度算法:時間片輪轉(zhuǎn)調(diào)度算法本程序用該算法對n個進程進行調(diào)度,進程每執(zhí)行一次,CPU時間片數(shù)加1,進程還需要的時間片數(shù)減1。在調(diào)度算法中,采用固定時間片(即:每執(zhí)行一次進程,該進程的執(zhí)行時間片數(shù)為已執(zhí)行了1個單位),這時,CPU時間片數(shù)加1,進程還需要的時間片數(shù)減1,并排列到就緒隊列的尾上。三、實驗環(huán)境操作系統(tǒng)環(huán)境:Windows系統(tǒng)。編程語

3、言:C#。四、實驗思路和設計1、程序流程圖對進程進行初始化,建立就緒隊列、阻塞隊列。Input()觸發(fā)時鐘,調(diào)用時間片輪轉(zhuǎn)調(diào)度算法。runFcfs()取就緒隊列的第一個進程,判斷其運行的時間片是否達到所需次數(shù)。如果達到,則釋放資源如果沒達到,則運行一個時間片。running()輸出就緒隊列和阻塞隊列的信息。outputall()就緒隊列為空?檢查阻塞隊列,對于當前資源數(shù)目滿足阻塞隊列的進程,由阻塞轉(zhuǎn)入就緒隊列。testblock()檢查是否有新進程產(chǎn)生,如果有,則判斷系統(tǒng)資源是否夠用,如果夠用,則分配給該進程,插入就緒隊列。如果不夠用,則插入阻塞隊列。testnew()顯示三類資源情況。res

4、core()結束開始是否2、主要程序代碼/PCB結構體struct pcb public int id; /進程ID public int ra; /所需資源A的數(shù)量 public int rb; /所需資源B的數(shù)量 public int rc; /所需資源C的數(shù)量 public int ntime; /所需的時間片個數(shù) public int rtime; /已經(jīng)運行的時間片個數(shù) public char state; /進程狀態(tài),W(等待)、R(運行)、B(阻塞) /public int next; ArrayList hready = new ArrayList(); ArrayList h

5、block = new ArrayList(); Random random = new Random(); /ArrayList p = new ArrayList(); int m, n, r, a,a1, b,b1, c,c1, h = 0, i = 1, time1Inteval;/m為要模擬的進程個數(shù),n為初始化進程個數(shù) /r為可隨機產(chǎn)生的進程數(shù)(r=m-n) /a,b,c分別為A,B,C三類資源的總量 /i為進城計數(shù),i=1n /h為運行的時間片次數(shù),time1Inteval為時間片大?。ê撩耄?對進程進行初始化,建立就緒數(shù)組、阻塞數(shù)組。public void input()/對進

6、程進行初始化,建立就緒隊列、阻塞隊列 m = int.Parse(textBox4.Text); n = int.Parse(textBox5.Text); a = int.Parse(textBox6.Text); b = int.Parse(textBox7.Text); c = int.Parse(textBox8.Text); a1 = a; b1 = b; c1 = c; r = m - n; time1Inteval = int.Parse(textBox9.Text); timer1.Interval = time1Inteval; for (i = 1; i = 0 & (b

7、- jincheng.rb) = 0 & (c - jincheng.rc) = 0) a = a - jincheng.ra; b = b - jincheng.rb; c = c - jincheng.rc; jincheng.state = W; hready.Add(jincheng);/加入就緒隊列 else jincheng.state = B; hblock.Add(jincheng);/加入阻塞隊列 listBox1.Items.Add(當前進程狀態(tài): + jincheng.state); /從數(shù)組起始地址開始輸出該數(shù)組的內(nèi)容public void disp(ArrayList

8、 list) ArrayList list1 = new ArrayList(); list1 = list; if (list1.Count 0) for (int j = 0; j list1.Count; j+) pcb p = (pcb)list1j; listBox1.Items.Add( + p.id.ToString() + + p.state.ToString() + + p.ra.ToString() + + p.rb.ToString() + + p.rc.ToString()+ + p.ntime.ToString() + + p.rtime.ToString() + r

9、n); else listBox1.Items.Add(rnt 該隊列中沒有進程!rn); /輸出就緒數(shù)組和阻塞數(shù)組的信息public void outputall() listBox1.Items.Add(rn=CPU運行了: + h.ToString() + 次=rn); listBox1.Items.Add(*當前就緒隊列的信息!*); listBox1.Items.Add(進程ID 進程狀態(tài) A資源數(shù) B資源數(shù) C資源數(shù) 所需時間片 已運行時間片); disp(hready); listBox1.Items.Add(*當前就阻塞列的信息!*); listBox1.Items.Add(進

10、程ID 進程狀態(tài) A資源數(shù) B資源數(shù) C資源 所需時間片 已運行時間片); disp(hblock); /運行就緒數(shù)組的頭進程,運行一個時間片,輪轉(zhuǎn)一個時間片,時間片輪轉(zhuǎn)調(diào)度算法public void running() ArrayList hready1 = new ArrayList(); hready1 = hready; pcb p1 = new pcb(); p1=(pcb)hready10; p1.state=R; p1.rtime= p1.rtime + 1; h=h+1; listBox1.Items.Add(rn當前正在運行進程ID是: +p1.id + rn); listB

11、ox1.Items.Add(rn進程ID 進程狀態(tài) A資源數(shù) B資源數(shù) C資源數(shù) 所需時間片 已運行時間片rn); listBox1.Items.Add(p1.id + +p1.state+ + p1.ra + + p1.rb + + p1.rc + + p1.ntime + + p1.rtime); if (p1.ntime=p1.rtime) listBox1.Items.Add(p1.id.ToString()+的進程已經(jīng)完成!rn); a = a + p1.ra; b = b + p1.rb; c = c + p1.rc; hready.RemoveAt(0); else p1.sta

12、te=W; hready1.Add(p1); hready.RemoveAt(0); /檢測當前資源數(shù)目是否滿足阻塞數(shù)組里進程的需求public void testblock() ArrayList hblock1 = new ArrayList(); hblock1 = hblock; for (int m = 0; m = 0) & (b - p1.rb = 0) & (c - p1.rc = 0) p1.state=W; hready.Add(p1); a = a - p1.ra; b = b - p1.rb; c = c - p1.rc; listBox1.Items.Add(ID號為

13、:+p1.id + 的進程由阻塞隊列轉(zhuǎn)入就緒隊列rn); hblock.RemoveAt(m); m-; /檢測是否有新的進程產(chǎn)生,隨機產(chǎn)生新進程public void testnew() int t; if (r0)/r為隨機產(chǎn)生的進程數(shù)目 t = random.Next(9) + 1; if (t = 0 & (b - jincheng.rb) = 0 & (c - jincheng.rc) = 0) a = a - jincheng.ra; b = b - jincheng.rb; c = c - jincheng.rc; jincheng.state = W; listBox1.Ite

14、ms.Add(進程狀態(tài)為: + jincheng.state); hready.Add(jincheng);/加入就緒隊列 listBox1.Items.Add(資源滿足新進程請求,該進程進入就緒隊列rn); else jincheng.state = B; hblock.Add(jincheng);/加入阻塞隊列 listBox1.Items.Add(進程狀態(tài)為: + jincheng.state); listBox1.Items.Add(資源不滿足新進程請求,該進程進入阻塞隊列rn); r = r - 1; /系統(tǒng)三類資源變化情況的顯示public void rescore()/系統(tǒng)三類資

15、源變化情況的顯示 if (a a1) textBox1.Text = a1.ToString(); if (a = 0 & a b1) textBox2.Text = b1.ToString(); if (b = 0 & b c1) textBox3.Text = c1.ToString(); if (c = 0 & c 0) outputall(); running(); testblock(); testnew(); rescore(); else timer1.Enabled = false; textBox1.Text = a1.ToString(); textBox2.Text =

16、b1.ToString(); textBox3.Text = c1.ToString(); listBox1.Items.Add(rnrn); /計時器觸發(fā)時間片輪轉(zhuǎn)調(diào)度算法private void timer1_Tick(object sender, EventArgs e) runFcfs();/開始模擬按鈕單擊執(zhí)行函數(shù)private void button1_Click(object sender, EventArgs e) runmain(); button1.Enabled = false; textBox1.Enabled = false; textBox2.Enabled = f

17、alse; textBox3.Enabled = false; textBox4.Enabled = false; textBox5.Enabled = false; textBox6.Enabled = false; textBox7.Enabled = false; textBox8.Enabled = false; textBox9.Enabled = false; /清除屏幕按鈕單擊執(zhí)行函數(shù)private void button2_Click(object sender, EventArgs e) textBox1.Text = ; textBox2.Text = ; textBox3

18、.Text = ; textBox4.Text = ; textBox5.Text = ; textBox6.Text = ; textBox7.Text = ; textBox8.Text = ; textBox9.Text = ; listBox1.Items.Clear(); textBox4.Enabled = true; textBox5.Enabled = true; textBox6.Enabled = true; textBox7.Enabled = true; textBox8.Enabled = true; textBox9.Enabled = true; button1.Enabled = true; /運行的主函數(shù)public void runmain() input(); imer1.Enabled = true;3、運行界面和運行結果界面中,可以任意設定需要模擬的進程總數(shù)(如5),初始化進程個數(shù)(如3),還有A、B、C三類資

溫馨提示

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

最新文檔

評論

0/150

提交評論