处理机调度算法的实现.docx

上传人:b****6 文档编号:5660030 上传时间:2022-12-30 格式:DOCX 页数:17 大小:111.37KB
下载 相关 举报
处理机调度算法的实现.docx_第1页
第1页 / 共17页
处理机调度算法的实现.docx_第2页
第2页 / 共17页
处理机调度算法的实现.docx_第3页
第3页 / 共17页
处理机调度算法的实现.docx_第4页
第4页 / 共17页
处理机调度算法的实现.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

处理机调度算法的实现.docx

《处理机调度算法的实现.docx》由会员分享,可在线阅读,更多相关《处理机调度算法的实现.docx(17页珍藏版)》请在冰豆网上搜索。

处理机调度算法的实现.docx

处理机调度算法的实现

实验报告

学院(系)名称:

计算机与通信工程学院

姓名

学号

专业

班级

实验项目

实验一:

处理机调度算法的实现

课程名称

操作系统

课程代码

0668036

实验时间

2011年10月25日

2011年10月28日

2011年11月01日

实验地点

软件实验室7-215

批改意见

成绩

 

教师签字:

【实验环境】Windows操作系统环境下的个人微机

【实验目的】

通过编写程序实现进程高优先权优先调度算法,使学生进一步掌握进程调度的概

念和算法,加深对处理机分配的理解。

【实验要求】

1.详细描述实验设计思想、程序结构及各模块设计思路;

2.详细描述程序所用数据结构及算法;

3.明确给出测试用例和实验结果;

4.为增加程序可读性,在程序中进行适当注释说明;

5.认真进行实验总结,包括:

设计中遇到的问题、解决方法与收获等;

6.实验报告撰写要求结构清晰、描述准确逻辑性强;

7.实验过程中,同学之间可以进行讨论互相提高,但绝对禁止抄袭。

【实验内容】

1.设定系统中有五个进程,每一个进程用一个进程控制块表示。

2.输入每个进程的“优先数”和“要求运行时间”。

3.为了调度方便,将五个进程按给定的优先数从大到小连成就绪队列。

用一单元指出队列首进程,用指针指出队列的连接情况。

4.处理机调度总是选队首进程运行。

采用动态优先数算法,进程每运行一次优先数就减“1”,同时将运行时间减“1”。

5.若某进程运行时间为零,则将其状态置为“结束”,且退出队列。

6.运行所设计程序,显示或打印逐次被选中进程的进程名,以及进程控制块的动态变化过程。

【实验步骤、过程】

1、程序流程图

2、程序代码

//Deal_ProcessDlg.cpp:

implementationfile

//

#include"stdafx.h"

#include"Deal_Process.h"

#include"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("");

//}}AFX_DATA_INIT

//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32

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

}

voidCDeal_ProcessDlg:

:

DoDataExchange(CDataExchange*pDX)

{

CDialog:

:

DoDataExchange(pDX);

//{{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);

//}}AFX_DATA_MAP

}

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)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

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

//CDeal_ProcessDlgmessagehandlers

BOOLCDeal_ProcessDlg:

:

OnInitDialog()

{

CDialog:

:

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);

pSysMenu->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,"优先数",LVCFMT_LEFT,nColInterval*2);

m_list_w.InsertColumn(2,"要求运行时间",LVCFMT_LEFT,nColInterval*2);

m_list_w.InsertColumn(3,"状态",LVCFMT_LEFT,rect.Width()-nColInterval*6);

m_list_f.InsertColumn(0,"进程编号",LVCFMT_LEFT,nColInterval*2);

m_list_f.InsertColumn(1,"优先数",LVCFMT_LEFT,nColInterval*2);

m_list_f.InsertColumn(2,"要求运行时间",LVCFMT_LEFT,nColInterval*2);

m_list_f.InsertColumn(3,"状态",LVCFMT_LEFT,rect.Width()-nColInterval*6);

returnTRUE;//returnTRUEunlessyousetthefocustoacontrol

}

voidCDeal_ProcessDlg:

:

OnSysCommand(UINTnID,LPARAMlParam)

