操作系统实验六代码自用

这篇文章描述了一个C语言实现的虚拟内存管理系统,使用了FIFO(先进先出)页替换算法。系统包括虚拟页数组、页框控制结构、空闲和繁忙页框链表,以及页故障计数器。程序生成随机指令流并将其转换为页地址流,模拟页故障情况。
摘要由CSDN通过智能技术生成

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define TRUE 1
#define FALSE 0
#define INVALID -1

#define INSTRUCT_CNT 640             /* length of instruction stream */
#define MAX_WORD_ADDR 320             /* max word address */
#define VIRTUAL_PAGE_LEN 32            /* length of virtual page */

typedef struct{
    int pn,            /* page number */
            pfn;                /* page frame number */
}page_struct_type;


page_struct_type page_array[VIRTUAL_PAGE_LEN];                 /* page struct array */
struct pf_ctl_struct{                       /* page frame control struct */
    int pn,                /* page number */
    pfn;                /* page frame number */
    struct pf_ctl_struct *next;    /* to next */
};

/* used for paging replacement, such as free page frame list or busy page frame list */
typedef struct pf_ctl_struct pf_ctl_t;
pf_ctl_t pfc[VIRTUAL_PAGE_LEN],
         *freepf_head,        /* head of free page frame list */
         *busypf_head,        /* head of busy page frame list */
         *busypf_tail;        /* tail of busy page frame list */

int page_fault_cnt,        /* number of page fault */  
    a[INSTRUCT_CNT];        /* the instruction list, which contains the logical address */
int page[INSTRUCT_CNT],        /* page number, the instruction<> */  
    offset[INSTRUCT_CNT];    /* offset */

/* initialize the associated page control struct */
void initialize(int total_page_frame){
    int i;
    page_fault_cnt=0;
    for(i=0;i<VIRTUAL_PAGE_LEN;i++){
        page_array[i].pn=i;
        // INVALID shows the page is not in the memory
        page_array[i].pfn=INVALID;
    }

    for(i=1;i<total_page_frame;i++){       
        // set link between pfc[i-1] and pfc[i]
        pfc[i-1].next=&pfc[i];
        pfc[i-1].pfn=i-1;
    }
    // page frame control
    pfc[total_page_frame-1].next=NULL;
    pfc[total_page_frame-1].pfn=total_page_frame-1;
        // the head pointer of free page array is pfc[0]
    freepf_head=&pfc[0];
}

void FIFO(int total_page_frame)                           /*FIFO ALGORITHM*/
{
    initialize(total_page_frame);
        //Coding here.
}

void main()
{
    int a[INSTRUCT_CNT], page[INSTRUCT_CNT];
    /* generate the instruction stream randomly */
    int S,i;
    srand(10*getpid());               
    S=(float) ((MAX_WORD_ADDR-1)*rand()/(RAND_MAX+1.0));  //FLOAT
    //printf("%2d RAND_MAX\n",RAND_MAX);
    for (i=0;i<INSTRUCT_CNT;i+=4){
        a[i]=S;                                     
        a[i+1]=a[i]+1;                              
        a[i+2]=(float)a[i]*rand()/RAND_MAX;              
        a[i+3]=a[i+2]+1;                            
        S=(float)rand()*(MAX_WORD_ADDR -2 -a[i+2])/RAND_MAX+a[i+2]+2;
    }
     /* change the instruction sequence to page address stream */
    for (i=0;i<INSTRUCT_CNT;i++){
        printf("%d\t", a[i]);
        page[i]=a[i]/10;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值