2 Replies - 20101 Views - Last Post: 05 May 2010 - 04:22 AM Rate Topic: -----

#1 hollywood210  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-May 10

FIFO LRU Page Replacement C++ to Java

Posted 04 May 2010 - 10:36 PM

Im new here, and need a lot of help. Ive been working on this code for the FIFO LRU algorithm implementation. I wrote it in C++ and am trying now to run it with Java. Though im not very good at java and dont even know where to begin. Any help would be appreciated. Thanks in advance.

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define max 100
#define min 10


int ref[max],count,frame[min],n,counter;
	int i,temp;
void input()
{

	
	count = 0;
	printf("\n\nEnter the number of page frames : ");
	scanf("%d",&n);
	printf("\n\nEnter the reference string (-1 to finish string) : \n");
	scanf("%d",&temp);
	while(temp != -1)
	{
		ref[count++]=temp;
		scanf("%d",&temp);
	}
}
///////// END OF INPUT FUNCTION 

void autoinput()
{
	int rande;

	//int i,temp;
	//counter =0;
	printf("\n\n\tNumber of Frames : ");
	srand(time(0));
	rande = 1 + rand() % 10;
	n = rande;
	cout<<n <<" frames"<<endl;

	printf("\n\n Reference string : ");

	rande = rand() % 10;
	temp = rande;

	rande = rand() % 50;
	for(int i = 0; i < 15; i++)
	{
        ref[count++]=temp;
        rande = rand() % 4;
        temp = rande;
     }

	for(int i = 0 ; i < count; i++)
	{
        cout<<ref[i]<<",";
        
    }
}
////////////END OF AUTOINPUT FUNCTION

void FIFO()
{
	int i,j,fault=0;

	for(i=0;i<n;i++)
	frame[i]=-1;

		for(i=0;i<count;i++)
		{
			for(j=0;j<n;j++)
			if(frame[j]==ref[i])
			break;
			if(j==n)
		frame[fault%n]=ref[i], fault++;

			printf("\n\nAfter inserting %d  : ",ref[i]);
		for(j=0;j<n;j++)
		printf("%d ",frame[j]);

		}
	printf("\n\n\tEnd of the reference string.");
	printf("\n\n\tTotal Page Fault is %d.",fault);
}
////////END OF FIFO FUNCTION


void LRU()
{
	int i,j,k,stack[min],top=0,fault=0;

	for(i=0;i<count;i++)
	{
		if(top<n)
		stack[top++]=ref[i],fault++;
		else
		{
		for(j=0;j<n;j++)
		if(stack[j]==ref[i])
		break;
		if(j<n)
		{
			for(k=j;k<n-1;k++)
			stack[k]=stack[k+1];
			stack[k]=ref[i];
		}
	else
	{
		for(k=0;k<n-1;k++)
		stack[k]=stack[k+1];
		stack[k]=ref[i];
		fault++;
	}
	}
	printf("\n\nAfter inserting %d : ",ref[i]);
	for(j=0;j<top;j++)
	printf("%d ",stack[j]);

	}
	printf("\n\n\tEnd to inserting the reference string.");
	printf("\n\n\tTotal page fault is %d.",fault);
}

////////END OF LRU FUNCTION

void main()
{
	int choice;

	 
			printf("\nPLEASE INSERT INFORMATION TO BE ABLE TO USE THE ALGORITHMS. \n\n");
				while(1)
				{        
					printf("\n|******************** MENU ******************|");
					printf("\n|                                            |");
					printf("\n|    1. Input page information               |");
					printf("\n|    2. Automatically Input page information |");
					printf("\n|    3. FIFO Algorithm                       |");
					printf("\n|    4. Least Recently Used (LRU) Algorithm  |");
					printf("\n|    5. Exit                                 |");
					printf("\n|                                            |");
					printf("\n|********************************************|");
					printf("\n\n Enter your choice.\n");
					scanf("%d",&choice);
               
						switch(choice)
						{
							case 1:
									input();
									break;
							case 2:
									autoinput();
									break;
							case 3:
								FIFO();
									break;
							case 4:
								LRU();
									break;
							case 5:
								exit(0);
						}
		}
}



Is This A Good Question/Topic? 0
  • +

Replies To: FIFO LRU Page Replacement C++ to Java

#2 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: FIFO LRU Page Replacement C++ to Java

Posted 05 May 2010 - 02:43 AM

I would start by learning java. Sun has a lot of nice tutorials for beginners. You might want to take a look at this. A few other helpful links:
http://www.dreaminco...hp?showforum=32
http://pages.cs.wisc...8/JavaTutorial/
http://triton.towson...a-tutorial.html

Good luck.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: FIFO LRU Page Replacement C++ to Java

Posted 05 May 2010 - 04:22 AM

View Posthollywood210, on 04 May 2010 - 11:36 PM, said:

I wrote it in C++


I don't believe you. :P

If you wrote it in C++ ( looks more like C, with some Turbo C++ flavoring ), then simply doing a one to one replacement should be rudimentary.

Assuming you did write it...
prinf ~= System.out.printf
scanf ~= Scanner ( class )
rand ~= Random ( class )
int array[i] ~= int [] array = new int[i];

Other than that, you could almost paste it in. Give it a go. Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1