Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<conio.h>

void fifo();
void lru();
void optimal();
int nof,nos;
char input[]="70120304230321201701";
char slot[10][2];

void main()
{
 int ch,i,cont=0
 ;
 clrscr();
	printf("Enter number of frames");
 scanf("%d",&nof);
 printf("Enter number of slots");
 scanf("%d",&nos);
 printf("\n\nEnter values");
/* for(i=0;i<nof;i++)>
 {
	scanf("%c",&input[i]);
 }*/


 printf("Enter your choice");
 printf("\n1)FIFIO\n2)Optimal\n3)LRU\n4)Exit");
 scanf("%d",&ch);
 do
 {
	switch(ch)
	{
		case 1: fifo();
						break;
	 /*	case 2: optimal();
						break; */
		case 3:lru();
					 break;
		case 4: exit(0);

	}
 printf("\n Do you want to continue Yes->1/No->0:");
 scanf("%d",&cont);
 }while(cont==1);
 getch();
}

void fifo()
{
 int i,j,pf=0,loc=0,empty=0,found=0;
 int min;
 for(j=0;j<nos;j++)>
	{
	 slot[j][0]='\0';
	 slot[j][1]=0+48;
	}
	pf=0;
	loc=0;

	for(i=0;i<nof;i++)>
	{
	 found=0;
	 empty=0;

	 for(j=0;j<nos;j++)>
	 {
		if(input[i]==slot[j][0])
		{
		 found=1;
		 break;
		}
	 }
	 if(found==0)//for page replacement
	 {
		for(j=0;j<nos;j++)>
		{
		 if(slot[j][0]=='\0')
		 {
			loc=j;
			empty=1;
			break;
		 }
		}
		if(empty==1)
		{
		 pf++;
		 slot[loc][0]=input[i];
		 slot[loc][1]=i+48;
		}
		else
		{
		 pf++;
		 min=slot[0][1]-48;
		 loc=0;
		 for(j=0;j<nos;j++)>
		 {
			if(min>(slot[j][1]-48))
			{
			 min=slot[j][1]-48;
			 loc=j;
			}
		 }
		 slot[loc][0]=input[i];
		 slot[loc][1]=i+48;
		}

	 }
	 printf("\n");

	 for(j=0;j<nos;j++)>
		 printf("%c ",slot[j][0]);
	}
	printf("\n Number of page faults %d",pf);
}
void lru()
{
 int i,j,pf=0,loc=0,empty=0,found=0;
 int min;
 for(j=0;j<nos;j++)>
	{
	 slot[j][0]='\0';
	 slot[j][1]=0+48;
	}
	pf=0;
	loc=0;

	for(i=0;i<nof;i++)>
	{
	 found=0;
	 empty=0;

	 for(j=0;j<nos;j++)>
	 {
		if(input[i]==slot[j][0])
		{
		 found=1;
		 slot[j][1]=i+48;
		 break;
		}
	 }
	 if(found==0)//for page replacement
	 {
		for(j=0;j<nos;j++)>
		{
		 if(slot[j][0]=='\0')
		 {
			loc=j;
			empty=1;
			break;
		 }
		}
		if(empty==1)
		{
		 pf++;
		 slot[loc][0]=input[i];
		 slot[loc][1]=i+48;
		}
		else
		{
		 pf++;
		 min=slot[0][1]-48;
		 loc=0;
		 for(j=0;j<nos;j++)>
		 {
			if(min>(slot[j][1]-48))
			{
			 min=slot[j][1]-48;
			 loc=j;
			}

		 for(i=0;i<nof;i++)>
	{
	 found=0;
	 empty=0;

	 for(j=0;j<nos;j++)>
	 {
		if(input[i]==slot[j][0])
		{
		 found=1;
		 slot[j][1]=i+48;
		 break;
		}
	 }}
		 slot[loc][0]=input[i];
		 slot[loc][1]=i+48;
		}
	 }
	 printf("\n");

	 for(j=0;j<nos;j++)>
		 printf("%c ",slot[j][0]);
	}
	printf("\n Number of page faults %d",pf);
}

void optimal()
{
 int val,f_flag=0,o_index,f_index;

 for(j=0;j<nos;j++)>
 {
	slot[j][0]='\0';
	slot[j][1]=0+48; //f_flag
	slot[j][2]=0+48;//old
	slot[j][3]=0+48; //future
 }
 for(i=0;i<nof;i++)>
	{
	 found=0;
	 empty=0;

	 for(j=0;j<nos;j++)>
	 {
		if(input[i]==slot[j][0])
		{
		 found=1;
		 slot[j][1]=i+48;
		 break;
		}
	 }
	 if(slot[j][1]==1)
	 {
		 pf++;
		 max=slot[0][1]-48;
		 loc=0;
		 for(j=0;j<nos;j++)>
		 {
			if(max<(slot[j][1]-48))
			{
			 max=slot[j][1]-48;
			 loc=j;
			}
		 }
		 slot[loc][0]=input[i];
		 slot[loc][1]=i+48;
		}
		else
		pf++;
		 min=slot[0][2]-48;
		 loc=0;
		 for(j=0;j<nos;j++)>
		 {
			if(min>(slot[j][2]-48))
			{
			 min=slot[j][2]-48;
			 loc=j;
			}
		 }
		 slot[loc][0]=input[i];
		 slot[loc][2]=i+48;
		}
	 }
}

[EDIT by Jochen Arndt: Added code block]
Posted
Updated 25-Apr-15 0:32am
v2
Comments
ZurdoDev 24-Apr-15 15:31pm    
Do you have a question?
KarstenK 24-Apr-15 15:55pm    
What are the error??? Copy the whole error message. Think about it - maybe it is true what the compiler is yelling!

1 solution

Your optimal() function is defined inside the lru() function. That is not allowed. So move the closing brace from the end of the code to the end of your lru() function and add the necessary local variables to optimal().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900