Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. //paging simulation
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<process.h>
  6. int main()
  7. {
  8.     int refstr[50],ch,frame[10],distance[10],present=0,i=0,j=0,k=0,l=0,found=0,flag=0,len=0,val,framesize=0,toberemoved=0,pagefault=0;
  9.  
  10.     printf("\nenter the referrence string  (-1 to terminate):");
  11.     //read the referrence string
  12.     while(flag!=1)
  13.     {
  14.         scanf("%d",&refstr[len]);
  15.  
  16.         if(refstr[len]==-1)
  17.             flag=1;
  18.         len++;
  19.  
  20.     }
  21.     len--;
  22.     printf("\nEnter frame size  : ");
  23.     scanf("%d",&framesize);
  24.  
  25.   while(1)
  26.   {
  27.     printf("\n\n\t\tMAIN MENU\n1.OPTIMAL\n2.LRU\n3.FCFS\n4.Exit\n ");
  28.     scanf("%d",&ch);
  29.     switch(ch)
  30.     {
  31.  
  32.      case 1:
  33.  
  34.       //    goto lru;
  35.        //goto fcfs  ;
  36.  
  37.  
  38.        /*optimal starts*/
  39.     pagefault=0;
  40.     for(i=0;i<framesize;i++)
  41.         frame[i]=-1;
  42.       //    frame=new int[framesize];
  43.     for(i=0;i<framesize;i++)
  44.     {
  45.         frame[i]=refstr[i];
  46.         pagefault++;
  47.         printf("%d  ",frame[i]);
  48.     }
  49.     printf("\n");
  50.     k=0;
  51.  
  52.     //i now starts with framesize,say the frame size is three then now,
  53.     //frame already has the first three elements of the referrence string
  54.     while(i<len)
  55.     {
  56.         found=0;
  57.            for(j=0;j<framesize;j++)
  58.            {
  59.         if(refstr[i]==frame[j])
  60.         {
  61.             found=1;
  62.         }
  63.  
  64.            }
  65.            if(found!=1)
  66.            {
  67.         for(j=0;j<framesize;j++)
  68.             distance[j]=100;
  69.  
  70.         int  max=0;
  71.         toberemoved=0;
  72.         for(j=0;j<framesize;j++)
  73.         {
  74.  
  75.             for(k=i+1;k<len;k++)
  76.             {
  77.                 if(frame[j]==refstr[k])
  78.                 {
  79.                     distance[j]=k;
  80.                     break;
  81.                 }
  82.             }
  83.         }
  84.         for(j=1;j<framesize;j++)
  85.         {
  86.             if(distance[j]>distance[max])
  87.                 max=j;
  88.         }
  89.         toberemoved=max;
  90.         frame[toberemoved]=refstr[i];
  91.         pagefault++;
  92.               //    if(k==len)
  93.             //  k=0;
  94.  
  95.            }
  96.  
  97.     //  k++;
  98.      for(j=0;j<framesize;j++)
  99.            printf("%d  ",frame[j])   ;
  100.            printf("\n");
  101.  
  102.       i++;
  103.     }
  104.     printf("\npagefaults = %d",pagefault);
  105. /*optimal ends*/
  106.  
  107.  
  108. /*lru starts*/
  109.     break;
  110.  
  111.     case 2:
  112.  
  113.     for(i=0;i<framesize;i++)
  114.     {
  115.         frame[i]=refstr[i];
  116.         distance[i]=i;
  117.         pagefault++;
  118.         printf("%d  ",frame[i]);
  119.     }
  120.     k=0;
  121.     printf("\n");
  122.     //i=0;
  123.     //i now starts with framesize
  124.     while(i<len)
  125.     {
  126.      found=0;
  127.            for(j=0;j<framesize;j++)
  128.            {
  129.         if(refstr[i]==frame[j])
  130.         {
  131.             found=1;
  132.             distance[j]=i;//if the variable is found, then the distance tag should be replaced with the new tag
  133.  
  134.         }
  135.  
  136.            }
  137.            if(found!=1)
  138.            {
  139.  
  140.         int  min=0;
  141.         toberemoved=0;
  142.         for(j=1;j<framesize;j++)
  143.         {
  144.             if(distance[j]<distance[min])
  145.                 min=j;
  146.         }
  147.         toberemoved=min;
  148.         distance[min]=i;
  149.         frame[toberemoved]=refstr[i];
  150.         pagefault++;
  151.               //    if(k==len)
  152.             //  k=0;
  153.  
  154.            }
  155.  
  156.     //  k++;
  157.      for(j=0;j<framesize;j++)
  158.            printf("%d  ",frame[j])   ;
  159.            printf("\n");
  160.  
  161.       i++;
  162.     }
  163.     printf("\npagefaults = %d",pagefault);
  164.   /*lru ends*/
  165.   break;
  166.   case 3:
  167.  
  168.   /*fcfs starts*/
  169.  
  170.  
  171.      // fcfs:
  172.  
  173.        pagefault=0;
  174.        toberemoved=0;
  175.   //    distance=new int[framesize];
  176.     for(i=0;i<framesize;i++)
  177.          {
  178.         frame[i]=refstr[i];
  179.         pagefault++;
  180.         printf("%d  ",frame[i]);
  181.          }
  182.          printf("\n");
  183.  
  184.  // printf("%d  %d",i,len);
  185.     while(i<len)
  186.     {
  187.        //    printf("%d",refstr[i++]);
  188.          found=0;
  189.            for(j=0;j<framesize;j++)
  190.            {
  191.         if(refstr[i]==frame[j])
  192.             found=1;
  193.  
  194.            }
  195.            if(found!=1)
  196.            {
  197.         frame[toberemoved]=refstr[i];
  198.         toberemoved++;
  199.         if(toberemoved==framesize)
  200.         {
  201.             toberemoved=0;
  202.         }
  203.         pagefault++;
  204.  
  205.            }
  206.            for(j=0;j<framesize;j++)
  207.            printf("%d  ",frame[j])   ;
  208.            printf("\n");
  209.            i++;
  210.     }
  211.     printf("\npagefaults = %d",pagefault);
  212.        break;
  213.        case 4:
  214.     exit(0);
  215.        }
  216.  }
  217.     getch();
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement