Uhm, i'm new to linux as well as C++ so i was supposed to change the if none are available part to a random replacement algorithm from the previous LRU algorithm but i can't get the program to run in linux RedHat 9.0, when i used the terminal to gcc my input file...something about not recognising the rand and srand..?? Plus i also can't output the hit/miss ratio for this code using scanf or cout, what gives??
Can anyone please help me?
Code:
#include <stdio.h>
#include <time.h>
#include <math.h>
#define MAXSWAP 500
#define MAXPAGE 20
typedef struct{
int age;
int state; /* 1 is used, 0 is unused */
char pageframe[5];
}vm;
int find_next_available_page(vm * memory, vm * swap)
{
int i,position;
int highest=0;
int j=-1;
/* see if there are any empty page frames in memory */
for(i=0;i<MAXPAGE;i++)
{
if(memory[i].state==0)
{
return i;
}
}
/* if none are available, a suitable candidate must be chosen... */
srand (time(NULL));
position = rand()%20; /* any position could be chosen to be replaced */
/* find an empty swap spot */
for(i=0;i<MAXSWAP;i++)
{
if(swap[i].state==0)
j=i;
}
if(j<0)
{
printf("Out of memory condition in simulation. Exiting\n");
exit(0);
}
strncpy(swap[j].pageframe, memory[position].pageframe, 5);
memory[position].state=0;
memory[position].age=0;
swap[j].state=1;
/* returns free spot */
return position;
}
int find_page_in_memory(char * page, vm * memory)
{
int i;
for(i=0;i<MAXPAGE;i++)
{
if(memory[i].state=1){
memory[i].age = memory[i].age+1;
}
}
for(i=0;i<MAXPAGE;i++)
{
if(strncmp(page, memory[i].pageframe, 5)==0){
memory[i].age = 0;
return 1;
}
}
return 0;
}
int find_page_in_swap(char * page, vm * swap, vm * memory)
{
int i,availablepage;
for(i=0;i<MAXSWAP;i++)
{
if(strncmp(page, swap[i].pageframe, 5)==0){
availablepage = find_next_available_page(memory, swap);
strncpy(memory[availablepage].pageframe, page, 5);
memory[availablepage].state=1;
memory[availablepage].age=0;
swap[i].state=0;
return 1;
}
}
return 0;
}
void dump_memory_map(vm *memory, vm *swap)
{
int i;
for (i=0;i<MAXPAGE;i++)
{
printf(" RAM Frame[%2i] = %s\n",i,memory[i].pageframe);
}
printf("\n");
for (i=0;i<MAXSWAP;i++)
{
printf("Swap Frame[%2i] = %s\n",i,swap[i].pageframe);
}
printf("\n");
}
int main(int argc, char *argv[])
{
FILE * fptr;
vm memory[MAXPAGE];
char page[5];
vm swap[MAXSWAP];
int i,hit,miss,availablepage;
hit = miss = 0;
int available_page = 0;
if(argc!=2){
fprintf(stderr, "Please specify trace file\n");
exit(1);
}
/* initialize all age as 0 and all state as unused */
for(i=0;i<MAXPAGE;i++)
{
memory[i].age=0;
memory[i].state=0;
memory[i].pageframe[0]='\0';
}
for(i=0;i<MAXSWAP;i++)
{
swap[i].age=0;
swap[i].state=0;
swap[i].pageframe[0]='\0';
}
fptr = fopen(argv[1], "r");
if(fptr==NULL){
fprintf(stderr, "Unable to open trace file");
exit(1);
}
while(!feof(fptr)){
fscanf(fptr,"%5s",page);
dump_memory_map(memory,swap); /* remove or comment this debugging line if
program output gets too verbose */
printf("Page Requested: %s\n",page);
if(!(find_page_in_memory(page,memory))){
miss++;
printf("Page miss!\n");
if(!(find_page_in_swap(page,swap,memory))){
availablepage = find_next_available_page(memory, swap);
strncpy(memory[availablepage].pageframe, page, 5);
memory[availablepage].state=1;
memory[availablepage].age=0;
}
}
else
{
hit++;
printf("Page hit!\n");
}
}
return 0;
}