document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ----------------------------------------------------------------------------
  2.  Title -  Write a Program to implement following scheduling algorithms:1. First in First Out,
  3.     2. Least Recently Used, &  3. Optimal Page Replacement.
  4. ----------------------------------------------------------------------------
  5.  
  6. ------------------------
  7. MAIN PROGRAM a7.c
  8. ------------------------
  9.  
  10. //header file declaration
  11. #include<stdio.h>
  12. #include<conio.h>
  13.  
  14. //main function
  15. void main()
  16. {
  17.     //variable declaration
  18.     int ch,i,j,n,f,k,p,r[50],ind[50],t=0,page[10];
  19.     int temp,flag=0,pf=0,sel=0;
  20.     int found=0,s;
  21.  
  22.     clrscr();
  23.  
  24.     do
  25.     {
  26.         printf("\\n\\n\\t MENU \\n\\n 1. FIFO : \\n 2. LRU : \\n 3. OPTIMAL : \\n 4. EXIT : \\n\\n Enter Your Choice : ");
  27.         scanf("%d",&ch);
  28.  
  29.         switch(ch)
  30.         {
  31.             case 1 :
  32.  
  33.  
  34.                 printf("\\n Enter Length : ");
  35.                 scanf("%d",&n);
  36.  
  37.                 printf("\\n Enter Frame Length : ");
  38.                 scanf("%d",&f);
  39.  
  40.                 printf("\\n Enter List of Reference : ");
  41.                 for(i=0;i<n;i++)
  42.                 {
  43.                     scanf("%d",&r[i]); // accept all references from user upto value of \'n\'
  44.                     }
  45.  
  46.                 for(i=0;i<f;i++)
  47.                 {
  48.                     page[i]=-1;      // initially set all values as \'-1\'  in frame
  49.                     ind[i]=0;
  50.                     }
  51.  
  52.                 for(i=0;i<n;i++)
  53.                 {
  54.                     flag=0;  //set flag=0 because no. of frames are present
  55.  
  56.                     for(j=0;j<f;j++) // check upto the length of frame
  57.                     {
  58.                         if(r[i]==page[j]) // if \'i\' value is present in the frame \'page\' then
  59.                         {
  60.  
  61.                             flag=1; // set the flag to 1
  62.                             break;
  63.                             }
  64.                         }
  65.  
  66.                     if(flag==0)   //if new value is inserted in the frame
  67.                     {
  68.                         pf++;    // increment the value of page fault
  69.                         temp=n+1;
  70.  
  71.                         for(k=0;k<f;k++)  //upto frame length
  72.                         {
  73.                             if(temp>ind[k]) //if inserted value is greater than index
  74.                             {
  75.                                 temp=ind[k];  //copy the index value into temp
  76.                                 sel=k;     //copy new value into the variable \'sel\'
  77.                                 }
  78.                             }
  79.  
  80.                         page[sel]=r[i];  //copy the reference value into page var.
  81.                         t++;            //increament t
  82.                         ind[sel]=t;
  83.                         }
  84. /*                  else
  85.                     {
  86.                         pf++;
  87.                         }
  88. */                  printf("\\n\\n");
  89.  
  90.                     for(p=0;p<f;p++)
  91.                     {
  92.                         printf(" -- %d -- ",page[p]); // print the elements present in the frame for every pass
  93.                         }
  94.                     }
  95.                 printf("\\n\\n No. of Page Fault : %d \\n",pf); // display total no. of page fualts
  96.                 pf=0;
  97.  
  98.                 break;
  99.  
  100.             case 2 :
  101.                 printf("\\n Enter Length : ");
  102.                 scanf("%d",&n);
  103.  
  104.                 printf("\\n Enter Frame Length : ");
  105.                 scanf("%d",&f);
  106.  
  107.                 printf("\\n Enter List of Reference : ");
  108.                 for(i=0;i<n;i++)
  109.                 {
  110.                     scanf("%d",&r[i]); // accept all references from user upto value of \'n\'
  111.                     }
  112.  
  113.                 for(i=0;i<f;i++)
  114.                 {
  115.                     page[i]=-1;      // initially set all values as \'-1\'  in frame
  116.                     ind[i]=0;
  117.                     }
  118.  
  119.  
  120.                 for(i=0;i<n;i++)      //upto the no. of length
  121.                 {
  122.                     flag=0;      //for new values set flag=0
  123.  
  124.                     for(j=0;j<f;j++) //upto frame length
  125.                     {
  126.                         if(r[i]==page[j]) // if reference value & page value  is eqaul
  127.                         {
  128.                             t++;    //increament the value of t
  129.                             ind[j]=t;  //copy the value into index
  130.                             flag=1;    //initialize flag,because the value is replaced
  131.                             break;
  132.                             }
  133.                         }
  134.  
  135.                     if(flag==0) //to add the new value
  136.                     {
  137.                         pf++;  // increment the value of page fault
  138.                         temp=n+1;
  139.  
  140.                         for(k=0;k<f;k++) //check upto the frame length
  141.                         {
  142.                             if(temp>ind[k])  //if new value is greater than index
  143.                             {
  144.                                 temp=ind[k]; //copy the value of index
  145.                                 sel=k;
  146.                                 }
  147.                             }
  148.                         page[sel]=r[i]; //update the index of \'page\' variable
  149.                         t++;
  150.                         ind[sel]=t;
  151.                         }
  152. /*                  else
  153.                     {
  154.                         pf++;
  155.                         }
  156. */                  printf("\\n");
  157.  
  158.                     for(p=0;p<f;p++)
  159.                     {
  160.                         printf(" -- %d -- ",page[p]); // print the elements present in the frame for every pass
  161.                         }
  162.                     }
  163.                 printf("\\n\\n No. of Page Fault : %d \\n",pf);  //display total no. of page fualts
  164.                 pf=0;
  165.  
  166.                 break;
  167.  
  168.             case 3 :
  169.                 printf("\\n Enter Length : ");
  170.                 scanf("%d",&n);
  171.  
  172.                 printf("\\n Enter Frame Length : ");
  173.                 scanf("%d",&f);
  174.  
  175.                 printf("\\n Enter List of Reference : ");
  176.                 for(i=0;i<n;i++)
  177.                 {
  178.                     scanf("%d",&r[i]); // accept all references from user upto value of \'n\'
  179.                     }
  180.  
  181.                 for(i=0;i<f;i++)
  182.                 {
  183.                     page[i]=-1;      // initially set all values as \'-1\'  in frame
  184.                     ind[i]=n+1;
  185.                     }
  186.  
  187.  
  188.                 for(i=0;i<n;i++)
  189.                 {
  190.                     flag=0; //to add new value set flag=0
  191.  
  192.                     for(j=0;j<f;j++)  // upto frame length
  193.                     {
  194.                         if(r[i]==page[j])  // check whether the reference value & value present in the frame \'page\' is eqaul
  195.                         {
  196.                             found=0; // if same then set \'found\'=0
  197.  
  198.                             for(s=i+1;s<n;s++) //check from 1st index to last index
  199.                             {
  200.                                 if(r[i]==r[s]) //if value is already used then
  201.                                 {
  202.                                     ind[j]=s;  //copy that index(\'s\') into variabe \'ind\'
  203.                                     found=1;   //reset the found as \'1\'
  204.                                     break;
  205.                                     }
  206.                                 }
  207.  
  208.                             if(found==0) //if reference value & page value is same then
  209.                             ind[j]=n+1; //add the 1st index value(of length) into \'ind\'
  210.  
  211.                             flag=1; //reset flag
  212.                             break;
  213.                             }
  214.                         }
  215.                     if(flag==0) //now if new value is inserted then
  216.                     {
  217.                         pf++;   // increment the value of page fault
  218.                         temp=-1 ;  //initialize temp to -1
  219.  
  220.                         for(k=0;k<f;k++) //upto frame length
  221.                         {
  222.                             if(temp<ind[k])//if value is less than index value
  223.                             {
  224.                                 temp=ind[k];  //then copy the value
  225.                                 sel=k;
  226.                                 }
  227.                             }
  228.                         page[sel]=r[i]; //update the index of \'page\'
  229.                         found=0;
  230.  
  231.                         for(s=i+1;s<n;s++)  //upto no. of length
  232.                         {
  233.                             if(r[i]==r[s])  //if value of index are same
  234.                             {
  235.                                 ind[sel]=s; //then copy that index int \'ind\'
  236.                                 found=1;
  237.                                 break;
  238.                                 }
  239.                             }
  240.  
  241.                          if(found==0)  //if values are not same then
  242.                          {
  243.                             ind[sel]=n+1;    //copy n+1 value into \'ind\' variable
  244.                             }
  245.                         }
  246.  
  247.                     printf("\\n\\n");
  248.  
  249.                     for(p=0;p<f;p++)
  250.                     {
  251.                         printf(" -- %d -- ",page[p]); // print the elements present in the frame for every pass
  252.                         }
  253.                     }
  254.                 printf("\\n\\n No. of Page Fault : %d \\n",pf); //display total no. of page fualts
  255.                 pf=0;
  256.  
  257.                 break;
  258.  
  259.             case 4 :
  260.                 printf("\\n Exit..!");
  261.                 break;
  262.  
  263.             default :
  264.                 printf("\\n Invalid Choice..!");
  265.             }
  266.         }while(ch!=4);
  267.     }
  268.  
  269. ------------------------
  270. END OF THE PROGRAM
  271. ------------------------
  272. /*
  273. ------------------------
  274.     OUTPUT
  275. ------------------------
  276.  
  277. gescoe@gescoe-Vostro-230:~/Desktop/TE$ gcc a7.c
  278. gescoe@gescoe-Vostro-230:~/Desktop/TE$ ./a.out
  279.  
  280.          MENU
  281.  
  282.  1. FIFO :
  283.  2. LRU :
  284.  3. OPTIMAL :
  285.  4. EXIT :
  286.  
  287.  Enter Your Choice :
  288. 1
  289.  
  290.  Enter Length : 5
  291.  
  292.  Enter Frame Length : 3
  293.  
  294.  Enter List of Reference : 2
  295. 3
  296. 2
  297. 1
  298. 5
  299.  
  300.  
  301.  -- 2 --  -- -1 --  -- -1 --
  302.  
  303.  -- 2 --  -- 3 --  -- -1 --
  304.  
  305.  -- 2 --  -- 3 --  -- -1 --
  306.  
  307.  -- 2 --  -- 3 --  -- 1 --
  308.  
  309.  -- 5 --  -- 3 --  -- 1 --
  310.  
  311.  No. of Page Fault : 4
  312.  
  313.  
  314.          MENU
  315.  
  316.  1. FIFO :
  317.  2. LRU :
  318.  3. OPTIMAL :
  319.  4. EXIT :
  320.  
  321.  Enter Your Choice : 2
  322.  
  323.  Enter Length : 5
  324.  
  325.  Enter Frame Length : 3
  326.  
  327.  Enter List of Reference : 2
  328. 3
  329. 2
  330. 1
  331. 5
  332.  
  333.  -- 2 --  -- -1 --  -- -1 --
  334.  -- 2 --  -- 3 --  -- -1 --
  335.  -- 2 --  -- 3 --  -- -1 --
  336.  -- 2 --  -- 3 --  -- 1 --
  337.  -- 2 --  -- 3 --  -- 5 --
  338.  
  339.  No. of Page Fault : 4
  340.  
  341.  
  342.          MENU
  343.  
  344.  1. FIFO :
  345.  2. LRU :
  346.  3. OPTIMAL :
  347.  4. EXIT :
  348.  
  349.  Enter Your Choice : 3
  350.  
  351.  Enter Length : 8
  352.  
  353.  Enter Frame Length : 3
  354.  
  355.  Enter List of Reference : 2
  356. 3
  357. 2
  358. 1
  359. 5
  360. 3
  361. 2
  362. 5
  363.  
  364.  
  365.  -- 2 --  -- -1 --  -- -1 --
  366.  
  367.  -- 2 --  -- 3 --  -- -1 --
  368.  
  369.  -- 2 --  -- 3 --  -- -1 --
  370.  
  371.  -- 2 --  -- 3 --  -- 1 --
  372.  
  373.  -- 2 --  -- 3 --  -- 5 --
  374.  
  375.  -- 2 --  -- 3 --  -- 5 --
  376.  
  377.  -- 2 --  -- 3 --  -- 5 --
  378.  
  379.  -- 2 --  -- 3 --  -- 5 --
  380.  
  381.  No. of Page Fault : 4
  382.  
  383.  
  384. ------------------------
  385. EXIT
  386. ------------------------
  387. */
');