Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2275512
  • 博文数量: 668
  • 博客积分: 10016
  • 博客等级: 上将
  • 技术积分: 8588
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 19:22
文章分类

全部博文(668)

文章存档

2011年(1)

2010年(2)

2009年(273)

2008年(392)

分类:

2009-05-13 11:52:29

#include
#include
 
#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();               //最近最久未使用(LRU)置换算法
 
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     {
              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     {
           //找第一个空闲的物理块
           for(j=0; 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);
}
阅读(7057) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-06-03 08:57:47

hao