2022年操作系统进程调度算法模拟__MFC实现 .pdf

上传人:Q****o 文档编号:25942808 上传时间:2022-07-14 格式:PDF 页数:18 大小:101.07KB
返回 下载 相关 举报
2022年操作系统进程调度算法模拟__MFC实现 .pdf_第1页
第1页 / 共18页
2022年操作系统进程调度算法模拟__MFC实现 .pdf_第2页
第2页 / 共18页
点击查看更多>>
资源描述

《2022年操作系统进程调度算法模拟__MFC实现 .pdf》由会员分享,可在线阅读,更多相关《2022年操作系统进程调度算法模拟__MFC实现 .pdf(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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 RR 2 / 时间片#define chHANDLE_DLGMSG(hWnd, message, fn) case (message): return (SetDlgMsgResult(hWnd, uMsg, HANDLE_#messa

2、ge(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 hWnd; /* 检查

3、6 个进程的到达时间和服务时间是否已经全部输入已经全部输入返回TRUE,否则返回FALSE */ bool CheckEdits() wchar_t strEDITLEN; for (int i=IDC_A1;i=IDC_F1;i+=5) GetDlgItemText(hWnd,i,str,EDITLEN); if (lstrcmp(str,_T(0) = 0) return false; GetDlgItemText(hWnd,i+1,str,EDITLEN); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 -

4、 - - - - - - 第 1 页,共 18 页 - - - - - - - - - if (lstrcmp(str,_T(0) = 0 ) return false; return true; /* 计算平均周转时间和平均带权周转时间,并显示*/ void ShowEdits() float zzsj=0,pjzz=0; wchar_t str10; for (int i=IDC_A4;i=IDC_F4;i+=5) GetDlgItemText(hWnd,i,str,10); zzsj += _wtof(str); GetDlgItemText(hWnd,i+1,str,10); pjzz

5、 += _wtof(str); StringCchPrintf(str,10,_T(%f),zzsj/6); SetDlgItemText(hWnd,IDC_TIME1,str); StringCchPrintf(str,10,_T(%f),pjzz/6); SetDlgItemText(hWnd,IDC_TIME2,str); /* 清除所有编辑框的内容*/ void ClearEdits() for (int i=IDC_A1;inext = node; endProc = node; if (startProc-next = NULL) startProc-next = endProc;

6、 /* 移除链表头结点*/ void RemoveNode() if (startProc != NULL) float finishT; wchar_t str10; ProcInfo *tmp = startProc; if (startProc-next != NULL) startProc = startProc-next; startProc-lastrunT = tmp-lastrunT+tmp-remainT; else startProc = endProc = NULL; finishT = tmp-lastrunT+tmp-remainT; StringCchPrintf(

7、str,10,_T(%f),finishT); SetDlgItemText(hWnd,IDC_A3+5*tmp-procID,str); StringCchPrintf(str,10,_T(%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; /

8、* 当链表头结点被抢占或时间片到时,将头结点移到尾结点参数: moreT 为头结点运行到被抢占或时间片到的时间间隔名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 18 页 - - - - - - - - - */ void ChangeNode(int moreT) if (startProc != NULL) if (startProc-next != NULL) ProcInfo *tmp = startProc; startProc = startProc-next

9、; 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-remainT -= moreT; /* 先到先服务调度算法*/ void AlgoFCFS(int *arrive,int *server) int procNum = 6; int in

10、dex = 0; ProcInfo *node; startProc = endProc = NULL; while (procNum) if (startProc != NULL) / 直接将每个进程插入链表,链表中每个结点按顺序运行while (indexprocID = index; node-arriveT = arriveindex; node-serverT = serverindex; node-remainT = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 0; 名师资料总结

11、- - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 18 页 - - - - - - - - - node-next = NULL; index+; InsertNode(node); RemoveNode(); procNum-; else if (indexprocID = index; node-arriveT = arriveindex; node-serverT = serverindex; node-remainT = serverindex; node-lastrunT =

12、arriveindex; node-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 (startPr

13、oc != NULL) tmp = seize = startProc; minremainT = startProc-remainT; / 查看链表中是否有剩余运行时间比头结点小的进程,如果有存入最小运行名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 18 页 - - - - - - - - - 时间while (tmp=tmp-next) != NULL) if (tmp-remainT remainT; seize = tmp; else if (minremain

14、T remainT & tmp-remainT = minremainT) if (tmp-arriveT arriveT) seize = tmp; while (index6) / 查看在头结点被短进程抢占,或运行结束时,是否有新进程到达if (arriveindex lastrunT+minremainT) node = new ProcInfo; node-procID = index; node-arriveT = arriveindex; node-serverT = serverindex; node-remainT = serverindex; node-lastrunT =

15、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-next; if (mid != NULL) tmp = startProc-next; while (tmp-next

16、!= NULL) if (tmp-next = endProc) tmp2 = endProc; tmp-next = NULL; endProc = tmp; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 18 页 - - - - - - - - - tmp2-next = mid-next; mid-next = tmp2; break; tmp = tmp-next; / 查看刚插入的结点的剩余运行时间是否小于最小剩余时间if (node-remainT rema

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

18、) tmp-next = seize-next; if (seize = endProc) endProc = tmp; break; tmp = tmp-next; seize-next = startProc-next; if (startProc = endProc) endProc = seize; startProc-next = seize; ChangeNode(minremainT); else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 18 页 -

19、 - - - - - - - - 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+; InsertNode(node); else break; /* 高响应比优先调度算法*/ void AlgoHRP(int *arrive,int *server) i

20、nt 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; / 计算在头结点剩余时间内,是否有进程的优先级超过头结点,如果有,取最先超过头结点的进程为抢占进程

21、while(tmp=tmp-next) != NULL) waitT = (startProc-priority-1)*tmp-serverT+0.1; / 计算超过头结点的最小等待时间lastwaitT = waitT-(startProc-lastrunT-tmp-arriveT-tmp-runT); / 计算头结点运行期间还需要等待的时间if (lastwaitT priority = waitT/tmp-serverT+1; seize = tmp; else if (minwaitT remainT & lastwaitT = minwaitT) / 当等于最小时间时,服务时间越小,

22、优先级越高if (tmp-serverT serverT) tmp-priority = waitT/tmp-serverT+1; seize = tmp; else if (tmp-serverT = seize-serverT) / 当等于最小时间,服务时间也相同时,先到达者优先if (tmp-arriveT arriveT) tmp-priority = waitT/tmp-serverT+1; seize = tmp; else if (minwaitT = startProc-remainT) / 当头结点剩余时间内,没有进程的优先级抢占时,计算优先级第二高的进程waitT2 = s

23、tartProc-lastrunT-tmp-arriveT-tmp-runT+startProc-remainT; stmp = waitT2/tmp-serverT+1; if (stmpprio) tmp-priority = prio = stmp; second = tmp; while (index6) / 查看在头结点的剩余时间内,或被抢占的时间间隔内,是否有进程到达if (arriveindex lastrunT+minwaitT) node = new ProcInfo; node-procID = index; node-arriveT = arriveindex; node

24、-serverT = serverindex; node-remainT = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 1; node-next = NULL; index+; InsertNode(node); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 18 页 - - - - - - - - - / 查看在该进程到达,到头结点运行结束,或被抢占前,优先级是否能

25、超过头结点waitT = (startProc-priority-1)*node-serverT+0.1; lastwaitT = waitT+(node-arriveT-startProc-lastrunT); if (lastwaitT priority = waitT/node-serverT+1; seize = node; else if (minwaitT remainT & lastwaitT = minwaitT) if (node-serverT serverT) node-priority = waitT/node-serverT+1; seize = node; else

26、 if (node-serverT = seize-serverT) if (node-arriveT arriveT) node-priority = waitT/node-serverT+1; seize = node; else if (minwaitT = startProc-remainT) waitT2 = startProc-remainT-(node-arriveT-startProc-lastrunT); stmp = waitT2/node-serverT+1; if (stmp prio) node-priority = prio = stmp; second = nod

27、e; else break; / 当计算的抢占结点仍为头结点时,表示没有抢占的进程,故将第二高优先级的进程移到头结点之后,并移除头结点if (seize = startProc) if (second != startProc) tmp = startProc; while (tmp != NULL) if (tmp-next = second) tmp-next = second-next; if (second = endProc) endProc = tmp; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理

28、- - - - - - - 第 10 页,共 18 页 - - - - - - - - - break; tmp = tmp-next; second-next = startProc-next; if (startProc = endProc) endProc = second; startProc-next = second; RemoveNode(); procNum-; else / 当计算的抢占结点不为头结点时,表示存在能够抢占的进程,故将该抢占进程移到头结点之后,并更改结点tmp = startProc; while (tmp != NULL) if (tmp-next = sei

29、ze) tmp-next = seize-next; if (seize = endProc) endProc = tmp; break; tmp = tmp-next; seize-next = startProc-next; if (startProc = endProc) endProc = seize; startProc-next = seize; ChangeNode(minwaitT); else if (indexprocID = index; node-arriveT = arriveindex; node-serverT = serverindex; node-remain

30、T = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 1; node-next = NULL; index+; InsertNode(node); else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 18 页 - - - - - - - - - break; /* 时间片轮转调度算法*/ void AlgoRR(int *arrive,int *server) in

31、t procNum=6; / 进程数int index=0; ProcInfo *node; startProc = endProc = NULL; while(procNum) if (startProc != NULL) / 队列不为空时while (index6) / 当头结点运行到时间片到,或运行到自行结束,这段时间间隔内有进程到达时插入链表if (arriveindex lastrunT+(startProc-remainTremainT:RR) node = new ProcInfo; node-procID = index; node-arriveT = arriveindex;

32、 node-serverT = serverindex; node-remainT = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 0; node-next = NULL; index+; InsertNode(node); else break; / 当头结点进程的剩余时间小于等于时间片时,移除头结点,否则将结点移到链表末尾,第二结点开始运行if (startProc-remainTremainTRR)*/ ChangeNode(RR); else / 队列为空时if (index proc

33、ID = index; node-arriveT = arriveindex; node-serverT = serverindex; node-remainT = serverindex; node-lastrunT = arriveindex; node-runT = 0; node-priority = 0; node-next = NULL; index+; InsertNode(node); else break; BOOL OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) hWnd = hwnd; SendMessage(

34、hwnd,WM_SETICON,ICON_BIG ,(LPARAM)LoadIcon(HINSTANCE)GetWindowLongPtr(hwnd,GWLP_HINSTANCE), MAKEINTRESOURCE(IDI_MAIN); SendMessage(hwnd,WM_SETICON,ICON_SMALL,(LPARAM)LoadIcon(HINSTANCE)GetWindowLongPtr(hwnd,GWLP_HINSTANCE), MAKEINTRESOURCE(IDI_MAIN); return TRUE; void OnCommand(HWND hwnd, int id, HW

35、ND hwndCtl, UINT codeNotify) unsigned int value1,value2; wchar_t strEDITLEN; wchar_t str2EDITLEN; switch(id) case IDCANCEL: 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 18 页 - - - - - - - - - EndDialog(hwnd,id); break; case IDOK: ClearEdits(); break; case ID

36、C_FCFS: case IDC_SJF: case IDC_HRP: case IDC_RR: if (!CheckEdits() MessageBox(hWnd,_T( 将信息填写完整),_T( 提示 ),MB_OK); return; int arrive6; int server6; int n=0; for (int i=IDC_A1; i= value2) MessageBox(hWnd,_T( 该进程到达时间应比前一进程晚),_T(提示 ),MB_OK); SetDlgItemText(hWnd,IDC_B1,_T(); SetFocus(GetDlgItem(hWnd,IDC_

37、B1); else if ( lstrcmp(str,_T(0)=0 & lstrcmp(str2,_T(0)!=0 ) MessageBox(hWnd,_T( 先 填 写 前 一 进 程 的 到 达 时 间 ),_T( 提 示),MB_OK); SetDlgItemText(hWnd,IDC_B1,_T(); SetFocus(GetDlgItem(hWnd,IDC_A1); break; case IDC_C1: if (codeNotify = EN_KILLFOCUS) GetDlgItemText(hWnd,IDC_B1,str,EDITLEN); GetDlgItemText(hW

38、nd,IDC_C1,str2,EDITLEN); if ( lstrcmp(str,_T(0)!=0 & lstrcmp(str2,_T(0)!=0 ) value1 = GetDlgItemInt(hWnd,IDC_B1,NULL,TRUE);value2 = GetDlgItemInt(hWnd,IDC_C1,NULL,TRUE);if (value1 = value2) MessageBox(hWnd,_T( 该进程到达时间应比前一进程晚),_T(提示 ),MB_OK); SetDlgItemText(hWnd,IDC_C1,_T(); SetFocus(GetDlgItem(hWnd,

39、IDC_C1); else if ( lstrcmp(str,_T(0)=0 & lstrcmp(str2,_T(0)!=0 ) MessageBox(hWnd,_T( 先 填 写 前 一 进 程 的 到 达 时 间 ),_T( 提 示),MB_OK); SetDlgItemText(hWnd,IDC_C1,_T(); SetFocus(GetDlgItem(hWnd,IDC_B1); break; case IDC_D1: if (codeNotify = EN_KILLFOCUS) GetDlgItemText(hWnd,IDC_C1,str,EDITLEN); GetDlgItemTex

40、t(hWnd,IDC_D1,str2,EDITLEN); if ( lstrcmp(str,_T(0)!=0 & lstrcmp(str2,_T(0)!=0 ) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 18 页 - - - - - - - - - value1 = GetDlgItemInt(hWnd,IDC_C1,NULL,TRUE);value2 = GetDlgItemInt(hWnd,IDC_D1,NULL,TRUE);if (value1 = valu

41、e2) MessageBox(hWnd,_T( 该进程到达时间应比前一进程晚),_T(提示 ),MB_OK); SetDlgItemText(hWnd,IDC_D1,_T(); SetFocus(GetDlgItem(hWnd,IDC_D1); else if ( lstrcmp(str,_T(0)=0 & lstrcmp(str2,_T(0)!=0 ) MessageBox(hWnd,_T( 先 填 写 前 一 进 程 的 到 达 时 间 ),_T( 提 示),MB_OK); SetDlgItemText(hWnd,IDC_D1,_T(); SetFocus(GetDlgItem(hWnd,

42、IDC_C1); break; case IDC_E1: if (codeNotify = EN_KILLFOCUS) GetDlgItemText(hWnd,IDC_D1,str,EDITLEN); GetDlgItemText(hWnd,IDC_E1,str2,EDITLEN); if ( lstrcmp(str,_T(0)!=0 & lstrcmp(str2,_T(0)!=0 ) value1 = GetDlgItemInt(hWnd,IDC_D1,NULL,TRUE);value2 = GetDlgItemInt(hWnd,IDC_E1,NULL,TRUE); if (value1 =

43、 value2) MessageBox(hWnd,_T( 该进程到达时间应比前一进程晚),_T(提示 ),MB_OK); SetDlgItemText(hWnd,IDC_E1,_T(); SetFocus(GetDlgItem(hWnd,IDC_E1); else if ( lstrcmp(str,_T(0)=0 & lstrcmp(str2,_T(0)!=0 ) MessageBox(hWnd,_T( 先 填 写 前 一 进 程 的 到 达 时 间 ),_T( 提 示),MB_OK); SetDlgItemText(hWnd,IDC_E1,_T(); SetFocus(GetDlgItem(

44、hWnd,IDC_D1); break; case IDC_F1: if (codeNotify = EN_KILLFOCUS) GetDlgItemText(hWnd,IDC_E1,str,EDITLEN); GetDlgItemText(hWnd,IDC_F1,str2,EDITLEN); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 18 页 - - - - - - - - - if ( lstrcmp(str,_T(0)!=0 & lstrcmp(str2,_

45、T(0)!=0 ) value1 = GetDlgItemInt(hWnd,IDC_E1,NULL,TRUE); value2 = GetDlgItemInt(hWnd,IDC_F1,NULL,TRUE); if (value1 = value2) MessageBox(hWnd,_T( 该进程到达时间应比前一进程晚),_T(提示 ),MB_OK); SetDlgItemText(hWnd,IDC_F1,_T(); SetFocus(GetDlgItem(hWnd,IDC_F1); else if ( lstrcmp(str,_T(0)=0 & lstrcmp(str2,_T(0)!=0 )

46、MessageBox(hWnd,_T( 先 填 写 前 一 进 程 的 到 达 时 间 ),_T( 提 示),MB_OK); SetDlgItemText(hWnd,IDC_F1,_T(); SetFocus(GetDlgItem(hWnd,IDC_E1); break; INT_PTR CALLBACK ProcMain(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) switch(uMsg) chHANDLE_DLGMSG(hwndDlg,WM_INITDIALOG,OnInitDialog);chHANDLE_DLGMSG(hwnd

47、Dlg,WM_COMMAND,OnCommand); return FALSE; int WINAPI _tWinMain(HINSTANCE hinst,HINSTANCE,PTSTR pszCmdLine,int) HWND hwnd = FindWindow( _T(#32770),_T(进程调度算法模拟) ); if (IsWindow(hwnd) ) SetForegroundWindow(hwnd); else DialogBoxParam(hinst,MAKEINTRESOURCE(IDD_MAIN),NULL,ProcMain,_ttoi(pszCmdLine); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 18 页 - - - - - - - - - return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 18 页 - - - - - - - - -

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

当前位置:首页 > 技术资料 > 技术总结

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