操作系统实验—进程调度的设计与实现实验报告

一、 实验目的

1.  综合应用下列知识点设计并实现操作系统的进程调度:邻接表,布尔数组,非阻塞输入,图形用户界面GUI ,进程控制块,进程状态转换,多级反馈队列进程调度算法。

2.  加深理解操作系统进程调度的过程。

3.  加深理解多级反馈队列进程调度算法。

二、 实验内容与主要设计思想

1.  采用一种熟悉的语言,如C、PASCAL或C++等,编制程序,最好关键代码采用C/C++ ,界面设计可采用其它自己喜欢的语言。

2.  采用多级反馈队列调度算法进行进程调度。

3.  每个进程对应一个PCB。在 PCB 中包括进程标识符pid、进程的状态标识status、进程优先级priority、进程的队列指针 next 和表示进程生命周期的数据项life (在实际系统中不包括该项)。

4.  创建进程时即创建一个PCB ,各个进程的pid 都是唯一的,pid 是在1到100 范围内的一个整数。可以创建一个下标为1 到100 的布尔数组,“真”表示下标对应的进程标识号是空闲的,“假”表示下标对应的进程标识号已分配给某个进程。

5.  进程状态status 的取值为“就绪ready”或“运行run ”,刚创建时,状态为“ready”。被进程调度程序选中后变为“run ”。

6.  进程优先级priority 是0 到49范围内的一个随机整数。

7.  进程生命周期life 是1 到5 范围内的一个随机整数。

8.  初始化时,创建一个邻接表,包含50个就绪队列,各就绪队列的进程优先级priority 分别是0 到49。

9.  为了模拟用户动态提交任务的过程,要求动态创建进程。进入进程调度循环后,每次按ctrl+f即动态创建一个进程,然后将该 PCB 插入就绪队列中。按ctrl+q退出进程调度循环。

10. 在进程调度循环中,每次选择优先级最大的就绪进程来执行。将其状态从就绪变为运行,通过延时一段时间来模拟该进程执行一个时间片的过程,然后优先级减半,生命周期减一。设计图形用户界面GUI ,在窗口中显示该进程和其他所有进程的PCB 内容。如果将该运行进程的生命周期不为0 ,则重新把它变为就绪状态,插入就绪队列中;否则该进程执行完成,撤消其PCB 。以上为一次进程调度循环。

三、 d程序的主要流程图

四、 实验心得

1.  通过计数器KillTimer()、ONTIMER()以及在计数器中对函数run()设计并在ONTIMER()中对run函数调用从而完成多级反馈队列运行的模拟。

2.  使用MFC设计界面,可以通过按钮和快捷键进成相关的操作,还充分利用了模板进行简化设计邻接表。

3.  实验中最重要的是在自己无法解决问题的时候,向同学、老师等请教以及利用好网络资源。

4.  在设计的时候,要尽可能考虑到用户的一切可能的操作,提高程序的用户友好性。

五、 主要源程序清单

主要程序MyScheuleDlg.cpp的代码:

// MyScheduleDlg.cpp : implementation file

//

#include "stdafx.h"

#include "MySchedule.h"

#include "MyScheduleDlg.h"

#include <string.h>

#include <iostream>

#include <queue>

#include "PCB.h"

using namespace std;

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

bool flag[100];

queue<PCB> q[50];

/////////////////////////////////////////////////////////////////////////////

// CMyScheduleDlg dialog

CMyScheduleDlg::CMyScheduleDlg(CWnd* pParent /*=NULL*/)

  : CDialog(CMyScheduleDlg::IDD, pParent)

