处理机调度算法的实现文档格式.docx
《处理机调度算法的实现文档格式.docx》由会员分享,可在线阅读,更多相关《处理机调度算法的实现文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
【实验内容】
1.设定系统中有五个进程,每一个进程用一个进程控制块表示。
2.输入每个进程的“优先数”和“要求运行时间”。
3.为了调度方便,将五个进程按给定的优先数从大到小连成就绪队列。
用一单元指出队列首进程,用指针指出队列的连接情况。
4.处理机调度总是选队首进程运行。
采用动态优先数算法,进程每运行一次优先数就减“1”,同时将运行时间减“1”。
5.若某进程运行时间为零,则将其状态置为“结束”,且退出队列。
6.运行所设计程序,显示或打印逐次被选中进程的进程名,以及进程控制块的动态变化过程。
【实验步骤、过程】
1、程序流程图
2、程序代码
//Deal_ProcessDlg.cpp:
implementationfile
//
#include"
stdafx.h"
Deal_Process.h"
Deal_ProcessDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
#defineN5
//定义进程控制块
typedefstructPCB{
intnum;
intprior;
intrequest_time;
charstate;
}PCB;
//定义全局变量
PCB*pcb=newPCB[5];
intcount=0,_count=0;
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);
//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
IDD)
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
DoDataExchange(CDataExchange*pDX)
CDialog:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//CDeal_ProcessDlgdialog
CDeal_ProcessDlg:
CDeal_ProcessDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CDeal_ProcessDlg:
IDD,pParent)
//{{AFX_DATA_INIT(CDeal_ProcessDlg)
m_count1=_T("
"
);
m_prior1=0;
m_count2=_T("
m_request_time1=0;
m_prior2=_T("
m_request_time2=_T("
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->
LoadIcon(IDR_MAINFRAME);
voidCDeal_ProcessDlg:
//{{AFX_DATA_MAP(CDeal_ProcessDlg)
DDX_Control(pDX,IDC_Start,m_Start);
DDX_Control(pDX,IDC_Confirm,m_Confirm);
DDX_Control(pDX,IDC_Request_Time1,m_Edit2);
DDX_Control(pDX,IDC_Prior1,m_Edit1);
DDX_Control(pDX,IDC_LIST_F,m_list_f);
DDX_Control(pDX,IDC_LIST_W,m_list_w);
DDX_Text(pDX,IDC_Count1,m_count1);
DDX_Text(pDX,IDC_Prior1,m_prior1);
DDX_Text(pDX,IDC_Count2,m_count2);
DDX_Text(pDX,IDC_Request_Time1,m_request_time1);
DDV_MinMaxInt(pDX,m_request_time1,0,100);
DDX_Text(pDX,IDC_Prior2,m_prior2);
DDX_Text(pDX,IDC_Request_Time2,m_request_time2);
BEGIN_MESSAGE_MAP(CDeal_ProcessDlg,CDialog)
//{{AFX_MSG_MAP(CDeal_ProcessDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Confirm,OnConfirm)
ON_BN_CLICKED(IDC_Start,OnStart)
ON_BN_CLICKED(IDC_Exit,OnExit)
//CDeal_ProcessDlgmessagehandlers
BOOLCDeal_ProcessDlg:
OnInitDialog()
OnInitDialog();
//Add"
About..."
menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&
0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<
0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->
AppendMenu(MF_SEPARATOR);
AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'
smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);
//Setbigicon
SetIcon(m_hIcon,FALSE);
//Setsmallicon
//TODO:
Addextrainitializationhere
//添加自己的代码
m_Edit1.ShowWindow(SW_SHOW);
m_Edit2.ShowWindow(SW_SHOW);
m_Confirm.ShowWindow(SW_SHOW);
m_Start.ShowWindow(SW_SHOW);
UpdateWindow();
//提示用户进行初始化
m_count1.Format("
%d"
1);
UpdateData(false);
//首先设定列表控件风格可以整行选中,并具有作业线
DWORDstyle=m_list_w.GetExtendedStyle();
m_list_w.SetExtendedStyle(style|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
DWORDstyle_1=m_list_f.GetExtendedStyle();
m_list_f.SetExtendedStyle(style_1|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
//以下为插入几个列的列标题
CRectrect;
m_list_w.GetClientRect(&
rect);
intnColInterval=rect.Width()/7;
m_list_w.InsertColumn(0,"
进程编号"
LVCFMT_LEFT,nColInterval*2);
m_list_w.InsertColumn(1,"
优先数"
m_list_w.InsertColumn(2,"
要求运行时间"
m_list_w.InsertColumn(3,"
状态"
LVCFMT_LEFT,rect.Width()-nColInterval*6);
m_list_f.InsertColumn(0,"
m_list_f.InsertColumn(1,"
m_list_f.InsertColumn(2,"
m_list_f.InsertColumn(3,"
returnTRUE;
//returnTRUEunlessyousetthefocustoacontrol
OnSysCommand(UINTnID,LPARAMlParam)
if((nID&
0xFFF0)==IDM_ABOUTBOX)
CAboutDlgdlgAbout;
dlgAbout.DoModal();
else
CDialog:
OnSysCommand(nID,lParam);
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
OnPaint()
if(IsIconic())
CPaintDCdc(this);
//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
OnPaint();
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCDeal_ProcessDlg:
OnQueryDragIcon()
return(HCURSOR)m_hIcon;
//初始化N个进程
OnConfirm()
Addyourcontrolnotificationhandlercodehere
UpdateData(true);
pcb[count].num=count+1;
pcb[count].prior=m_prior1;
pcb[count].request_time=m_request_time1;
pcb[count].state='
W'
;
//表示进程处于就绪状态
//将进程信息显示在就绪进程列表中中
CStringstrItem;
strItem.Format(_T("
),count+1);
m_list_w.InsertItem(count,strItem);
//首先插入一行,其中的字符可以任意,也可以为空
),m_prior1);
m_list_w.SetItemText(count,1,strItem);
//设置第二列数据,如此可以设置其它列的数据
),m_request_time1);
m_list_w.SetItemText(count,2,strItem);
m_list_w.SetItemText(count,3,_T("
W"
));
if(count<
N-1)
m_count1.Format("
count+2);
m_prior1=0;
m_request_time1=0;
UpdateData(false);
{
//屏蔽初始化组件
m_Edit1.ShowWindow(SW_HIDE);
m_Edit2.ShowWindow(SW_HIDE);
m_Confirm.ShowWindow(SW_HIDE);
MessageBox("
初始化完成,系统中现有5个进程,点击“开始”按钮,模拟处理机调度"
return;
count++;
/*理论上将N个进程按给定的优先数从大到小连成就绪队列,
但实际上只需找到优先数最高的就绪进程*/
intCDeal_ProcessDlg:
Get_Max_prior_ready_process()
inti,j,max;
for(i=0;
i<
N;
i++)
if(pcb[i].state=='
)
max=i;
break;
if(i==5)
max=i;
for(j=max+1;
j<
j++)
if(pcb[j].state=='
if(pcb[max].prior<
pcb[j].prior)
max=j;
returnmax;
Running()
Sleep(500);
//等待,表示运行了一个时间片
inttemp;
temp=Get_Max_prior_ready_process();
if(temp==N)
系统内部的进程全部运行完毕!
//屏蔽其他组件
m_count2.Format("
m_prior2.Format("
m_request_time2.Format("
//修改当前运行进程的PCB
if(pcb[temp].request_time){
pcb[temp].request_time-=1;
//需要运行时间减1
pcb[temp].prior-=1;
//优先数减1
//显示当前正在运行的进程信息
m_prior2.Format("
pcb[temp].prior);
m_request_time2.Format("
pcb[temp].request_time);
m_count2.Format("
temp+1);
if(pcb[temp].request_time==0){
pcb[temp].state='
F'
//将该进程信息在就绪进程表中删除
int_num=0;
intnIndex;
tagLVFINDINFOAinfo;
CStringstrlist=_T("
_num=m_list_w.GetItemCount();
info.flags=LVFI_PARTIAL|LVFI_STRING;
for(inti=0;
_num;
strlist.Format("
pcb[temp].num);
info.psz=strlist;
//找到待删除的行
if((nIndex=m_list_w.FindItem(&
info))!
=-1)
m_list_w.DeleteItem(nIndex);
break;
//将该进程加入到已完成进程列表中
),pcb[temp].num);
m_list_f.InsertItem(_count,strItem);
),pcb[temp].prior);
m_list_f.SetItemText(_count,1,strItem);
),0);
m_list_f.SetItemText(_count,2,strItem);
m_list_f.SetItemText(_count,3,_T("
F"
m_list_f.Update(_count);
_count++;
Running();
OnStart()
//屏蔽开始按钮
m_Start.ShowWindow(SW_HIDE);
OnExit()
OnOK();
3、运行结果
(1)初始化进程信息,显示到就绪进程列表控件中
(2)点击开始按钮,模拟处理机调度,动态显示运行情况
(3)将完成的进