狠狠干影院/欧美午夜电影在线观看/高黄文/国产精品一区二区在线观看完整版

操作系統課程,實驗報告完整版

| 瀏覽次數:

 中 南 大 學

 《操作系統》實驗報告 姓

 名 : 孫

  福

 星 專

 業 班

 級: 軟件 1006 班 學

 號 :

 完 完

 成

 日

 期:

  2011 、11 、22

  進程調度與內存管理 一、

 實驗目的 在采用多道程序設計的系統中,往往有若干個進程同時處于就緒狀態。當就續進程個數大于處理器數時,就必須依照某種策略來決定哪些進程優先占用處理器。實驗模擬實現處理機調度,以加深了解處理機調度的工作,并體會優先級與時間片輪轉調度算法的具體實施方法。幫助了解在不同的存儲管理方式下,應怎樣實現主存空間的分配與回收。

 二、實驗要求

  1、可隨機輸入若干進程,并按優先權排序;

  2、從就緒隊首選進程運行:優先權-1/要求運行時間-1

 要求運行時間=0 時,撤銷該進程 3、重新排序,進行下輪調度。

 4、可隨時增加進程; 5、規定道數,設置后備隊列與掛起狀態。若內存中進程少于規定道數,可自動從后備隊列調度一作業進入。被掛起進程入掛起隊列,設置解掛功能用于將指定掛起進程解掛入

 就緒隊列。

 6、每次調度后,顯示各進程狀態。

 7、自行假設主存空間大小,預設操作系統所占大小并構造未分分區表;

 表目內容:起址、長度、狀態(未分/空表目)

  8、結合以上實驗,PCB 增加為: {PID,要求運行時間,優先權,狀態,所需主存大小,主存起始位置,

  PCB 指針}

  9、采用最先適應算法分配主存空間; 10、進程完成后,回收主存,并與相鄰空閑分區合并。

 11、采用圖形界面; 三、實驗內容 選擇一個調度算法,實現處理機調度。

 1、設計一個按優先權調度算法實現處理機調度的程序; 2、設計按時間片輪轉實現處理機調度的程序。

 3、主存儲器空間的分配與回收。在可變分區管理方式下,采用最先適應算法實現主存空間的分配與回收。

 四、實驗原理 該模擬系統采用 java 語言實現,要實現的功能有新建進程、進程調度、掛起進程、解掛進程、刪除進程,道數與時間片大小可以由用戶自己調整,有兩種調度策略:按優先權調度與按時間片輪轉調度。每個進程可能有 5 種狀態:新建(new)、就緒(ready)、運行(running)、阻塞(waiting)、掛起(suspend)。每個狀態都有一個隊列用來存放處于該狀態的進程,不同的調度策略采用不同的隊列實現。當創建進程時,如果內存中的進程數還沒達到規定道數,則將新建進程插入就緒隊列,如果內存中進程數已經達到規定道數,則插到后備隊列,后備隊列中的進程的狀態為 new。CPU 每次調度時都從就緒隊列中取進程,在進程執行過程中如果下一個操作時 IO 操作,則將進程插入到 waiting 隊列。在系統運行過程中可以執行進程掛起操作,但執行的掛起操作時系統自動暫停運行,在彈出窗口選擇要掛起的進程后,將選中的進程從原來的隊列中刪除并插入到掛起隊列。進行解掛操作時將選中的進程從掛起隊列中刪除并插入該進程原來所處的隊列。

 ? 按優先級調度:

  當選擇按優先權調度時,所有隊列都采用優先隊列,優先隊列采用一個有序鏈表實現,進程的優先權值越大代表優先級越高,優先隊列中的進程按優先權從大到小排列,當新進程插入時根據該進程的優先權插入到隊列中的合適位置,插入后保持隊列按優先權從大到小排列,如果新進程與隊列中某個進程優先權值相等,則該新進程插到那個進程后面,以遵循先來先服務的規則。當要從隊列中取出進程時總就是取隊列中第一個進程,因為該進程的優先級最高。

 ? 按時間片輪轉調度: 當選擇按時間片輪轉調度時,所有隊列都采用先進先出隊列,先進先出隊列采用一個普通單向鏈表實現,當新進程插入時插入到隊列的末尾,當要取進程時取隊首進程,這樣就實現了先進先出。

 ? 內存管理 該實驗基于實驗一完成,核心就是內存的分配與回收,在實驗一的基礎上增加內存管理部分,在新建進程的時候增加一個輸入內存大小的輸入框,在進程進入內存時要分配內存,在進程銷毀時要回收內存,如果進入內存時內存不足,則將進程插入到后備隊列等待下次調度。系統維護一個內存表,每個表項代表一個空間,每個空間保存了該空間的起始地址與空間大小以及空間使用狀態。初始時只有一個空間,當 CPU 啟動時要分配內存,內存分配采用最先適應算法。回收內存時如果有相鄰空閑空間,則要進行空閑空間合并。

 } } 五 、 源代碼及截圖: 1、divDTO:

 public class divDTO {

 private int divBase;

 private int length;

 private int divFlag;

 public divDTO(int divBase,int length,int divFlag)

 {

  this、divBase = divBase;

  this、divFlag = divFlag;

  this、length = length;

 }

 public divDTO()

 {

  }

 public void setDivBase(int base)

 {

  this、divBase = base;

 }

 public int getDivBase()

 {

  return this、divBase;

 }

 public void setLength(int length)

 {

  this、length = length;

 }

 public int getLength()

 {

  return this、length;

 }

 public void setDivFlag(int flag)

 {

  this、divFlag = flag;

 }

 public int getDivFalg()

 {

  return this、divFlag;

 } } 2、PcbDTO: public class PcbDTO {

 static final int Running = 1;

 static final int Ready = 2;

 static final int Waiting = 3;

 private String processName;

 private int runTime;

 private int prority;

 private int processState;

 private int base;

 private int limit;

 private int pcbFlag;

 public PcbDTO(String name, int time,int pro,int base,int limit)

 {

  this、processName = name;

  this、runTime = time;

  this、prority = pro;

 this、processState = 0;

  this、limit = limit;

  this、base = base;

 }

 public PcbDTO()

 {this、pcbFlag = 0;}

 public void setProcessName(String name)

 {

  this、processName = name;

 }

 public String getProcessName()

 {

  return processName;

 }

 public void setRunTime(int time)

 {

  this、runTime = time;

 }

 public int getRunTime()

 {

  return this、runTime;

 }

 public void setPrority(int prority)

 {

  this、prority = prority;

 }

 public int getPrority()

 {

  return this、prority;

 }

 public void setProcessState(int state)

 {

  this、processState = state;

 }

 public String getProcessState()

 {

  String s = new String();

  if(this、processState == 1)

  {

 s = "running";

  }

  else if(this、processState == 2)

  {

 s = "ready";

 }

  else if(this、processState == 3)

  {

 s = "waiting";

  }

  return s;

 }

 public int getBase()

 {

  return this、base;

 }

 public void setBase(int base)

 {

  this、base = base;

 }

 public void setLimit(int limit)

 {

  this、limit = limit;

 }

 public int getLimit()

 {

  return this、limit;

 } } 3、

 import javax、swing、*; import java、util、*; import java、awt、*; import java、awt、event、*; import javax、swing、event、*; public class MainFrame {

 private JList readyList;

 private JList waitingList;

 private JList jobList;

 private JButton susButton;

 private JButton relaxButton;

 private JButton startButton;

 private JButton newButton;

 private JLabel nameLabel;

 private JLabel prorityLabel;

 private JLabel timeLabel;

 private JLabel jobLabel;

 private JLabel readyLabel;

 private JLabel waitingLabel;

 private JLabel runningLabel;

  private JLabel spaceLabel;

 private JLabel divLabel;

 private JLabel allocLabel;

 private JTable readyTable;

 private JTable runningTable;

 private JTable divTable;

 private JTable allocTable;

 private JTextField nameText;

 private JTextField timeText;

 private JTextField spaceText;

 private JComboBox prorityCom;

 private JPanel newPanel;

 private JPanel waitingPanel;

 private JPanel readyPanel;

 Vector jobVectorName;

 Vector jobDtoVector;

 Vector waitingVectorName;

 Vector waitingDtoVector;

 PcbDTO[] readyDtoArray;

 PcbDTO[] newDtoArray;

 divDTO[] divDtoArray;

 PcbDTO[] newSort;

 Object[][] readydata;

 Object[][] runningdata;

 Object[][] divdata;

 Object[][] allocdata;

 int first;

 int end;

 int point;

 PcbDTO a;

 public MainFrame() {

  a = new PcbDTO();

  first = 0;

  end = 0;

  point = 0;

  JFrame jf = new JFrame("進程調度-ws");

  Container c = jf、getContentPane();

  c、setLayout(null);

  // c、setBackground(Color、pink);

  newPanel = new JPanel();

  newPanel、setLayout(null);

  waitingPanel = new JPanel();

  waitingPanel、setLayout(null);

  // waitingPanel、setBackground(Color、pink);

 readyPanel = new JPanel();

  readyPanel、setLayout(null);

  susButton = new JButton("掛起");

  relaxButton = new JButton("釋放");

  startButton = new JButton("開始");

  newButton = new JButton("新建進程");

  nameLabel = new JLabel("進程名");

  prorityLabel = new JLabel("優先級");

  timeLabel = new JLabel("運行時間");

  jobLabel = new JLabel("后備隊列");

  readyLabel = new JLabel("就緒隊列");

  waitingLabel = new JLabel("等待隊列");

  runningLabel = new JLabel("運行進程");

  spaceLabel = new JLabel("需要空間");

  divLabel = new JLabel("未分分區表");

  allocLabel = new JLabel("內存分配表");

  nameText = new JTextField();

  timeText = new JTextField();

  spaceText = new JTextField();

  prorityCom = new JComboBox();

  prorityCom、setToolTipText("優先級");

  readyDtoArray = new PcbDTO[6];

  newSort = new PcbDTO[6];

  for (int i = 0; i < 6; i++) {

 newSort[i] = new PcbDTO();

  }

  newDtoArray = new PcbDTO[100];

  jobDtoVector = new Vector();

  jobVectorName = new Vector();

  waitingDtoVector = new Vector();

  waitingVectorName = new Vector();

  divDtoArray = new divDTO[20];

  for (int i = 0; i < 20; i++) {

 divDtoArray[i] = new divDTO();

 divDtoArray[i]、setDivFlag(0);

  }

  divDtoArray[0]、setDivFlag(1);

  divDtoArray[0]、setDivBase(20);

  divDtoArray[0]、setLength(180);

  readydata = new Object[6][4];

  runningdata = new Object[2][3];

  divdata = new Object[20][3];

  allocdata = new Object[20][3];

  String[] col1 = { "進程", "時間", "優先級", "狀態" };

 String[] col2 = { "進程", "時間", "優先級" };

  String[] col3 = { "起址", "長度", "狀態" };

  String[] col4 = { "起址", "長度", "占用進程" };

  readyTable = new JTable(readydata, col1);

  // readyTable、setEnabled(false);

  runningTable = new JTable(runningdata, col2);

  runningTable、setRowHeight(22);

  runningTable、setEnabled(false);

  allocTable = new JTable(allocdata, col4);

  allocTable、setEnabled(false);

  divTable = new JTable(divdata, col3);

  divTable、setEnabled(false);

  divTable、setValueAt(String、valueOf(20), 0, 0);

  divTable、setValueAt(String、valueOf(180), 0, 1);

  divTable、setValueAt(String、valueOf(1), 0, 2);

  JScrollPane runningSP = new JScrollPane();

  JScrollPane readySP2 = new JScrollPane();

  JScrollPane divSP = new JScrollPane();

  JScrollPane allocSP = new JScrollPane();

  runningSP、getViewport()、add(runningTable);

  readySP2、getViewport()、add(readyTable);

  divSP、getViewport()、add(divTable);

  allocSP、getViewport()、add(allocTable);

  // int []prorityArray = new int[10];

  for (int i = 0; i < 10; i++) {

 prorityCom、addItem(i);// prorityArray[i] = i;

  }

  jobList = new JList();

  waitingList = new JList();

  JScrollPane readySP = new JScrollPane(readyList);

  JScrollPane jobSP = new JScrollPane(jobList);

  JScrollPane waitingSP = new JScrollPane(waitingList);

  newPanel、setSize(450, 100);

  newPanel、setLocation(0, 0);

  nameLabel、setSize(80, 20);

  nameLabel、setLocation(10, 5);

  nameText、setSize(100, 25);

  nameText、setLocation(10, 30);

  prorityLabel、setSize(80, 20);

  prorityLabel、setLocation(120, 5);

  prorityCom、setSize(100, 25);

  prorityCom、setLocation(120, 30);

  timeLabel、setSize(80, 20);

  timeLabel、setLocation(230, 5);

 timeText、setSize(100, 25);

  timeText、setLocation(230, 30);

  spaceLabel、setSize(80, 20);

  spaceLabel、setLocation(340, 5);

  spaceText、setSize(100, 25);

  spaceText、setLocation(340, 30);

  newButton、setSize(100, 20);

  newButton、setLocation(320, 70);

  waitingPanel、setSize(190, 410);

  waitingPanel、setLocation(0, 100);

  jobLabel、setSize(100, 20);

  jobLabel、setLocation(10, 2);

  jobSP、setSize(180, 105);

  jobSP、setLocation(10, 25);

  waitingLabel、setSize(100, 20);

  waitingLabel、setLocation(10, 129);

  waitingSP、setSize(180, 105);

  waitingSP、setLocation(10, 150);

  divLabel、setSize(100, 20);

  divLabel、setLocation(10, 253);

  divSP、setSize(180, 113);

  divSP、setLocation(10, 273);

  relaxButton、setSize(80, 20);

  relaxButton、setLocation(110, 388);

  readyPanel、setSize(260, 410);

  readyPanel、setLocation(190, 100);

  readyLabel、setSize(100, 22);

  readyLabel、setLocation(10, 2);

  allocLabel、setSize(100, 20);

  allocLabel、setLocation(10, 232);

  startButton、setSize(80, 20);

  startButton、setLocation(177, 388);

  susButton、setSize(80, 20);

  susButton、setLocation(95, 388);

  readySP2、setSize(250, 117);

  readySP2、setLocation(10, 25);

  runningLabel、setLocation(10, 142);

  runningLabel、setSize(100, 20);

  runningSP、setSize(250, 65);

  runningSP、setLocation(10, 167);

  allocSP、setSize(250, 130);

  allocSP、setLocation(10, 255);

  c、add(newPanel);

  newPanel、add(nameLabel);

 newPanel、add(nameText);

  newPanel、add(prorityLabel);

  newPanel、add(prorityCom);

  newPanel、add(timeText);

  newPanel、add(timeLabel);

  newPanel、add(newButton);

  newPanel、add(spaceLabel);

  newPanel、add(spaceText);

  c、add(waitingPanel);

  waitingPanel、add(jobLabel);

  waitingPanel、add(jobSP);

  waitingPanel、add(waitingLabel);

  waitingPanel、add(waitingSP);

  waitingPanel、add(divLabel);

  waitingPanel、add(divSP);

  waitingPanel、add(relaxButton);

  c、add(readyPanel);

  readyPanel、add(readyLabel);

  readyPanel、add(allocLabel);

  readyPanel、add(runningLabel);

  readyPanel、add(startButton);

  readyPanel、add(susButton);

  readyPanel、add(allocSP);

  readyPanel、add(runningSP);

  readyPanel、add(readySP2);

  jf、setSize(470, 550);

  jf、setDefaultCloseOperation(JFrame、DISPOSE_ON_CLOSE);

  jf、setLocationRelativeTo(null);

  jf、setVisible(true);

  startButton、addActionListener(new MyActionListener());

  newButton、addActionListener(new MyActionListener());

  susButton、addActionListener(new MyActionListener());

  relaxButton、addActionListener(new MyActionListener());

 }

 public void sus() {

  try {

 Thread、sleep(1000);

  } catch (Exception ex) {

  }

 }

 class MyActionListener implements ActionListener {

  public void actionPerformed(ActionEvent e) {

 int count = 0;

 PcbDTO test = new PcbDTO();

  JButton jb = (JButton) e、getSource();

 int max = -1;

 if (jb == startButton) {

  // while(true)

  // {

  int runAllocFlag = -1;

  if ((String) runningTable、getValueAt(0, 0) == null

  || (String) runningTable、getValueAt(0, 0) == "") {

 try {

  Thread、sleep(0);

 } catch (Exception ex) {

 }

 // System、out、println("到 3");

 for (int j = first; j != end;) {

  if (!readyDtoArray[j]、getProcessState()、equals(

  "waiting")) {

 max = j;

 break;

  }

  j = (j + 1) % 6;

 }

 for (int j = first; j % 6 != end;) {

  if (!readyDtoArray[j]、getProcessState()、equals(

  "waiting")) {

 if (readyDtoArray[j] 、 getPrority() > readyDtoArray[max]

 、getPrority()) {

  max = j;

 }

  }

  j = (j + 1) % 6;

 }

 if (max >= 0) {

  a = readyDtoArray[max];

  readyDtoArray[max] = readyDtoArray[first];

  readyDtoArray[first] = a;

  readyTable、setValueAt(

  readyDtoArray[max]、getProcessName(), max, 0);

  readyTable 、 setValueAt(readyDtoArray[max] 、getRunTime(),

  max, 1);

  readyTable 、 setValueAt(readyDtoArray[max] 、getPrority(),

  max, 2);

 readyTable、setValueAt(

  readyDtoArray[max]、getProcessState(), max, 3);

  readyTable、setValueAt("", first, 0);

  readyTable、setValueAt("", first, 1);

  readyTable、setValueAt("", first, 2);

  readyTable、setValueAt("", first, 3);

  runningTable、setValueAt(a、getProcessName(), 0, 0);

  runningTable、setValueAt(a、getRunTime(), 0, 1);

  runningTable、setValueAt(a、getPrority(), 0, 2);

  readyDtoArray[first]、setRunTime(readyDtoArray[first]

  、getRunTime() - 1);

  if (0 != readyDtoArray[first]、getPrority()) {

 readyDtoArray[first]

 、setPrority(readyDtoArray[first]

 、getPrority() - 1);

  }

  first = (first + 1) % 6;

 } else {

  System、out、println("cpu 等待中……");

 }

  } else {

 /*

  * try { Thread、sleep(2000); } catch(InterruptedException

  * e1) { System、out、println(e1); }

  */

 // System、out、println("到 1");

 runningTable、setValueAt("", 0, 0);

 runningTable、setValueAt("", 0, 1);

 runningTable、setValueAt("", 0, 2);

 // 如果運行時間為 0 則撤銷進程,否則將進程重新添加到就緒隊列中

 if (a、getRunTime() <= 0) {

  // 收回內存空間

  for (int i = 0; i < point; i++) {

 if (newSort[i]、getBase() >= a、getBase()) {

  newSort[i] = newSort[i + 1];

 }

  }

  point--;

  // 設置內存分配表的內容

  for (int i = 0; i < point; i++) {

 allocTable、setValueAt(

 String、valueOf(newSort[i]、getBase()), i, 0);

 allocTable

  、setValueAt(String、valueOf(newSort[i]

 、getLimit()), i, 1);

 allocTable 、 setValueAt(newSort[i] 、getProcessName(),

 i, 2);

  }

  allocTable、setValueAt("", point, 0);

  allocTable、setValueAt("", point, 1);

  allocTable、setValueAt("", point, 2);

  // 把收回的內存加入到記錄未分分區的數組

  int memoryEnd = 0;

  int location = 0;

  int up = -1;//

  int down = -1;

  for (int i = 0; i < 20; i++) {

 if (divDtoArray[i]、getDivFalg() == 1) {

  memoryEnd = divDtoArray[i]、getDivBase()

  + divDtoArray[i]、getLength();

  if (memoryEnd == a、getBase()) {

 up = i;

  }

  if (divDtoArray[i]、getDivBase() == (a、getBase() + a

  、getLimit())) {

 down = i;

  }

 }

  }

  if (up >= 0 && down >= 0) {

 divDtoArray[up]

 、setLength((divDtoArray[up]、getLength()

 + a、getLimit() + divDtoArray[down]

 、getLength()));

 divDtoArray[down]、setDivFlag(0);

 for (int i = (down + 1); i < 20; i++) {

  if (divDtoArray[i]、getDivFalg() == 1) {

 divDtoArray[i - 1]

 、setDivBase(divDtoArray[i]

 、getDivBase());

 divDtoArray[i - 1]、setDivFlag(1);

 divDtoArray[i - 1] 、setLength(divDtoArray[i]

 、getLength());

 divDtoArray[i]、setDivFlag(0);

 } else {

 divTable、setValueAt("", i - 1, 0);

 divTable、setValueAt("", i - 1, 1);

 divTable、setValueAt("", i - 1, 2);

 break;

  }

 }

  } else if (up >= 0 && down < 0) {

 divDtoArray[up]、setLength((divDtoArray[up]

 、getLength() + a、getLimit()));

  } else if (up < 0 && down >= 0) {

 divDtoArray[down]、setLength((divDtoArray[down]

 、getLength() + a、getLimit()));

 divDtoArray[down]、setDivBase(a、getBase());

  } else if (up < 0 && down < 0) {

 for (int i = 0; i < 20; i++) {

  if (divDtoArray[i]、getDivBase() > a、getBase()

  || divDtoArray[i]、getDivFalg() == 0) {

 location = i;

 break;

  }

 }

 for (int i = 20; i > location; i--) {

  if (divDtoArray[i - 1]、getDivFalg() == 1) {

 divDtoArray[i]

 、setDivBase(divDtoArray[i - 1]

 、getDivBase());

 divDtoArray[i]、setDivFlag(1);

 divDtoArray[i]、setLength(divDtoArray[i - 1]

 、getLength());

  }

 }

 divDtoArray[location]、setDivBase(a、getBase());

 divDtoArray[location]、setDivFlag(1);

 divDtoArray[location]、setLength(a、getLimit());

  }

  // 設置未分分區表的內容

  for (int i = 0; i < 20; i++) {

 if (divDtoArray[i]、getDivFalg() == 1) {

  divTable、setValueAt(String

  、 valueOf(divDtoArray[i] 、getDivBase()),

  i, 0);

 divTable、setValueAt(String

  、valueOf(divDtoArray[i]、getLength()),

  i, 1);

  divTable、setValueAt(String

  、 valueOf(divDtoArray[i] 、getDivFalg()),

  i, 2);

 }

  }

  if (!jobDtoVector、isEmpty()) {

 int runLength = 0;

 PcbDTO jobToReady = (PcbDTO) jobDtoVector

 、elementAt(0);

 for (int i = 0; i < 20; i++) {

  if (divDtoArray[i]、getDivFalg() == 1) {

 if (divDtoArray[i] 、 getLength() >= jobToReady

 、getLimit()) {

  runAllocFlag = i;

  break;

 }

  }

 }

 if (runAllocFlag >= 0) {

  jobDtoVector、removeElementAt(0);

  jobVectorName、remove(jobVectorName

  、 indexOf(jobToReady 、getProcessName()));

  jobList、setListData(jobVectorName);

  jobToReady、setProcessState(PcbDTO、Ready);

  jobToReady、setBase(divDtoArray[runAllocFlag]

  、getDivBase());

  runLength = divDtoArray[runAllocFlag]

  、getLength() - jobToReady、getLimit();

  if (runLength == 0) {

 int i = runAllocFlag;

 divDtoArray[i]、setDivFlag(0);

 for (; i < 19; i++) {

  if (divDtoArray[i + 1]、getDivFalg() == 1) {

 divDtoArray[i] = divDtoArray[i + 1];

 divDtoArray[i + 1]、setDivFlag(0);

  }

 divTable、setValueAt(String

  、valueOf(divDtoArray[i]

  、getDivBase()), i, 0);

  divTable、setValueAt(String

  、valueOf(divDtoArray[i]

  、getLength()), i, 1);

  divTable、setValueAt(String

  、valueOf(divDtoArray[i]

  、getDivFalg()), i, 2);

 }

 divTable、setValueAt(String

 、valueOf(divDtoArray[i]

 、getDivFalg()), i, 2);

  } else if (runLength > 0) {

 int c2 = divDtoArray[runAllocFlag]

 、getDivBase()

 + jobToReady、getLimit();

 divDtoArray[runAllocFlag]、setDivBase(c2);

 divDtoArray[runAllocFlag]

 、setLength(runLength);

 divTable、setValueAt(String、valueOf(c2),

 runAllocFlag, 0);

 divTable、setValueAt(

 String、valueOf(runLength),

 runAllocFlag, 1);

 divTable、setValueAt(String

 、valueOf(divDtoArray[runAllocFlag]

 、getDivFalg()),

 runAllocFlag, 2);

  }

  readyDtoArray[end] = jobToReady;

  readyTable、setValueAt(

  jobToReady、getProcessName(), end, 0);

  readyTable 、 setValueAt(jobToReady 、getRunTime(),

  end, 1);

  readyTable 、 setValueAt(jobToReady 、getPrority(),

  end, 2);

  readyTable、setValueAt(

  jobToReady、getProcessState(), end, 3);

  end = (end + 1) % 6;

  int runi = 0;// 用于記錄當前新生成的 PcbDTO 對象

 應該插入到 newSort 中的位置

  for (; runi < point; runi++) {

 if (jobToReady、getBase() < newSort[runi]

 、getBase()) {

  break;

 }

  }

  // 如果不就是插入到數組末尾,則把比它大的都向后挪一位并設置 JTable 中的顯示

  for (int i = point; i > runi; i--) {

 newSort[i] = newSort[i - 1];

 allocTable、setValueAt(String

 、valueOf(newSort[i]、getBase()), i,

 0);

 allocTable、setValueAt(String

 、valueOf(newSort[i]、getLimit()), i,

 1);

 allocTable、setValueAt(

 newSort[i] 、 getProcessName(), i, 2);

  }

  // 插入新生成的對象

  newSort[runi] = jobToReady;

  allocTable、setValueAt(

  String、valueOf(jobToReady、getBase()),

  runi, 0);

  allocTable、setValueAt(

  String 、 valueOf(jobToReady 、getLimit()),

  runi, 1);

  allocTable、setValueAt(

  jobToReady、getProcessName(), runi, 2);

  point++;

 }

  }

 } else {

  readyDtoArray[end] = a;

  readyTable、setValueAt(a、getProcessName(), end, 0);

  readyTable、setValueAt(a、getRunTime(), end, 1);

  readyTable、setValueAt(a、getPrority(), end, 2);

  readyTable、setValueAt(a、getProcessState(), end, 3);

  end = (end + 1) % 6;

 }

 }

  // }

 } else if (jb == newButton) {

  int newAllocFlag = -1;

  int newLength = 0;

  if (nameText、getText()、trim()、length() == 0) {

 JOptionPane、showMessageDialog(null, "進程名不能為空!");

  } else if (timeText、getText()、trim()、length() == 0) {

 JOptionPane、showMessageDialog(null, "運行時間不能為空");

  } else if (spaceText、getText()、trim()、length() == 0) {

 JOptionPane、showMessageDialog(null, "空間不能為空");

  } else {

 test、setRunTime(Integer、parseInt(timeText、getText()));

 test、setLimit(Integer、parseInt(spaceText、getText()));

 String s = prorityCom、getSelectedItem()、toString();

 test、setPrority(Integer、parseInt(s));

 test、setProcessName(nameText、getText()、trim());

 newDtoArray[count] = test;

 jobDtoVector、add(newDtoArray[count]);

 jobVectorName、add(newDtoArray[count]、getProcessName());

 jobList、setListData(jobVectorName);

 count++;

 nameText、setText("");

 timeText、setText("");

 spaceText、setText("");

 PcbDTO b = (PcbDTO) jobDtoVector、elementAt(0);

 for (int i = 0; i < 20; i++) {

  if (divDtoArray[i]、getDivFalg() == 1) {

 if (divDtoArray[i]、getLength() >= b、getLimit()) {

  newAllocFlag = i;

  break;

 }

  }

 }

 // 在就緒隊列未滿且內存有足夠空間時將后備隊列jobDtoVetor中的對象添加到就緒隊列中

 if ((end + 2) % 6 != first && newAllocFlag >= 0) {

  jobDtoVector、removeElementAt(0);

  b、setProcessState(PcbDTO、Ready);

  b、setBase(divDtoArray[newAllocFlag]、getDivBase());

  newLength = divDtoArray[newAllocFlag]、getLength()

  - b、getLimit();

  if (newLength == 0) {

 int i = newAllocFlag;

  divDtoArray[i]、setDivFlag(0);

 for (; i < 19; i++) {

  if (divDtoArray[i + 1]、getDivFalg() == 1) {

 divDtoArray[i] = divDtoArray[i + 1];

 divDtoArray[i + 1]、setDivFlag(0);

  }

  divTable、setValueAt(String

  、 valueOf(divDtoArray[i] 、getDivBase()),

  i, 0);

  divTable、setValueAt(String

  、valueOf(divDtoArray[i]、getLength()),

  i, 1);

  divTable、setValueAt(String

  、 valueOf(divDtoArray[i] 、getDivFalg()),

  i, 2);

 }

 divTable、setValueAt(

 String 、 valueOf(divDtoArray[i] 、getDivFalg()),

 i, 2);

  } else if (newLength > 0) {

 int c1 = divDtoArray[newAllocFlag]、getDivBase()

 + b、getLimit();

 divDtoArray[newAllocFlag]、setDivBase(c1);

 divDtoArray[newAllocFlag]、setLength(newLength);

 divTable、setValueAt(String、valueOf(c1),

 newAllocFlag, 0);

 divTable、setValueAt(String、valueOf(newLength),

 newAllocFlag, 1);

 divTable、setValueAt(String

 、valueOf(divDtoArray[newAllocFlag]

 、getDivFalg()), newAllocFlag, 2);

  }

  readyDtoArray[end] = b;

  jobVectorName、remove(jobVectorName、indexOf(b

  、getProcessName()));

  readyTable、setValueAt(b、getProcessName(), end, 0);

  readyTable、setValueAt(b、getRunTime(), end, 1);

  readyTable、setValueAt(b、getPrority(), end, 2);

  readyTable、setValueAt("ready", end, 3);

  end = (end + 1) % 6;

  int newi = 0;// 用于記錄當前新生成的 PcbDTO 對象應該插入

 到 newSort 中的位置

  for (; newi < point; newi++) {

 if (b、getBase() < newSort[newi]、getBase()) {

  break;

 }

  }

  // 如果不就是插入到數組末尾,則把比它大的都向后挪一位并設置 JTable 中的顯示

  for (int i = point; i > newi; i--) {

 newSort[i] = newSort[i - 1];

 allocTable、setValueAt(

 String、valueOf(newSort[i]、getBase()), i, 0);

 allocTable

 、setValueAt(String、valueOf(newSort[i]

 、getLimit()), i, 1);

 allocTable 、 setValueAt(newSort[i] 、getProcessName(),

 i, 2);

  }

  // 插入新生成的對象

  newSort[newi] = b;

  allocTable、setValueAt(String、valueOf(b、getBase()),

  newi, 0);

  allocTable、setValueAt(String、valueOf(b、getLimit()),

  newi, 1);

  allocTable、setValueAt(b、getProcessName(), newi, 2);

  point++;

 }

  }

 } else if (jb == susButton) {

  if (readyDtoArray[readyTable、getSelectedRow()] != null) {

 if (!readyDtoArray[readyTable、getSelectedRow()]

 、getProcessState()、equals("waiting")) {

  readyDtoArray[readyTable、getSelectedRow()]

  、setProcessState(PcbDTO、Waiting);

  readyTable、setValueAt("waiting",

  readyTable、getSelectedRow(), 3);

  waitingDtoVector、add(readyDtoArray[readyTable

  、getSelectedRow()]);

  waitingVectorName、add(readyDtoArray[readyTable

  、getSelectedRow()]、getProcessName());

  waitingList、setListData(waitingVectorName);

 } else {

 System、out、println("已掛起");

 }

  } else {

 JOptionPane、showMessageDialog(null, "請選擇要掛起的進程");

 // System、out、println("請選擇要掛起的進程");

  }

 } else if (jb == relaxButton) {

  String s = (String) waitingList、getSelectedValue();

  if (s != null) {

 waitingVectorName、remove(s);

 PcbDTO p = new PcbDTO();

 for (int i = 0; i < waitingDtoVector、size(); i++) {

  p = (PcbDTO) waitingDtoVector、elementAt(i);

  if (s、equals(p、getProcessName())) {

 p、setProcessState(PcbDTO、Ready);

 waitingDtoVector、remove(p);

 break;

  }

 }

 for (int i = 0; i < 6; i++) {

  if (s、equals(readyDtoArray[i]、getProcessName())) {

 readyTable、setValueAt("ready", i, 3);

 break;

  }

 }

 waitingList、setListData(waitingVectorName);

  } else {

 JOptionPane、showMessageDialog(null, "請選擇要解掛的進程");

 // System、out、println("沒有選擇的進程");

  }

 }

  }

 }

 public static void main(String args[]) {

  new MainFrame();

 } }

 運行截圖:

 運行后開始界面

 輸入進程后界面

  建立后掛起

  掛起后各內存分配

  解掛

 操作異常時的提示 六、實驗總結

 這個程序,我參考了課本,互聯網以及相關資料。由于對 java 語言比較陌生,因此雖然這個試驗比較簡單,并且不就是我一人獨立完成,但也花費了我大量時間。通過這個實驗,我更加形象的了解了進程的調度過程,加深了對于優先權調度與時間片輪轉調度的理解,并不像從前一樣僅僅停留在概念上。除此之外讓我對 java 語言也有了進一步的了解。通過次實驗,我對內存分配與內存回收有了更深刻的了解,我們平時用電腦時簡單的一個動作對內存來說卻要做出如此多的反應,找到一個空閑并且大小合適的空間進行內存分配。本次實驗使我對內存分配的了解有了很大的幫助。在這次編程中我也出現了很多程序上的簡單錯誤,都就是因為我動手寫程序比較少造成的,這也讓我了解到,要多次鍛煉才能順心順手。

推薦訪問: 完整版 操作系統 課程

【操作系統課程,實驗報告完整版】相關推薦

工作總結最新推薦

NEW