【大三操作系统实验】 请求页式管理中的置换算法

(1)FIFO算法总是选择在内存驻留时间最长的一页将其淘汰。FIFO算法认为调入内存的页不再被可能性要比其他页大,因而选择最先调入内存的页换出。

(2)LRU算法基本思想:当需要淘汰某一页时,选择离当前时间最近的一段时间内最久没有使用过的页先淘汰。

(3)OPT算法基本思想:在访问串中将来再也不出现的或是在离当前最远的位置上出现的页。

 

主要算法实现代码部分在Onqueding()

Code:
  1. // 置换算法Dlg.cpp : implementation file   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include "置换算法.h"   
  6. #include "置换算法Dlg.h"   
  7.   
  8. #ifdef _DEBUG   
  9. #define new DEBUG_NEW   
  10. #undef THIS_FILE   
  11. static char THIS_FILE[] = __FILE__;   
  12. #endif   
  13.   
  14. /   
  15. // CAboutDlg dialog used for App About   
  16.   
  17.      CRect rectSmall;  //收缩   
  18.      CRect rectLarge;  //扩展   
  19.      CRect rectrang_1;  //边缘           
  20.     bool flag=true;   
  21.     int i,j,k,m;   
  22.         int visit[19];   //访问数组   
  23.     int *count;  //停留标记   
  24.     int *mem;  //内存分配页   
  25. int FIFO(int *count_0,int num);   
  26. int OPT(int *count_0,int i,int num,int *vis);   
  27. class CAboutDlg : public CDialog   
  28. {   
  29. public:   
  30.     CAboutDlg();   
  31.   
  32. // Dialog Data   
  33.     //{{AFX_DATA(CAboutDlg)   
  34.     enum { IDD = IDD_ABOUTBOX };   
  35.     //}}AFX_DATA   
  36.   
  37.     // ClassWizard generated virtual function overrides   
  38.     //{{AFX_VIRTUAL(CAboutDlg)   
  39.     protected:   
  40.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support   
  41.     //}}AFX_VIRTUAL   
  42.   
  43. // Implementation   
  44. protected:   
  45.     //{{AFX_MSG(CAboutDlg)   
  46.     //}}AFX_MSG   
  47.     DECLARE_MESSAGE_MAP()   
  48. };   
  49.   
  50. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)   
  51. {   
  52.     //{{AFX_DATA_INIT(CAboutDlg)   
  53.     //}}AFX_DATA_INIT   
  54. }   
  55.   
  56. void CAboutDlg::DoDataExchange(CDataExchange* pDX)   
  57. {   
  58.     CDialog::DoDataExchange(pDX);   
  59.     //{{AFX_DATA_MAP(CAboutDlg)   
  60.     //}}AFX_DATA_MAP   
  61. }   
  62.   
  63. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)   
  64.     //{{AFX_MSG_MAP(CAboutDlg)   
  65.         // No message handlers   
  66.     //}}AFX_MSG_MAP   
  67. END_MESSAGE_MAP()   
  68.   
  69. /   
  70. // CMyDlg dialog   
  71.   
  72. CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)   
  73.     : CDialog(CMyDlg::IDD, pParent)   
  74. {   
  75.     //{{AFX_DATA_INIT(CMyDlg)   
  76.     m_suanfa = -1;   
  77.     //}}AFX_DATA_INIT   
  78.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32   
  79.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);   
  80. }   
  81.   
  82. void CMyDlg::DoDataExchange(CDataExchange* pDX)   
  83. {   
  84.     CDialog::DoDataExchange(pDX);   
  85.     //{{AFX_DATA_MAP(CMyDlg)   
  86.     DDX_Control(pDX, IDC_rang, m_rang);   
  87.     DDX_Radio(pDX, IDC_RADIO1, m_suanfa);   
  88.     //}}AFX_DATA_MAP   
  89. }   
  90.   
  91. BEGIN_MESSAGE_MAP(CMyDlg, CDialog)   
  92.     //{{AFX_MSG_MAP(CMyDlg)   
  93.     ON_WM_SYSCOMMAND()   
  94.     ON_WM_PAINT()   
  95.     ON_WM_QUERYDRAGICON()   
  96.     ON_WM_MOUSEMOVE()   
  97.     ON_BN_CLICKED(IDC_queding, Onqueding)   
  98.     //}}AFX_MSG_MAP   
  99. END_MESSAGE_MAP()   
  100.   
  101. /   
  102. // CMyDlg message handlers   
  103.   
  104. BOOL CMyDlg::OnInitDialog()   
  105. {   
  106.     CDialog::OnInitDialog();   
  107.   
  108.     // Add "About..." menu item to system menu.   
  109.   
  110.     // IDM_ABOUTBOX must be in the system command range.   
  111.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);   
  112.     ASSERT(IDM_ABOUTBOX < 0xF000);   
  113.   
  114.     CMenu* pSysMenu = GetSystemMenu(FALSE);   
  115.     if (pSysMenu != NULL)   
  116.     {   
  117.         CString strAboutMenu;   
  118.         strAboutMenu.LoadString(IDS_ABOUTBOX);   
  119.         if (!strAboutMenu.IsEmpty())   
  120.         {   
  121.             pSysMenu->AppendMenu(MF_SEPARATOR);   
  122.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);   
  123.         }   
  124.     }   
  125.   
  126.     // Set the icon for this dialog.  The framework does this automatically   
  127.     //  when the application's main window is not a dialog   
  128.     SetIcon(m_hIcon, TRUE);         // Set big icon   
  129.     SetIcon(m_hIcon, FALSE);        // Set small icon   
  130.        
  131.     // TODO: Add extra initialization here   
  132.     /*  
  133.     CRect rect,rect_1;    
  134.     m_rang.GetWindowRect(&rect);  
  135.     GetWindowRect(&rect_1);  
  136.     rect_1.right=rect.right+2;  
  137.     SetWindowPos(NULL,0,0,rect_1.Width(),rect_1.Height(),  
  138.             SWP_NOMOVE | SWP_NOZORDER);  
  139.             */  
  140.     m_rang.GetWindowRect(&rectrang_1);   
  141.     GetWindowRect(&rectLarge);   
  142.     rectSmall.top=rectLarge.top;   
  143.     rectSmall.left=rectLarge.left;   
  144.     rectSmall.right=rectrang_1.right;   
  145.     rectSmall.bottom=rectLarge.bottom;   
  146.     //默认为FIFO算法   
  147.     m_suanfa=0;   
  148.     UpdateData(FALSE);   
  149.     return TRUE;  // return TRUE  unless you set the focus to a control   
  150. }   
  151.   
  152. void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)   
  153. {   
  154.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)   
  155.     {   
  156.         CAboutDlg dlgAbout;   
  157.         dlgAbout.DoModal();   
  158.     }   
  159.     else  
  160.     {   
  161.         CDialog::OnSysCommand(nID, lParam);   
  162.     }   
  163. }   
  164.   
  165. // If you add a minimize button to your dialog, you will need the code below   
  166. //  to draw the icon.  For MFC applications using the document/view model,   
  167. //  this is automatically done for you by the framework.   
  168.   
  169. void CMyDlg::OnPaint()    
  170. {   
  171.     if (IsIconic())   
  172.     {   
  173.         CPaintDC dc(this); // device context for painting   
  174.   
  175.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);   
  176.   
  177.         // Center icon in client rectangle   
  178.         int cxIcon = GetSystemMetrics(SM_CXICON);   
  179.         int cyIcon = GetSystemMetrics(SM_CYICON);   
  180.         CRect rect;   
  181.         GetClientRect(&rect);   
  182.         int x = (rect.Width() - cxIcon + 1) / 2;   
  183.         int y = (rect.Height() - cyIcon + 1) / 2;   
  184.   
  185.         // Draw the icon   
  186.         dc.DrawIcon(x, y, m_hIcon);   
  187.     }   
  188.     else  
  189.     {   
  190.         CDialog::OnPaint();   
  191.     }   
  192.     CDC *pDC = GetWindowDC();   //获取窗口DC   
  193.     CDC *memDC  =   new   CDC;   //定义兼容DC   
  194. //  if(flag)   
  195. //  {   
  196.         memDC->CreateCompatibleDC(pDC);   
  197.     //加载位图   
  198.     CBitmap pBit;   
  199.     pBit.LoadBitmap(IDB_BITMAP1);   
  200.     CBitmap *pOldBit;   
  201.     //选位图进memDC   
  202.     pOldBit=(CBitmap *)memDC->SelectObject(&pBit);   
  203.     CRect rect;   
  204.     CRect rectrang;   
  205.     m_rang.GetWindowRect(&rectrang);   
  206.     GetWindowRect(&rect);   
  207.     rect.right=rectrang.right;   
  208.     BITMAP bitmap;   
  209.     pBit.GetBitmap(&bitmap);   
  210.     //pDC->BitBlt(0,0,rect.Width(),rect.Height(),memDC,0,0,SRCCOPY);   
  211.     pDC->StretchBlt(0,0,rect.Width(),rect.Height(),memDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);   
  212.     memDC->SelectObject(pOldBit);   
  213.     pDC->SetBkMode(TRANSPARENT);   
  214. //  flag=false;   
  215.        
  216. //  }   
  217. }   
  218.   
  219. // The system calls this to obtain the cursor to display while the user drags   
  220. //  the minimized window.   
  221. HCURSOR CMyDlg::OnQueryDragIcon()   
  222. {   
  223.     return (HCURSOR) m_hIcon;   
  224. }   
  225.   
  226. void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)    
  227. {   
  228.     // TODO: Add your message handler code here and/or call default   
  229.        
  230.     if(point.x>= rectSmall.right-10)  //扩展   
  231.     {   
  232.            
  233.         SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),   
  234.             SWP_NOMOVE |  SWP_NOZORDER);   
  235.         if(flag)   
  236.         {   
  237.         this->Invalidate(FALSE);   
  238.         flag=false;  //只闪烁一次   
  239.         }   
  240.     }   
  241.     else   //收缩   
  242.     {   
  243.         SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),   
  244.             SWP_NOMOVE |  SWP_NOZORDER);   
  245.         flag=true;   
  246.     }   
  247.     //this->Invalidate(FALSE);   
  248.     CDialog::OnMouseMove(nFlags, point);   
  249. }   
  250.   
  251.   
  252. void CMyDlg::Onqueding()    
  253. {   
  254.     UpdateData();   
  255.     CListCtrl *m_list = new CListCtrl();   
  256.     m_list->Create(WS_CHILD | WS_VISIBLE |LVS_REPORT,CRect(20,20,545,400),this,102);   
  257.     //改为网状风格   
  258.     DWORD dwStyle=m_list->GetExtendedStyle();   
  259.     dwStyle |= LVS_EX_FULLROWSELECT;  //整行高亮所选中的行只适用Report Style   
  260.     dwStyle |= LVS_EX_GRIDLINES;           //网格线. 只适用Report Style   
  261.     m_list->SetExtendedStyle(dwStyle );   
  262.        
  263.     m_list->SetBkColor(RGB(50,200,50));//绿背景色   
  264.     m_list->SetTextColor(RGB(0,0,220)); //蓝文本色   
  265.     m_list->SetTextBkColor(RGB(50,200,50));  //绿文本背景色   
  266.     m_list->InsertColumn(0,"访问序列",LVCFMT_LEFT,75);   
  267.   
  268.        
  269.     int m_proc=this->GetDlgItemInt(IDC_COMBO1);  //获取组合框文本   
  270.     int m_mem=this->GetDlgItemInt(IDC_COMBO2);   
  271.     int stay;   
  272.     CString cs;   
  273.     bool flag1=true;   
  274.     if(m_mem)  //有值才动态定义   
  275.     {   
  276.         mem = new int[m_mem];   //内存分配数组   
  277.         memset(mem,-1,sizeof(int)*m_mem);  //初始化为-1   
  278.         count = new int[m_mem];   //标记驻留次数   
  279.         memset(count,0,sizeof(int)*m_mem);   //初始化为0   
  280.     }   
  281.        
  282.     if(m_proc)   
  283.     {          
  284.         for(j=1;j<19;j++)   //初始化列   
  285.         {   
  286.             visit[j]=rand()%m_proc;    //访问序列初始化   
  287.                
  288.             cs.Format("%d",visit[j]);   
  289.             m_list->InsertColumn(j,cs,LVCFMT_LEFT,25);   
  290.         }   
  291.     }   
  292.     for(k=0;k<m_mem;k++)    //初始化行,分配页   
  293.     {   
  294.         char cha[2];   
  295.         itoa(k,cha,10);   
  296.         cs="内存第页";  //汉字双字节,注意   
  297.         cs.Insert(6,cha);   
  298.         m_list->InsertItem(k,cs);   
  299.            
  300.     }   
  301.     if(m_mem &&m_proc)   
  302.     {   
  303.         for(i=1;i<19;i++)  //列   
  304.         {   
  305.             for(k=0;k<m_mem;k++,i++)    //行   
  306.             {   
  307.                    
  308.                 for(j=0;j<m_mem;j++)   //找出相等的内存页visit[i]与所有行比较   
  309.                     if(mem[j]==visit[i])   //若相等马上输出所有行   
  310.                     {   
  311.                         switch(m_suanfa)   
  312.                         {   
  313.                         case 1:   //FIFO变为LRU只需把逗留数组count置为0就可以了   
  314.                             count[j]=0;   //相同的内存页把逗留次数置0   
  315.                             break;   
  316.                         }   
  317.                         for(m=0;m<m_mem;m++)  //输出   
  318.                         {      
  319.                             if(mem[m]!=-1)   
  320.                             {   
  321.                                 cs.Format("%d",mem[m]);   
  322.                                 m_list->SetItemText(m,i,cs);   
  323.                                 count[m]++;   
  324.                                    
  325.                             }   
  326.                         }   
  327.                         k--;   
  328.                         flag1=false;   
  329.                         break;//找到相等的先输出所有行,,然后跳出   
  330.                     }   
  331.                     ///    
  332.                     if(flag1)      
  333.                     {      
  334.                         switch(m_suanfa)   
  335.                         {   
  336.                         case 0: //FIFO   
  337.                         case 1: //LRU   
  338.                             if(mem[m_mem-1]!=-1)    //内存页满,开始置换   
  339.                             {   
  340.                                 stay=FIFO(count,m_mem);   //谁的逗留时间最长   
  341.                                    
  342.                                 mem[stay]=visit[i];   //最长的被置换   
  343.                                 count[stay]=0;   //逗留次数清零   
  344.                             }   
  345.                             else    //内存页还没用完   
  346.                             {   
  347.                                 mem[k]=visit[i];   
  348.                             }                                                  
  349.                                 break;   
  350.                         case 2:  //OPT   
  351.                             if(mem[m_mem-1]!=-1)    //内存页满,开始置换   
  352.                             {   
  353.                                 stay=OPT(mem,i,m_mem,visit);   //谁的逗留时间最长   
  354.                                    
  355.                                 mem[stay]=visit[i];   //最长的被置换   
  356.                             }   
  357.                             else    //内存页还没用完   
  358.                             {   
  359.                                 mem[k]=visit[i];   
  360.                             }   
  361.                                
  362.                                 break;   
  363.                         }//switch   
  364.                         for(m=0;m<m_mem;m++)  //输出所有行   
  365.                                 if(mem[m]!=-1)   
  366.                                 {   
  367.                                     CString cst;   
  368.                                     cst.Format("%d",mem[m]);   
  369.                                     m_list->SetItemText(m,i,cst);   
  370.                                     count[m]++;  //标记停留次数   
  371.                                 }   
  372.                     }   
  373.                     flag1=true;   
  374.             ///            
  375.             }//for(k   
  376.             if(k==m_mem) i--;  //有两个i++   
  377.         }//for(i   
  378.         delete []count;   
  379.         delete []mem;   
  380.     }//if   
  381.     UpdateData(FALSE);   
  382. }   
  383.   
  384. int FIFO(int *count_0,int num)   
  385. {   
  386.     int Max=count_0[0];   
  387.     int Max_stay=0;   
  388.     for(int n=1;n<num;n++) //找出最大数的下标   
  389.      if(Max < count_0[n])   
  390.         {   
  391.             Max=count_0[n];   
  392.             Max_stay=n;   
  393.         }   
  394.         return Max_stay;   
  395. }   
  396. int *L;   
  397. int OPT(int *temp_mem,int i,int num,int *vis)   
  398. {   
  399.     int Max;   
  400.     int Max_stay;   
  401.     int p=i+1;   
  402.     L = new int[num];   
  403.     memset(L,0,sizeof(int)*num);  //从0开始   
  404.     for(int n=0;n<num;n++)   
  405.         for(i=p;i<19;i++)  //下一个开始比   
  406.         if(temp_mem[n]!=vis[i]) L[n]++;   
  407.         else break;   
  408.         Max=L[0];   
  409.         Max_stay=0;   
  410.         for(int h=0;h<num;h++)   //找出最大数的下标   
  411.         {   
  412.            
  413.             if(Max < L[h])   
  414.             {   
  415.                 Max=L[h];   
  416.                 Max_stay=h;   
  417.             }   
  418.         }   
  419.             delete []L;   
  420.     return Max_stay;   
  421. }   

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值