Showing posts with label Oprating System. Show all posts
Showing posts with label Oprating System. Show all posts

Tuesday, 18 March 2014

Optimal Page Replacement C program

OPTIMAL PAGE REPLACEMENT ALGORITHM

Program Description: Optimal Page Replacement refers to the removal of the page that will not be used in the future, for the longest period of time.

Program Code:

#include<stdio.h>
int main()
{
int n,pg[30],fr[10];
int count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;
fault=0;
dist=0;
k=0;
printf("Enter the total no pages:\t");
scanf("%d",&n);
printf("Enter the sequence:");
for(i=0;i<n;i++)
scanf("%d",&pg[i]);
printf("\nEnter frame size:");
scanf("%d",&f);

for(i=0;i<f;i++)
{
count[i]=0;
fr[i]=-1;
}
for(i=0;i<n;i++)
{
flag=0;
temp=pg[i];
for(j=0;j<f;j++)
{
if(temp==fr[j])
{
flag=1;
break;
}
}
if((flag==0)&&(k<f))
{
fault++;
fr[k]=temp;
k++;
}
else if((flag==0)&&(k==f))
{
fault++;
for(cnt=0;cnt<f;cnt++)
{
current=fr[cnt];
for(c=i;c<n;c++)
{
if(current!=pg[c])
count[cnt]++;
else
break;
}
}
max=0;
for(m=0;m<f;m++)
{
if(count[m]>max)
{
max=count[m];
p=m;
}
}
fr[p]=temp;
}
printf("\npage  %d  frame\t",pg[i]);
for(x=0;x<f;x++)
{
printf("%d\t",fr[x]);
}
}
printf("\nTotal number of faults=%d",fault);
return 0;
}

Output:

Sunday, 17 November 2013

COMPARE CPU SCHEDULING OF LINUX AND UNIX--PROJECT REPORT








 COMPARE CPU SCHEDULING OF LINUX
                                                       AND UNIX


TERM PAPER OF OPERATING SYSTEM

TO DOWNLOAD CLICK BELOW:

Sunday, 27 October 2013

Least Recently Used(LRU) Page Replacement algorithm

Least Recently Used(LRU) Page Replacement algorithm:

Program Description: 

The Least Recently Used replacement policy chooses to replace the page which has not been referenced for the longest time.

The operating system keeps track of when each page was referenced by recording the time of reference or by maintaining a stack of references.This policy assumes the recent past will approximate the immediate future.

Program Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int cnt[10],page[20],frame[10];
int page_fault=0,t1,t2,i,t=0,j,k,m=0,n=0,min,count=0,nop,frame_size;//nop-->no. of pages
clrscr();
printf("Enter the no.of pages:\t");
scanf("%d",&nop);
printf("Enter the size of frame:\t");
scanf("%d",&frame_size);
printf("Enter page sequence:\n");
for(i=0;i<nop;i++)
scanf("%d",&page[i]);
for(i=0;i<frame_size;i++)
{
  frame[i]=0;cnt[i]=0;   //initialize frame to zero
}
k=0;
for(i=0;i<nop;i++)
{
  m=0;
  for(j=0;j<frame_size;j++)
  cnt[j]=0;
  for(j=0;j<frame_size;j++)
  {
      if(frame[j]==page[i])
      {
     m++;
      }
  }
  if(m!=0)
  {
      count++;
      if(k>2)
      k=0;
      k=k+1;
      printf("page %d\t",page[i]);
      printf("  frame\t");
      for(j=0;j<frame_size;j++)
      printf(" %d",frame[j]);
      printf("\tNo page fault\t");
      printf("\tPage_fault till now:\t%d\n",page_fault);
  }
  else if(count>3)
  {
      for(j=0;j<frame_size;j++)
      {
     t2=(count-3);
     for(n=(count-1);n>t2;n--)
     {
       if(frame[j]==page[n])
       {  cnt[j]++;
       }
     }
      }
      for(j=0;j<3;j++)
      min=cnt[0];
      t=0;
      for(j=0;j<frame_size;j++)
      {
     if(cnt[j]<min)
     {   min=cnt[j];
         t=j;
      }
      }
      if(k>2)
      k=0;
      frame[t]=page[i];
      count++;
      k++;
      printf("page %d\t",page[i]);
      printf("  frame\t");
      for(j=0;j<frame_size;j++)
      printf(" %d",frame[j]);
      page_fault++;
      printf("\tpage fault occurs\t");
      printf("Page_fault till now:\t%d\n",page_fault);
}
else
{     if(k>2)
      { k=0;}
      frame[k]=page[i];
      count++;
      k++;
      printf("page %d\t",page[i]);
      printf("  frame\t");
      for(j=0;j<frame_size;j++)
      printf(" %d",frame[j]);
      page_fault++;
      printf("\tpage fault occurs\t");
      printf("Page_fault till now:\t%d\n",page_fault);
}
}
getch();
return 0;
}

Output:



 

FIFO Page Replacement Algorithm - C Program

C Program - FIFO Page Replacement Algorithm

Program code:

#include<stdio.h>
int main()
{
int k,nop,frame_size,i,j,temp=100,page_fault=0;
int page_name[20],frame[20];
int m,n=0,count=0;
printf("Enter the No. of pages\t");
scanf("%d",&nop);
printf("Enter the frame size\t");
scanf("%d",&frame_size);
printf("Enter page name\n");
for(i=0;i<nop;i++)
scanf("%d",&page_name[i]);
for(i=0;i<frame_size;i++)
frame[i]=0;
for(i=0,j=0;i<frame_size;i++,j++)
{
frame[j]=page_name[i];
page_fault++;
printf("\npage\t%d\t",page_name[i]);
printf("Frame\t");
for(k=0;k<frame_size;k++)
printf(" %d",frame[k]);
printf("\tPage fault till now:\t");
printf("%d",page_fault);
}
j=0;
 

for(m=i;m<nop;m++)

if(page_name[m]==frame[j])
{

j++;
count++;
printf("\npage\t%d\t",page_name[m]);
printf("Frame\t");
for(k=0;k<frame_size;k++)
printf(" %d",frame[k]);
printf("\tPage fault till now:\t");
printf("%d",page_fault);
 }

else
{

frame[j]=page_name[m];
page_fault++;
j++;
count++;
printf("\npage\t%d\t",page_name[m]);
printf("Frame\t");
for(k=0;k<frame_size;k++)
printf(" %d",frame[k]);
printf("\tPage fault till now:\t");
printf("%d",page_fault);

}
if(count==frame_size)
j=0;
}
return 0;
}

 Output: