#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;
}
}