Search Tutorials

Saturday, 1 June 2013

C code for Page Replacement Algorithm

Page Replacement:
The page frame data table is used for page replacement. Several pointers are used to create lists within this table. All of the available frames are linked together in a list of free frames available for bringing in pages. When the number of available frames drops below a certain threshold, the kernel will steal a number of frames to compensate.

/* C program to implement page replacement algorithm */

#include<stdio.h>

void allocate(int);
void dellocate();
void display();

int mem[50][10],n,pmt[200][3],count=0;

int main()
 {
   int frame,i,ch,j;
   printf("Initially we have 500 KB of memory.\n");
   printf("Enter frame size:-     ");
    scanf("%d",&frame);
  printf("Total memory blocks will be = %d",500/frame);
    n=500/(frame*10);
   for(i=0;i<n;i++)
    for(j=0;j<10;j++)
      mem[i][j]= -1;
  while(1)
    {
       printf("\n *** MENU ***\n");
       printf("1.Alloctae memory to a process.\n");
       printf("2.Delete the allocated memory.\n");
       printf("3.Exit\n");
       printf("Enter ur choice:-    ");
    scanf("%d",&ch);
       switch(ch)
        {
           case 1:
              allocate(frame);
              display();
              break;
           case 2:
              dellocate();
              display();
              break;
           case 3:
              exit(1);
           default:
               printf("Entered wrong choice.Try again.\n");
         }
    }


 }

void allocate(int frame)
 {
   int pno,psize,m,i,j,x,p;
   printf("\nEnter the process number:-    ");
    scanf("%d",&pno);
   printf("\nEnter the size:-     ");
     scanf("%d",&psize);
    m=(psize/frame);
    if((psize%frame)!=0)
     m+=1;
     x=m;
     for(i=0;i<n && m>0;)
      {
       for(j=0;j<10;j++)
    if(mem[i][j]==-1)
     {
      mem[i][j]=pno;
      p=x-m;
      pmt[count+p][0]=pno;
      pmt[count+p][1]=p;
      pmt[count+p][2]=(i*n+j);
      m--;
          break;
         }
       if(j==10)
        i++;
      } 
    count+=x;
}

void display()
  {
    int i,j,flag=0;
    printf("\n*** P M T ****\n");
    printf(" Pr   Pg   Blk \n");
    for(i=0;i<count;i++)
     {
      for(j=0;j<3;j++)
    if(pmt[i][0]!=-1)
     {
      flag=1;
      printf(" %d  |",pmt[i][j]);
     }
    if(flag)
     printf("\n");
    flag=0;
      }
   printf("\n*** MEMORY ***\n");
     for(i=0;i<n;i++)
      {int count=0;
       for(j=0;j<10;j++)
        if(mem[i][j]==-1)
         printf(" _ ");
        else
        printf(" %d ",mem[i][j]);
       printf("\n");
    }
}

void dellocate()
 {
   int pno,i,j;
   printf("\nEnter the process to be dellocated:-     ");
    scanf("%d",&pno);
    for(i=0;i<n;i++)
     for(j=0;j<10;j++)
      if(mem[i][j]==pno)
        mem[i][j]=-1;
   for(i=0;i<count;i++)
     if(pmt[i][0]==pno)
        pmt[i][0]=-1;
        
}       
                   
//Output Of the above program:-

C code for Page Replacement Algorithm

C code for Page Replacement Algorithm

No comments:

Post a Comment

Back to Top