{

if((nID&0xFFF0)==IDM_ABOUTBOX)

{

CAboutDlgdlgAbout;

dlgAbout.DoModal();

}

else

{

CDialog:

:

OnSysCommand(nID,lParam);

}

}

//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow

//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,

//thisisautomaticallydoneforyoubytheframework.

voidCDeal_ProcessDlg:

:

OnPaint()

{

if(IsIconic())

{

CPaintDCdc(this);//devicecontextforpainting

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

//Centericoninclientrectangle

intcxIcon=GetSystemMetrics(SM_CXICON);

intcyIcon=GetSystemMetrics(SM_CYICON);

CRectrect;

GetClientRect(&rect);

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

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

//Drawtheicon

dc.DrawIcon(x,y,m_hIcon);

}

else

{

CDialog:

:

OnPaint();

}

}

//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags

//theminimizedwindow.

HCURSORCDeal_ProcessDlg:

:

OnQueryDragIcon()

{

return(HCURSOR)m_hIcon;

}

//初始化N个进程

voidCDeal_ProcessDlg:

:

OnConfirm()

{

//TODO:

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("%d"),count+1);

m_list_w.InsertItem(count,strItem);//首先插入一行,其中的字符可以任意,也可以为空

strItem.Format(_T("%d"),m_prior1);

m_list_w.SetItemText(count,1,strItem);//设置第二列数据,如此可以设置其它列的数据

strItem.Format(_T("%d"),m_request_time1);

m_list_w.SetItemText(count,2,strItem);

m_list_w.SetItemText(count,3,_T("W"));

if(count

{

m_count1.Format("%d",count+2);

m_prior1=0;

m_request_time1=0;

UpdateData(false);

}

else

{

//屏蔽初始化组件

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

if(pcb[i].state=='W')

{

max=i;break;

}

if(i==5)

max=i;

for(j=max+1;j

if(pcb[j].state=='W')

{

if(pcb[max].prior

max=j;

}

returnmax;

}

voidCDeal_ProcessDlg:

:

Running()

{

Sleep(500);//等待,表示运行了一个时间片

inttemp;

temp=Get_Max_prior_ready_process();

if(temp==N)

{

MessageBox("系统内部的进程全部运行完毕!

");

//屏蔽其他组件

m_count1.Format("");

m_count2.Format("");

m_prior2.Format("");

m_request_time2.Format("");

UpdateData(false);

return;

}

//修改当前运行进程的PCB

if(pcb[temp].request_time){

pcb[temp].request_time-=1;//需要运行时间减1

pcb[temp].prior-=1;//优先数减1

//显示当前正在运行的进程信息

m_prior2.Format("%d",pcb[temp].prior);

m_request_time2.Format("%d",pcb[temp].request_time);

m_count2.Format("%d",temp+1);

UpdateData(false);

}

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;i<_num;i++)

{

strlist.Format("%d",pcb[temp].num);

info.psz=strlist;//找到待删除的行

if((nIndex=m_list_w.FindItem(&info))!

=-1)

{

m_list_w.DeleteItem(nIndex);

break;

}

}

//将该进程加入到已完成进程列表中

CStringstrItem;

strItem.Format(_T("%d"),pcb[temp].num);

m_list_f.InsertItem(_count,strItem);//首先插入一行,其中的字符可以任意,也可以为空

strItem.Format(_T("%d"),pcb[temp].prior);

m_list_f.SetItemText(_count,1,strItem);//设置第二列数据,如此可以设置其它列的数据

strItem.Format(_T("%d"),0);

m_list_f.SetItemText(_count,2,strItem);

m_list_f.SetItemText(_count,3,_T("F"));

m_list_f.Update(_count);

_count++;

}

Running();

}

voidCDeal_ProcessDlg:

:

OnStart()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

Running();

//屏蔽开始按钮

m_Start.ShowWindow(SW_HIDE);

}

 

voidCDeal_ProcessDlg:

:

OnExit()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CDialog:

:

OnOK();

}

 

3、运行结果

(1)初始化进程信息,显示到就绪进程列表控件中

(2)点击开始按钮,模拟处理机调度,动态显示运行情况

(3)将完成的进

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1