| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 463 人关注过本帖
标题:关于先进先出算法和最近最久使用-只求思路!大神指点
只看楼主 加入收藏
lihonghaoc
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-1-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
关于先进先出算法和最近最久使用-只求思路!大神指点
本人编写出了随机数算法;
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "time.h"
main()
{
       int i,x;
       srand(time(0));
       for(i=0;i<20;i++)
       {
       x=rand()%20;
       printf("%d\n",x);
       }
       system("pause");
}
之后我同学编写出了FIFO;
#include  <stdio.h>
#include <stdlib.h>
#define mSIZE 3
#define pSIZE 8
static int memery[mSIZE] = {0};
static int process[pSIZE] = {0};
//static int process[pSIZE] = {2,3,2,1,5,2,4,5,3,2,5,2};
//static int process[pSIZE] = {7,10,1,2,10,3,10,4,2,3,10,3,2,1,2,10,1,7,10,1};
void build();             //生成一个随机数序列
void FIFO();               //FIFO置换算法
int main(int argc, char *argv[])
{
    printf("产生随机序列如下:\n");
    build();
    printf("先进先出(FIFO)页面置换算法 \n");
    FIFO();
    system("PAUSE");
    return 0;
}
void build()
{
     int i = 0;
     for(i=0; i<pSIZE; i++)
     {
              process[i] = (int)(10.0*rand()/(RAND_MAX+1.0) + 1);
              printf("%d  ",process[i]);
     }
     printf("\n");
}
void FIFO()
{
     int time[mSIZE] = {0};
     int i = 0, j = 0;
     int m = -1, n = -1;
     int max = -1,maxtime = 0;
     int count = 0;
   
     for(i = 0; i<pSIZE; i++)
     {
           //找第一个空闲的物理块
           for(j=0; j<mSIZE; j++)
           {
                    if(memery[j] == 0)
                    {
                           m = j;
                           break;
                    }
           }
         
           //找与进程相同的标号
           for(j = 0; j < mSIZE; j++)
           {
                 if(memery[j] == process[i])
                 {
                     n = j;
                 }
           }
           //找time值最大的
           for(j = 0; j < mSIZE;j++)
           {
                 if(time[j]>maxtime)
                 {
                       maxtime = time[j];
                       max = j;
                 }
           }  
         
           if(n == -1)               //不存在相同进程
           {
                if(m != -1)          //存在空闲物理块
                {
                     memery[m] = process[i];
                     time[m] = 0;
                     for(j = 0;j <= m; j++)
                     {
                           time[j]++;
                     }
                     m = -1;
                }
                else                //不存在空闲物理块
                {
                     memery[max] = process[i];
                     time[max] = 0;
                     for(j = 0;j < mSIZE; j++)
                     {
                           time[j]++;
                     }
                     max = -1;
                     maxtime = 0;
                     count++;
                }
           }
           else                    //存在相同的进程
           {
                 memery[n] = process[i];
                 for(j = 0;j < mSIZE; j++)
                 {
                       time[j]++;
                 }
                 n = -1;
           }
           for(j = 0 ;j < mSIZE; j++)
           {
                 printf("%d  ",memery[j]);
           }
           printf("\n");
     }
     printf("页面换算次数为:%d\n",count);
}
复上LRU的算法;
#include <stdio.h>
#include <stdlib.h>
#define mSIZE 3
#define pSIZE 8
static int memery[mSIZE] = {0};
static int process[pSIZE] = {0};
//static int process[pSIZE] = {2,3,2,1,5,2,4,5,3,2,5,2};
//static int process[pSIZE] = {7,10,1,2,10,3,10,4,2,3,10,3,2,1,2,10,1,7,10,1};
void build();             //生成一个随机数序列
void LRU();               //最近最久未使用(LRU)置换算法
int main(int argc, char *argv[])
{
    printf("产生随机序列如下:\n");
    build();
    printf("最近最久未使用(LRU)置换算法 \n");
    LRU();
    system("PAUSE");
    return 0;
}
void build()
{
     int i = 0;
     for(i=0; i<pSIZE; i++)
     {
              process[i] = (int)(10.0*rand()/(RAND_MAX+1.0) + 1);
              printf("%d  ",process[i]);
     }
     printf("\n");
}
void LRU()
{
     int flag[mSIZE] = {0};
     int i = 0, j = 0;
     int m = -1, n = -1;
     int max = -1,maxflag = 0;
     int count = 0;
   
     for(i = 0; i<pSIZE; i++)
     {
           //找第一个空闲的物理块
           for(j=0; j<mSIZE; j++)
           {
                    if(memery[j] == 0)
                    {
                           m = j;
                           break;
                    }
           }
           //找与进程相同的标号
           for(j = 0; j < mSIZE; j++)
           {
                 if(memery[j] == process[i])
                 {
                     n = j;
                 }
           }
           //找flag值最大的
           for(j = 0; j < mSIZE;j++)
           {
                 if(flag[j]>maxflag)
                 {
                       maxflag = flag[j];
                       max = j;
                 }
           }  
         
           if(n == -1)               //不存在相同进程
           {
                if(m != -1)          //存在空闲物理块
                {
                     memery[m] = process[i];
                     flag[m] = 0;
                     for(j = 0;j <= m; j++)
                     {
                           flag[j]++;
                     }
                     m = -1;
                }
                else                //不存在空闲物理块
                {
                     memery[max] = process[i];
                     flag[max] = 0;
                     for(j = 0;j < mSIZE; j++)
                     {
                           flag[j]++;
                     }
                     max = -1;
                     maxflag = 0;
                     count++;
                }
           }
           else                    //存在相同的进程
           {
                 memery[n] = process[i];
                 flag[n] = 0;
                 if(m != -1)       //若存在空闲物理块
                 {
                      flag[m] = 0;
                 }
                 for(j = 0;j < mSIZE; j++)
                 {
                       flag[j]++;
                 }
                 max = -1;
                 maxflag = 0;
                 n = -1;
           }
           for(j = 0 ;j < mSIZE; j++)
           {
                 printf("%d  ",memery[j]);
           }
           printf("\n");
     }
     printf("页面换算次数为:%d\n",count);
}

但是因为随机的方法不一样,我不能将随即数的方法放在FIFO和LRU算法中,请问是不是得重新编写,求编写思路....

[ 本帖最后由 lihonghaoc 于 2014-6-19 10:52 编辑 ]
搜索更多相关主题的帖子: include process system 
2014-06-19 10:39
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:20 
可以试试看

我不是砖家,要努力成为砖家。
2014-06-20 13:00
快速回复:关于先进先出算法和最近最久使用-只求思路!大神指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013954 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved