- 相關推薦
操作系統課程設計報告模擬進程調度程序(一)
此設計報告是對操作系統中進程調度的兩種算法,即靜態優先權調度算法和需要時間片的轉法進行了描述,并分析了它們的工作機理。
最高優先權調度算法的基本思想是把CPU分配給就緒隊列中優先權最高的進程。靜態優先數是在創建進程時確定的,并在整個進程運行期間不再改變。
簡單輪轉法的基本思想是:所有就緒進程按 FCFS排成一個隊列,總是把處理機分配給隊首的進程,各進程占用CPU的時間片相同。如果運行進程用完它的時間片后還未完成,就把它送回到就緒隊列的末尾,把處理機重新分配給隊首的進程,直至所有的進程運行完畢。
然后用具體語言模擬了一個進程調度的程序。用戶可以自己輸入產生進程,然后選擇調度方式進行調度。所用的語言為VisualBasic.Net,結果顯示了調度運行過程。
問題描述和分析 ……………………………………………… 4
算法設計 …………………………………………………… 5
源代碼及說明 ………………………………………………… 5
結果與分析 ………………………………………………………17
參考文獻……………………………………………………… 18
一、問題描述和分析
問題描述
CPU調度是多道程序操作系統的基礎,幾乎所有計算機資源在使用前都要被調度,因此,CPU調度對于操作系統來說非常重要。
假如操作系統中存在若干進程,這些進程,將會被按照指定的調度方式,由CPU進行調度。
本程序,用來模擬實現操作系統中的兩種調度方法,即:優先權調度和輪轉法調度。
下面對四種調度方法進行描述
優先權調度(priority-scheduling algorithm):在這種方式下,每一個進程都有一個優先權與其關聯,具有最高優先權的進程會被分配到CPU,具有相同優先權的進程按FCFS順序調度。
輪轉法(round-robin):這種調度方式是專門為分時系統而設計的。它類似于FCFS調度,但是增加了搶占以在進程是。定義一個較小時間單元,稱為時間量或時間片。時間片通常為10ms到100ms。為每個進程分配不超過一個時間片間隔的CPU。
分析
根據描述確定解決方法。本程序利用VisualBasic.Net語言實現。首先定義一個進程的類,包括進程名、達到時間、服務時間、優先權。再定義一個調度類,用以實現調度算法。
最后在主程序中,用戶可選擇兩種調度算法,確定問題的類型。
二、算法設計
主要設計思想
在主程序中,通過選擇結構,調用兩種調度算法。
各種調度算法實現
首先建立一個進程類, 然后建立一個調度類,通過這個類來執行調度算法。再建立一個鏈表,用來存放這些進程。
優先權調度:在優先權調度中,每產生一個進程,程序會記錄進程的優先權,然后按產生的先后順序插入。當當前進程結束時,程序從鏈表中取出一個優先權最高的進程執行。
輪轉法調度:由用戶定義一個時間片。在每個時間片中,程序執行一個進程。當時間片結束后,程序將結束當前執行的進程,從鏈表中調入下一個進程,并將當前執行的進程插入到鏈表尾部。整個鏈表相當于一個循環隊列。
三、源代碼及說明
1優先級調度算法源代碼
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label7.Width < 200 Then
Label7.Visible = True
Label7.Width += 10
Label17.Visible = True
Label16.Visible = False
Label18.Visible = False
Else
Label17.Visible = False
Label18.Visible = False
Label16.Visible = True
a = 6
End If
If a < b And a < c And a < d Then
Timer1.Enabled = True
End If
If b < a And b < c And b < d Then
Timer2.Enabled = True
End If
If c < a And c < b And c < d Then
Timer3.Enabled = True
End If
If d < a And d < b And d < c Then
Timer4.Enabled = True
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If Label8.Width < 200 Then
Label8.Visible = True
Label8.Width += 10
Label20.Visible = True
Label19.Visible = False
Label21.Visible = False
Else
Label20.Visible = False
Label21.Visible = False
Label19.Visible = True
b = 7
End If
If a < b And a < c And a < d Then
Timer1.Enabled = True
End If
If b < a And b < c And b < d Then
Timer2.Enabled = True
End If
If c < a And c < b And c < d Then
Timer3.Enabled = True
End If
If d < a And d < b And d < c Then
Timer4.Enabled = True
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
If Label9.Width < 200 Then
Label9.Visible = True
Label9.Width += 10
Label23.Visible = True
Label22.Visible = False
Label24.Visible = False
Else
Label23.Visible = False
Label24.Visible = False
Label22.Visible = True
c = 8
End If
If a < b And a < c And a < d Then
Timer1.Enabled = True
End If
If b < a And b < c And b < d Then
Timer2.Enabled = True
End If
If c < a And c < b And c < d Then
Timer3.Enabled = True
End If
If d < a And d < b And d < c Then
Timer4.Enabled = True
End If
End Sub
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
If Label10.Width < 200 Then
Label10.Visible = True
Label10.Width += 10
Label26.Visible = True
Label25.Visible = False
Label27.Visible = False
Else
Label26.Visible = False
Label27.Visible = False
Label25.Visible = True
d = 9
End If
If a < b And a < c And a < d Then
Timer1.Enabled = True
End If
If b < a And b < c And b < d Then
Timer2.Enabled = True
End If
If c < a And c < b And c < d Then
Timer3.Enabled =True
End If
If d < a And d < b And d < c Then
Timer4.Enabled = True
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
a = TextBox1.Text
b = TextBox2.Text
c = TextBox3.Text
d = TextBox4.Text
If a < b And a < c And a < d Then
Timer1.Enabled = True
End If
If b < a And b < c And b < d Then
Timer2.Enabled = True
End If
If c < a And c < b And c < d Then
Timer3.Enabled = True
End If
If d < a And d < b And d < c Then
Timer4.Enabled = True
End If
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
Button1.Enabled = False
Button2.Enabled = True
Label18.Visible = True
Label21.Visible = True
Label24.Visible = True
Label27.Visible = True
End Sub
End Class
2輪轉法調度源代碼
'
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
a = TextBox1.Text
b = TextBox2.Text
c = TextBox3.Text
d = TextBox4.Text
If a > 0 And b > 0 And c > 0 And d > 0 Then
Timer1.Enabled() = True
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
i = a - Label7.Width
If i < 20 Then
Label7.Width = Label7.Width + i
Label18.Visible = False
Label17.Visible = False
Label16.Visible = True
Timer1.Enabled = False
Timer2.Enabled = True
End If
If Label7.Width < a Then
Label7.Width = Label7.Width + 10
Label18.Visible = True
Label17.Visible = False
Label16.Visible = False
Timer1.Enabled = False
Timer2.Enabled = True
Else
Label18.Visible = False
Label17.Visible = False
Label16.Visible = True
Timer1.Enabled = False
Timer2.Enabled = True
End If
If Label8.Width >= b Then
Label21.Visible = False
Label20.Visible = False
Label19.Visible = True
Else
Label21.Visible = False
Label20.Visible = True
Label19.Visible = False
End If
If Label7.Width < a And Label8.Width >= b And Label9.Width >= c And Label10.Width >= d Then
Label18.Visible = False
Label17.Visible = True
Label16.Visible = False
ElseIf Label29.Width = a Then
Label18.Visible = False
Label17.Visible = False
Label16.Visible = True
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim i As Integer
i = b - Label8.Width
If i < 20 Then
Label8.Width += i
Label21.Visible = False
Label20.Visible = False
Label19.Visible = True
Timer2.Enabled = False
Timer3.Enabled = True
End If
If Label8.Width < b Then
Label8.Width += 10
Label21.Visible = True
Label20.Visible = False
Label19.Visible = False
Timer2.Enabled = False
Timer3.Enabled = True
Else
Label21.Visible = False
Label20.Visible = False
Label19.Visible = True
Timer2.Enabled = False
Timer3.Enabled = True
End If
If Label9.Width >= c Then
Label24.Visible = False
Label23.Visible = False
Label22.Visible = True
Else
Label24.Visible = False
Label23.Visible = True
Label22.Visible = False
End If
If Label8.Width < b And Label7.Width >= a And Label9.Width >= c And Label10.Width >= d Then
Label21.Visible = False
Label20.Visible = True
Label19.Visible = False
ElseIf Label8.Width = b Then
Label21.Visible = False
Label20.Visible = False
Label19.Visible = True
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Dim i As Integer
i = c - Label9.Width
If i < 20 Then
Label9.Width += i
Label24.Visible = False
Label23.Visible = False
Label22.Visible = True
Timer3.Enabled = False
Timer4.Enabled = True
End If
If Label9.Width < c Then
Label9.Width += 10
Label24.Visible = True
Label23.Visible = False
Label22.Visible = False
Timer3.Enabled = False
Timer4.Enabled = True
Else
&nbnbsp; Label27.Visible = True
Label26.Visible = False
Label25.Visible = False
Timer4.Enabled = False
Timer1.Enabled = True
Else
Label27.Visible = False
Label26.Visible = False
Label25.Visible = True
Timer4.Enabled = False
Timer1.Enabled = True
End If
If Label7.Width >= a Then
Label18.Visible = False
Label17.Visible = False
Label16.Visible = True
Else
Label18.Visible = False
Label17.Visible = True
Label16.Visible = False
End If
If Label10.Width < d And Label7.Width >= a And Label8.Width >= b And Label9.Width >= c Then
Label27.Visible = False
Label26.Visible = True
Label25.Visible = False
ElseIf Label10.Width = d Then
Label27.Visible = False
Label26.Visible = False
Label25.Visible = True
End If
End Sub
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
四、結果與討論
1優先權調度算法結果圖
2 輪轉法的程序結果圖
5.結果分析:
經選擇調度算法后,每種調度算法都能成功地執行。所以,本次課程設計成功。 五、參考文獻
1、《Windows操作系統原理》 陳向群等著 機械工業出版社
2、《VisualBasic.Net程序設計》馮博琴 崔舒寧著 清華大學出版社
3、《操作系統 原理·技術與編程》 蔣靜, 徐志偉著 機械工業出版社
【操作系統課程設計報告模擬進程調度程序(一)】相關文章:
利用進程間通信實現程序自我保護分析03-09
基于Vxworks實時操作系統的串口通信程序設計與實現03-18
I2C總線在uClinux操作系統下的驅動程序設計11-22
組件機制與操作系統的實現03-18
人文導向牽動每一個國家全面拓展進程03-07
哈姆雷特:現代化進程的祭品03-08
分枝結構的程序設計 (一)03-07
C語言程序設計 (一)12-26