操作系统进程调度算法模拟__mfc实现

上传人:小** 文档编号:55830835 上传时间:2018-10-07 格式:DOC 页数:18 大小:82.50KB
返回 下载 相关 举报
操作系统进程调度算法模拟__mfc实现_第1页
第1页 / 共18页
操作系统进程调度算法模拟__mfc实现_第2页
第2页 / 共18页
操作系统进程调度算法模拟__mfc实现_第3页
第3页 / 共18页
操作系统进程调度算法模拟__mfc实现_第4页
第4页 / 共18页
操作系统进程调度算法模拟__mfc实现_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《操作系统进程调度算法模拟__mfc实现》由会员分享,可在线阅读,更多相关《操作系统进程调度算法模拟__mfc实现(18页珍藏版)》请在金锄头文库上搜索。

1、#include “windows.h“ #include “windowsx.h“ #include “tchar.h“ #include “strsafe.h“ #include “resource.h“#pragma warning(disable:4312) #pragma warning(disable:4244)#define EDITLEN 20 #define RR2 / 时间片 #define chHANDLE_DLGMSG(hWnd, message, fn) case (message): return (SetDlgMsgResult(hWnd, uMsg, HANDL

2、E_#message(hWnd), (wParam), (lParam), (fn)struct ProcInfo int procID; / 进程 ID float arriveT; / 达到时间 float serverT; / 服务时间 float remainT; / 剩余运行时间 float lastrunT; / 上次运行时间 float runT; / 运行时间总和 float priority; / 优先级,高响应比优先调度算法时初始为 1,其他算法未用到,初始 为 0 struct ProcInfo *next; / 下一结点 *startProc,*endProc;HWND

3、 hWnd;/* 检查 6 个进程的到达时间和服务时间是否已经全部输入 已经全部输入返回 TRUE,否则返回 FALSE */ bool CheckEdits() wchar_t strEDITLEN;for (int i=IDC_A1;inext = node; endProc = node;if (startProc-next = NULL) startProc-next = endProc; /* 移除链表头结点 */ void RemoveNode() if (startProc != NULL) float finishT; wchar_t str10; ProcInfo *tmp

4、= startProc;if (startProc-next != NULL) startProc = startProc-next; startProc-lastrunT = tmp-lastrunT+tmp-remainT; else startProc = endProc = NULL; finishT = tmp-lastrunT+tmp-remainT; StringCchPrintf(str,10,_T(“%f“),finishT); SetDlgItemText(hWnd,IDC_A3+5*tmp-procID,str); StringCchPrintf(str,10,_T(“%

5、f“),finishT-tmp-arriveT); SetDlgItemText(hWnd,IDC_A4+5*tmp-procID,str); StringCchPrintf(str,10,_T(“%f“),(finishT-tmp-arriveT)/(float)tmp-serverT); SetDlgItemText(hWnd,IDC_A5+5*tmp-procID,str);delete tmp; /* 当链表头结点被抢占或时间片到时,将头结点移到尾结点 参数:moreT 为头结点运行到被抢占或时间片到的时间间隔*/ void ChangeNode(int moreT) if (star

6、tProc != NULL) if (startProc-next != NULL) ProcInfo *tmp = startProc; startProc = startProc-next; tmp-next = NULL; tmp-runT += moreT; tmp-remainT -= moreT; endProc-next = tmp; endProc = tmp; startProc-lastrunT = tmp-lastrunT+moreT; else startProc-lastrunT += moreT; startProc-runT += moreT; startProc

7、-remainT -= moreT; /* 先到先服务调度算法 */ void AlgoFCFS(int *arrive,int *server) int procNum = 6; int index = 0; ProcInfo *node;startProc = endProc = NULL;while (procNum) if (startProc != NULL) / 直接将每个进程插入链表,链表中每个结点按顺序运行 while (indexprocID = index; node-arriveT = arriveindex; node-serverT = serverindex; no

8、de-remainT = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 0;node-next = NULL; index+; InsertNode(node); RemoveNode(); procNum-; else if (indexprocID = index; node-arriveT = arriveindex; node-serverT = serverindex; node-remainT = serverindex; node-lastrunT = arriveindex; n

9、ode-runT = 0; node-priority = 0; node-next = NULL; index+; InsertNode(node); else break; /* 短进程优先调度算法 */ void AlgoSJF(int *arrive,int *server) int procNum = 6; int index = 0; float minremainT; ProcInfo *node,*tmp,*seize,*mid,*tmp2;startProc = endProc = NULL;while (procNum) if (startProc != NULL) tmp

10、 = seize = startProc; minremainT = startProc-remainT;/ 查看链表中是否有剩余运行时间比头结点小的进程,如果有存入最小运行时间 while (tmp=tmp-next) != NULL) if (tmp-remainT remainT; seize = tmp; else if (minremainT remainT while (indexlastrunT+minremainT) node = new ProcInfo; node-procID = index; node-arriveT = arriveindex; node-server

11、T = serverindex; node-remainT = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 0; node-next = NULL; index+; InsertNode(node);/ 当有新进程到达时,根据剩余运行时间插入到链表中 mid = NULL; tmp = startProc; while (tmp-next != endProc) if (tmp-next-remainT endProc-remainT) mid = tmp; break; tmp = tmp-

12、next; if (mid != NULL) tmp = startProc-next; while (tmp-next != NULL) if (tmp-next = endProc) tmp2 = endProc; tmp-next = NULL; endProc = tmp;tmp2-next = mid-next; mid-next = tmp2; break; tmp = tmp-next; / 查看刚插入的结点的剩余运行时间是否小于最小剩余时间 if (node-remainT remainT; seize = node; else if (minremainT remainT e

13、lse break; / 当 seize 仍为头结点时,表示没有比头结点更小的剩余运行时间,故移除 头结点 if (seize = startProc) RemoveNode(); procNum-; else / 否则,将抢占进程移到头结点之后,并更改结点 tmp = startProc; while (tmp != NULL) if (tmp-next = seize) tmp-next = seize-next; if (seize = endProc) endProc = tmp; break; tmp = tmp-next; seize-next = startProc-next;

14、if (startProc = endProc) endProc = seize; startProc-next = seize; ChangeNode(minremainT); else if (indexprocID = index; node-arriveT = arriveindex; node-serverT = serverindex; node-remainT = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 0; node-next = NULL; index+; InsertN

15、ode(node); else break; /* 高响应比优先调度算法*/ void AlgoHRP(int *arrive,int *server) int procNum=6; int index=0; float waitT,minwaitT,waitT2,lastwaitT; ProcInfo *node,*tmp,*seize,*second; float prio=0,stmp;startProc = endProc = NULL;while(procNum) if (startProc != NULL) tmp = seize = second = startProc; minwaitT = startProc-remainT;/ 计算在头结点剩余时间内,是否有进程的优先级超过头结点,如果有,取 最先超过头结点的进程为抢占进程 while(tmp=tmp-next) != NULL) waitT = (startProc-priority-1)*tmp-serverT+0.1; / 计算超过头结

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号