IT007 temp

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.4 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>


void FIFO(int pages[100],int  sizePage, int frame[100][100],int  sizeFrame,int  fault[100],int  victim=0, int i=0)
{
	for (int time=1; time <sizePage; time++)
	{
		for (i=0; i<sizeFrame; i++)
		{
			frame[i][time]= frame[i][time-1];
		}
		for (i=0; i<sizeFrame; i++)
		{
			if (frame[i][time] == pages[time]||frame[i][time]==-1)
			{
				if (frame[i][time]==-1)
				{
					frame[i][time]=pages[time];
					fault[time]="*";
				}
				else
				{
					fault[time]=" ";
				}
				break;
			}
		}	
		if (i== sizeFrame)
		{
			frame[victim][i]= pages[time];
			victime= (victim +1) % sizeFrame;
		}

	}
	print(pages[100], sizePage, frame[100][100], sizeFrame, fault[100]);
}
void OPT(int arr[100],int  sizePage, int frame[100][100],int sizeFrame,int fault[100])
{
	
}
void LRU(int arr[100],int  sizePage, int frame[100][100],int  sizeFrame,int  fault[100])
{
	
}
void print(int pages[100], int sizePage, int frame[100][100], int sizeFrame, int fault[100], int i=0)
{
	int pageFault=0;
	printf("\nPage replacement algorithm");
	for(i=0; i< sizePage;i++)
	{
		printf("%2d ", pages[i]);
		
	}
	for(i=0; i<sizePage; i++)
	{
		for (int j=0; j<sizeFrame; j++)
		{
			if (frame[i][j] =="-1")
				printf("  ");
			else
				printf("%d ",frame[i][j];
		}
		printf("\n");
	}
	print("\n");
	for (int i=0; i< sizePage; i++)
	{
		printf("%c",fault[i]);
		pageFault++;
	}
	printf("Number of Page Fault: %d", pageFault);
}



int main()
{
	int choice, sizePage,sizeFrame, i;
	char fault[100];
	int frame[100][100];
	int pages[100];
	printf("\n---Page Replacement Algorithm---");
	printf("\n1. Default referenced sequence");
	printf("\n2. Manual input sequence\n");
	scanf("%d",&choice);
	switch(choice)
	{
		case 1:
			sizePage=8;
			pages= {1,9,5,2,1,6,0,3};
			break;
		case 2:
			printf("Input length: ");
			scanf("%d",&sizePage);
			for (i=0; i< sizePage; i++)
			{
				scanf("%d",&pages[i]);
			}
			break;
		default: break;
	}
	printf("\n--Page Replacement algorithm---");
	printf("\n1. FIFO algorithm");
	printf("\n2. OPT algorithm");
	printf("\n3. LRU algorithm\n");
	scanf("%d",&choice);
	printf("\n--Input page frame");
	scanf("%d",&sizeFrame);
	frame[0][0]= pages[0][0];
	for (i=0; i<sizePage; i++)
	{
		frame[i][0]= -1;
	}
	fault[0]= "*";
	switch(choice)
	{
		case 1:
			FIFO(pages,sizePage,frame,sizeFrame,fault);
			break;
		case 2:
			break;
		case 3:
			break;
		default: break;
	}
	return 0;

}