計算機操作系統實驗報告.doc_第1頁
計算機操作系統實驗報告.doc_第2頁
計算機操作系統實驗報告.doc_第3頁
計算機操作系統實驗報告.doc_第4頁
計算機操作系統實驗報告.doc_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

操作系統課程設計報告學 院 計算機 專 業(yè) 計算機科學與技術 班 級 2011級1班 題 目 實現處理機調度模塊功能 學 號 姓 名 指導教師 丁國芳 一、 采用具有二級調度的調度隊列模型。其中作業(yè)調度采用先來先服務策略,進程調度采用時間片輪轉調度算法。二、利用銀行家算法避免死鎖。三、課程設計要求:1. 系統總共運行100個作業(yè)。為每個進入后備隊列的作業(yè)創(chuàng)建一個JCB,內容包括:作業(yè)標識、預計運行時間、資源需求等。其中預計運行時間和資源需求隨機生成(120個時間片,A資源0-5,B資源0-3,C資源0-4)。2. 系統開始從后備隊列中調入10個作業(yè)進入系統。以后每完成一個作業(yè)就從后備隊列中調入一個,直到所有作業(yè)完成。3. 系統為每個作業(yè)創(chuàng)建一個進程,按JCB分配系統資源。共有3類系統資源,其中A類資源50個,B類資源30個,C類資源40個)。4. 進程在運行過程中(即每個時間片內),可能會申請新的資源(申請的各類資源個數均不超過2)。用銀行家算法控制資源的分配,避免死鎖。注意,每個進程申請各類資源的數目不能超過系統資源總數的二分之一。5. 創(chuàng)建三個阻塞隊列A、B、C,分別記錄因資源請求而阻塞的進程。6. 可視化編程,形象地展現進程運行的狀態(tài)。四、代碼實現:using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;namespace process_scheduling public partial class Form1 : Form StringBuilder sb = new StringBuilder(); static int timeflat = 0; static int, max = new int100, 3; static int, allocation = new int100, 3; static int, need = new int100, 3; public struct JCB public int flag; public String name;/作業(yè)名 public int servertime;/服務時間 public int arrivetime;/到達時間 public int remaintime;/剩余服務時間 public int f;/阻塞時獲得資源變成就緒狀態(tài)標志 ; Queue pqueue = new Queue();/后備隊列 Queue rqueue = new Queue();/就緒隊列 Queue blockA = new Queue();/阻塞A隊列 Queue blockB = new Queue();/阻塞B隊列 Queue blockC = new Queue();/阻塞C隊列 int resource = new int3 50, 30, 40 ;/分別是資源A ,B,C 和它們的數量 static JCB job = new JCB100; public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) sb.Append(); Random rd = new Random(); for (int i = 0; i 0) timeflat = Convert.ToInt32(tb_timeflat.Text); else if (tb_timeflat.Text.Length = 0) sb.Append(請輸入時間片!); sb.Append(Environment.NewLine); tb_content.Text = sb.ToString(); if (timeflat 20) sb.Append(時間片大小不合法!); sb.Append(Environment.NewLine); tb_content.Text = sb.ToString(); else run(); public void run() int count = 0; for (int i = 0; i count) if (blockA.Count = 0 & blockB.Count = 0 & blockC.Count = 0) sb.Append(count.ToString(); sb.Append( 處理機處理空轉 ); sb.Append(Environment.NewLine); count+; else wakeup(); else JCB pro1 = new JCB(); pro1 = rqueue.Dequeue(); if (allocationpro1.flag, 0 = maxpro1.flag, 0) if (allocationpro1.flag, 1 = maxpro1.flag, 1) if (allocationpro1.flag, 2 = maxpro1.flag, 2) if (pro1.remaintime = timeflat&pro1 .servertime =timeflat ) pro1.remaintime -= timeflat; sb.Append(時間: 作業(yè)名: 服務總時間: 剩余服務時間 狀態(tài) ); sb.Append(Environment.NewLine); sb.Append(count); sb.Append(-); sb.Append(count +timeflat) ); sb.Append( ); sb.Append(pro1 .name ); sb.Append( ); sb.Append(pro1 .servertime ); sb.Append( ); sb.Append(pro1 .remaintime ); if(pro1 .remaintime=0) sb.Append( 進程結束 ); else sb.Append( 運行中 ); sb.Append(Environment .NewLine ); count += timeflat;/當前時間加上時間片 if (pro1.remaintime != 0) addprocess(ref count ,9);/加回之前確保在這之前的進程加入到就緒隊列里,9確保能有位置加入 rqueue.Enqueue(pro1);/加回就緒隊列 showqueue(rqueue ); else sb.Append(時間: 作業(yè)名: 服務總時間: 剩余服務時間 狀態(tài) ); sb.Append(Environment.NewLine); sb.Append(count); sb.Append(-); sb.Append(count + pro1.remaintime ); sb.Append( ); sb.Append(); sb.Append( ); sb.Append(pro1.servertime); sb.Append( ); sb.Append(0 進程結束 ); sb.Append(Environment.NewLine); count += pro1 .remaintime ;/當前時間加上時間片 pro1.remaintime = 0;/服務時間 /進程結束查看阻塞隊列 if (pro1.remaintime = 0) relaxresource(pro1); wakeup(); else sb.Append(pro1 .name ); sb.Append(阻塞 ); sb.Append(Environment.NewLine); blockC.Enqueue(pro1); else sb.Append(); sb.Append(阻塞 ); sb.Append(Environment.NewLine); blockB.Enqueue(pro1); else sb.Append(); sb.Append(阻塞 ); sb.Append(Environment.NewLine); blockA.Enqueue(pro1); tb_content.Text = sb.ToString(); /銀行家算法 private int allocationarithmetic(int i, int requestA, int requestB, int requestC) int rtn = 1; /資源A if (requestA = resource0 & requestA 0) resource0 -= requestA;/減去相應資源 needi, 0 -= requestA;/減去相應需求資源數 allocationi, 0 += requestA;/增加相應資源數 else rtn = -1; /資源B if (requestB = resource1 & requestB 0) resource1 -= requestB;/減去相應資源 needi, 1 -= requestB;/減去相應需求資源數 allocationi, 1 += requestB;/增加相應資源數 else rtn = -1; /資源C if (requestC = resource2 & requestC 0) resource2 -= requestC;/減去相應資源 needi, 2 -= requestC;/減去相應需求資源數 allocationi, 2 += requestC;/增加相應資源數 else rtn = -1; if (rtn != -1) / Console.WriteLine(進程請求量出現錯誤,請核實后在運行程序!); /else rtn = safearithmetic(); if (rtn = 0)/此分配方案導致系統不安全 if (requestA 0) resource0 += requestA;/加上相應資源 needi, 0 += requestA;/加上相應需求資源數 allocationi, 0 -= requestA;/減去相應資源數 if (requestB 0) resource1 += requestB;/加上相應資源 needi, 1 += requestB;/加上相應需求資源數 allocationi, 1 -= requestB;/減去相應資源數 if (requestC 0) resource2 += requestC;/加上相應資源 needi, 2 += requestC;/加上相應需求資源數 allocationi, 2 -= requestC;/減去相應資源數 return rtn; /安全算法 private int safearithmetic() int finish = new int10;/完成工作量 int work = new int3;/現有資源記錄量 int, record = new int10, 3;/記錄就緒隊列中每個進程的需求量 int, allocate = new int10, 3;/記錄就緒隊列中每個進程已分配的資源量 int i = 0; work0 = resource0; work1 = resource1; work2 = resource2; foreach (JCB item in rqueue) recordi, 0 = needitem.flag, 0;/進程對資源A的需求量 recordi, 1 = needitem.flag, 1; recordi, 2 = needitem.flag, 2; allocatei, 0 = allocationitem.flag, 0; allocatei, 1 = allocationitem.flag, 1; allocatei, 2 = allocationitem.flag, 2; i+; i = 0; while (i 10) if (recordi, 0 = work0 & recordi, 1 = work1 & recordi, 2 = work2 & finishi = 0) work0 += allocationi, 0; work1 += allocationi, 1; work2 += allocationi, 2; finishi = 1; i = 0; else i+; int flg = 1; for (i = 0; i 0) jp1 = blockA.Peek(); if (needjp1.flag, 0 0) jp1 = blockB.Peek(); if (needjp1.flag, 1 = 2)/請求資源 request(ref jp1, 0, needjp1.flag, 1, 0); else request(ref jp1, 0, 2, 0); if (jp1.f = 1 & jp1.flag 0) jp1 = blockC.Peek(); if (needjp1.flag, 2 = 2)/請求資源 request(ref jp1, 0, 0, needjp1.flag, 2); else request(ref jp1, 0, 0, 2); if (jp1.f = 1 & jp1.flag jp.flag) jp = blockC.Peek(); bl = 2; if (bl != -1) switch (bl) case 0: jp = blockA.Dequeue(); break; case 1: jp = blockB.Dequeue(); break; case 2: jp = blockC.Dequeue(); break; rqueue.Enqueue(jp);/將已獲資源的進程加回到就緒隊列中 sb.Append(); sb.Append(進入就緒隊列 ); sb.Append(Environment.NewLine); showqueue(rqueue ); private void relaxresource(JCB jcb1) resource0 += allocationjcb1.flag, 0; resource1 += allocationjcb1.flag, 1; resource2 += allocationjcb1.flag, 2; private void addprocess(ref int count,int sum) JCB jb = new JCB(); if (pqueue.Count != 0) jb = pqueue.Peek(); else jb.arrivetime = int.MaxValue; while (jb.arrivetime = count & rqueue.Count 0) pro = pqueue.Dequeue(); /分配資源 if (resource0 - maxpro.flag, 0 = 0) needpro.flag, 0 = 0; allocationpro.flag, 0 = maxpro.flag, 0; resource0 -= maxpro.flag, 0; else allocationpro.flag, 0 = resource0; needpro.flag, 0 = maxpro.flag, 0 - allocationpro.flag, 0; resource0 = 0; if (resource1 - maxpro.flag, 1 = 0) needpro.flag, 1 = 0; allocationpro.flag, 1 = maxpro.flag, 1; resource1 -= maxpro.flag, 1; else allocationpro.flag, 1 = resource1; needpro.flag, 1 = maxpro.flag, 1 - allocationpro.flag, 1; resource1 = 0; if (resource2 - maxpro.flag, 2 = 0) needpro.flag, 2 = 0; allocationpro.flag, 2 = maxpro.flag, 2; resource2 -= maxpro.flag, 2; else allocationpro.flag, 2 = resource2; needpro.flag, 2 = maxpro.flag, 2 - allocationpro.flag, 2; resource2 = 0; rqueue.Enqueue(pro); tb_content.Text = tb_content.Text + .ToString() + 進入系統+Environment .NewLine ; if (pqueue.Count != 0) jb = pqueue.Peek(); /顯示當前就緒隊列中所有進程信息 private void showqueue(Queue q) sb.Append(當前就緒隊列中所有進程信息); sb.Append(Environment .NewLin

溫馨提示

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

評論

0/150

提交評論