Hi there and thank you in advance for your help in advance. I am having problems with my page replacement program with taking the integers from an input file and putting them into an array. I need it in format [Next page in] | | | | (current pages between the lines followed by an F if it is a fault). I was able to write it so that I can set a number of frames "NR_PHYS_PAGES" but I need the number of pages to be the inputed value of "iFrames."

Below is a portion of my code

#include <stdio.h>	// for printf() 
#include <stdlib.h>	// for random() 
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <fstream>

#define NR_VIRT_PAGES	5
#define NR_PHYS_PAGES	3
#define NR_REFERENCES	100

int iFrames;
int pagestream[ NR_REFERENCES ];
int page_cache[NR_PHYS_PAGES];
using namespace std;

void help(){
	cout<<"Enter usage instructions here \n";
	exit(1);

}
int main( int argc, char **argv )
{
int i = 0;
	
	iFrames = atoi(argv[1]);

	//Read from the file
	string capture;
	ifstream myfile (argv[2]);
  	if (myfile.is_open()){
   		 while (! myfile.eof() ){
      			getline (myfile,capture,' ');
			
 			pagestream[i] = atoi(capture.c_str());
			//parse the contents of line to a int array
			i++;
      			
    		 }
	
	    	myfile.close();
  	}

  	else {
	cout << "Unable to open file \n";
	help();
	} 

	string function = argv[3];

	// Error check frame numbers
	if (iFrames > 99){
			cout << "Page stream needs to be under 99" << endl;
			help();
			exit(1);
			}
	

	 
	//-----------------------------------
	// display the page-reference stream
	//-----------------------------------
//	printf( "\nThe stream of page-references: \n" );
//	for (int x = 0; x < i; x++) 
//		printf( "%2d",pagestream[ x ] );
//	printf( "\n\n" );
	

if (function == "fifo" || "lru" || "opt" || "clo"){
	//-------------------------------------
	// perform page-replacement siulations	
	//-------------------------------------
	if (function == "opt"){
	int optimal_algorithm( void );
	printf( "\noptimal algorithm: %d faults \n\n", optimal_algorithm() );
	}

	if (function == "clo"){
	int clock_algorithm( void );
	printf( "\nclock algorithm: %d faults \n\n", clock_algorithm() );
	}

	if (function == "fifo"){
	int fifo_algorithm( void );
	printf( "\nfifo algorithm: %d faults \n\n", fifo_algorithm() );
	}

//	if (function == "lru"){
//	int lru_algorithm(void);
//	printf("\nlru algorithm: %d faults \n\n", lru_algorithm() );
//	}

}
	else {
	cout<< "Error \n";
	help();
	}
}


void show_page_cache( void )
{
	for (int i = 0; i < NR_PHYS_PAGES; i++)
		printf( "%2d ", page_cache[ i ] );
	printf( "\n\n" );
}	


int fill_up_pagecache( void )
{
	int	r = 0;	// reference-index
	int	u = 0;	// unused cache-entry index
	int	p, i;
	do	{
		p = pagestream[ r ];
		// see if p is in page_cache	
		for (i = 0; i < u; i++) 
			if ( p == page_cache[ i ] ) break;
		
		if ( i < u ) continue;	 // page already in cache
		page_cache[ u++ ] = p;  // add to cache
		}
	while (( ++r < NR_REFERENCES )&&( u < NR_PHYS_PAGES )); 
	return	r;
}


int fifo_algorithm( void )
{
	//-----------------------
	// count the page-faults
	//-----------------------
	int	faults = 0;
	int	r = fill_up_pagecache();
	printf("[", r, pagestream[ r ] );
	
 	
	show_page_cache();

	int	next = 0, page_accessed[ NR_PHYS_PAGES ] = {0};
       	while ( r < NR_REFERENCES )
		{
		int	p = pagestream[ r++ ];
		// see if page is in cache
		int	i;
		for (i = 0; i < NR_PHYS_PAGES; i++)
			if ( p == page_cache[ i ] ) break;
		if ( i == NR_PHYS_PAGES ) // not in cache 
			{ 
			++faults;
			page_cache[ next ] = p;
			next = ( 1 + next ) % NR_PHYS_PAGES;
			}
		printf("[%d]", r, pagestream[ r ] );
		show_page_cache();
		}				
	return	faults;			
}

A quick problem I see is this:

if (function == "fifo" || "lru" || "opt" || "clo")

It should be this:

if (function == "fifo" || 
    function == "lru" || 
    function == "opt" || 
    function == "clo")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.