{

  //{{AFX_DATA_INIT(CMyScheduleDlg)

      // NOTE: the ClassWizard will add member initialization here

  //}}AFX_DATA_INIT

  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32

  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CMyScheduleDlg::DoDataExchange(CDataExchange* pDX)

{

  CDialog::DoDataExchange(pDX);

  //{{AFX_DATA_MAP(CMyScheduleDlg)

  DDX_Control(pDX, IDC_LIST1, m_LIST);

  //}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CMyScheduleDlg, CDialog)

  //{{AFX_MSG_MAP(CMyScheduleDlg)

  ON_WM_PAINT()

  ON_WM_QUERYDRAGICON()

  ON_BN_CLICKED(IDC_BUTTON1, OnButton1)

  ON_BN_CLICKED(IDC_BUTTON2, OnButton2)

  ON_BN_CLICKED(IDC_BUTTON3, OnButton3)

  ON_WM_TIMER()

  //}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMyScheduleDlg message handlers

BOOL CMyScheduleDlg::OnInitDialog()

{

  CDialog::OnInitDialog();

  // Set the icon for this dialog.  The framework does this automatically

  //  when the application's main window is not a dialog

  SetIcon(m_hIcon, TRUE);         // Set big icon

  SetIcon(m_hIcon, FALSE);    // Set small icon

 

  // TODO: Add extra initialization here

  m_LIST.DeleteAllItems();      

  while(m_LIST.DeleteColumn(0));// 清空Control List内容

  m_LIST.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); // 添加网格

  m_LIST.SetTextColor(RGB(255,0,255));   // 设置字体颜色

  //m_LIST.InsertColumn(0,"",LVCFMT_LEFT,0);  // 第0列用来定位

  // 插入列,50个优先级列

  for(int i=0;i<50;i++)

  {

      CString s;

      s.Format("%d",i);

      s="优先级"+s;

      m_LIST.InsertColumn(i,s,LVCFMT_LEFT,185);

  }

  for(i=0;i<100;i++)m_LIST.InsertItem(i,"");

  for(i=1;i<=100;i++)

      flag[i]=false;

  for(i=0;i<50;i++)

      addPCB();

  return TRUE;  // return TRUE  unless you set the focus to a control

}

// If you add a minimize button to your dialog, you will need the code below

//  to draw the icon.  For MFC applications using the document/view model,

//  this is automatically done for you by the framework.

void CMyScheduleDlg::OnPaint()

{

  if (IsIconic())

  {

      CPaintDC dc(this); // device context for painting

      SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

      // Center icon in client rectangle

      int cxIcon = GetSystemMetrics(SM_CXICON);

      int cyIcon = GetSystemMetrics(SM_CYICON);

      CRect rect;

      GetClientRect(&rect);

      int x = (rect.Width() - cxIcon + 1) / 2;

      int y = (rect.Height() - cyIcon + 1) / 2;

      // Draw the icon

      dc.DrawIcon(x, y, m_hIcon);

  }

  else

  {

      CDialog::OnPaint();

  }

}

// The system calls this to obtain the cursor to display while the user drags

//  the minimized window.

HCURSOR CMyScheduleDlg::OnQueryDragIcon()

{

  return (HCURSOR) m_hIcon;

}

void CMyScheduleDlg::OnButton1() //创建线程

{

  // TODO: Add your control notification handler code here

  KillTimer(1);

  addPCB();

  UpdateWindow();

  OnButton2();

}

void CMyScheduleDlg::OnButton2()//调度开始

{

  // TODO: Add your control notification handler code here

  SetTimer(1,1000,NULL);//设置定时器

}

void CMyScheduleDlg::OnButton3()

{

  // TODO: Add your control notification handler code here

  KillTimer(1);//移除定时器

}

void CMyScheduleDlg::OnTimer(UINT nIDEvent)

{

  // TODO: Add your message handler code here and/or call default

  run();

  CDialog::OnTimer(nIDEvent);

}

void CMyScheduleDlg::run()//模拟进程执行

{

  PCB pcb;

  CString s1,s2,s3,s4;

  for(int i=49;i>=0;i--)//检测各个队列是否为空

  {

      if(!q[i].empty())break;

  }

  if(i!=-1)

  {

        

      pcb=q[i].front();

      q[i].pop();

      s1.Format("%d",pcb.getpid());

      s2.Format("%d",pcb.getlife());

      pcb.setstatus("run");

      s3="pid:"+s1+" status:"+pcb.getstatus()+" life:"+s2;

      m_LIST.SetTextColor(RGB(0,255,0));   // 设置字体颜色

      m_LIST.SetItemText(0,pcb.getpriority(),s3);

      UpdateWindow();

      Sleep(1000);// 停顿一段时间

      m_LIST.SetTextColor(RGB(255,0,0));   // 设置字体颜色

      for(int j=0;j<=q[i].size();j++)

      {

         s4=m_LIST.GetItemText(j+1,pcb.getpriority());

         m_LIST.SetItemText(j,pcb.getpriority(),s4);

      }

      m_LIST.SetItemText(j,pcb.getpriority(),"");

      UpdateWindow();

      pcb.setpriority(pcb.getpriority()/2);

      pcb.setlife(pcb.getlife()-1);  

      if(pcb.getlife()!=0)

      {         

         s1.Format("%d",pcb.getpid());

         s2.Format("%d",pcb.getlife());

         pcb.setstatus("ready");

         s3="pid:"+s1+" status:"+pcb.getstatus()+" life:"+s2;

         q[pcb.getpriority()].push(pcb); 

         m_LIST.SetItemText(q[pcb.getpriority()].size()-1,pcb.getpriority(),s3);

      }

      else

      {

         flag[pcb.getpid()]=false;

      }

  }

  else

  {  

      KillTimer(1);

      AfxMessageBox("进程调度完毕!");

  }

}

void CMyScheduleDlg::addPCB()//添加PCB

{

  PCB pcb;

  CString s1,s2,s3,s4;

  while(true)

  {

      pcb.setpid(rand()%100+1);

      if(!flag[pcb.getpid()])

      {

         flag[pcb.getpid()]=true;

         break;

      }

  }

  pcb.setstatus("ready");

  pcb.setpriority(rand()%49);

  pcb.setlife(rand()%5+1);

  s1.Format("%d",pcb.getpid());

  s2.Format("%d",pcb.getlife());

  s3="pid:"+s1+" status:"+pcb.getstatus()+" life:"+s2;

  q[pcb.getpriority()].push(pcb);

  m_LIST.SetItemText(q[pcb.getpriority()].size()-1,pcb.getpriority(),s3);

}

BOOL CMyScheduleDlg::PreTranslateMessage(MSG* pMsg) //设置快捷键

{

  // TODO: Add your specialized code here and/or call the base class

  UINT nKeyCode =pMsg->wParam;

  if(pMsg->message==WM_KEYDOWN)

  {

      if(nKeyCode==_T('F')&&(::GetKeyState(VK_CONTROL)&0x8000))

         OnButton1();

      else

      {

         if(nKeyCode==_T('S')&&(::GetKeyState(VK_CONTROL)&0x8000))

             OnButton2();

         else

         {

             if(nKeyCode==_T('Q')&&(::GetKeyState(VK_CONTROL)&0x8000))

                OnButton3();

         }

      }

  }

  return CDialog::PreTranslateMessage(pMsg);

}

相关推荐