Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------------------------------------
- Title - Write a Program to implement following scheduling algorithms:1. First in First Out,
- 2. Least Recently Used, & 3. Optimal Page Replacement.
- ----------------------------------------------------------------------------
- ------------------------
- MAIN PROGRAM a7.c
- ------------------------
- //header file declaration
- #include<stdio.h>
- #include<conio.h>
- //main function
- void main()
- {
- //variable declaration
- int ch,i,j,n,f,k,p,r[50],ind[50],t=0,page[10];
- int temp,flag=0,pf=0,sel=0;
- int found=0,s;
- clrscr();
- do
- {
- printf("\n\n\t MENU \n\n 1. FIFO : \n 2. LRU : \n 3. OPTIMAL : \n 4. EXIT : \n\n Enter Your Choice : ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1 :
- printf("\n Enter Length : ");
- scanf("%d",&n);
- printf("\n Enter Frame Length : ");
- scanf("%d",&f);
- printf("\n Enter List of Reference : ");
- for(i=0;i<n;i++)
- {
- scanf("%d",&r[i]); // accept all references from user upto value of 'n'
- }
- for(i=0;i<f;i++)
- {
- page[i]=-1; // initially set all values as '-1' in frame
- ind[i]=0;
- }
- for(i=0;i<n;i++)
- {
- flag=0; //set flag=0 because no. of frames are present
- for(j=0;j<f;j++) // check upto the length of frame
- {
- if(r[i]==page[j]) // if 'i' value is present in the frame 'page' then
- {
- flag=1; // set the flag to 1
- break;
- }
- }
- if(flag==0) //if new value is inserted in the frame
- {
- pf++; // increment the value of page fault
- temp=n+1;
- for(k=0;k<f;k++) //upto frame length
- {
- if(temp>ind[k]) //if inserted value is greater than index
- {
- temp=ind[k]; //copy the index value into temp
- sel=k; //copy new value into the variable 'sel'
- }
- }
- page[sel]=r[i]; //copy the reference value into page var.
- t++; //increament t
- ind[sel]=t;
- }
- /* else
- {
- pf++;
- }
- */ printf("\n\n");
- for(p=0;p<f;p++)
- {
- printf(" -- %d -- ",page[p]); // print the elements present in the frame for every pass
- }
- }
- printf("\n\n No. of Page Fault : %d \n",pf); // display total no. of page fualts
- pf=0;
- break;
- case 2 :
- printf("\n Enter Length : ");
- scanf("%d",&n);
- printf("\n Enter Frame Length : ");
- scanf("%d",&f);
- printf("\n Enter List of Reference : ");
- for(i=0;i<n;i++)
- {
- scanf("%d",&r[i]); // accept all references from user upto value of 'n'
- }
- for(i=0;i<f;i++)
- {
- page[i]=-1; // initially set all values as '-1' in frame
- ind[i]=0;
- }
- for(i=0;i<n;i++) //upto the no. of length
- {
- flag=0; //for new values set flag=0
- for(j=0;j<f;j++) //upto frame length
- {
- if(r[i]==page[j]) // if reference value & page value is eqaul
- {
- t++; //increament the value of t
- ind[j]=t; //copy the value into index
- flag=1; //initialize flag,because the value is replaced
- break;
- }
- }
- if(flag==0) //to add the new value
- {
- pf++; // increment the value of page fault
- temp=n+1;
- for(k=0;k<f;k++) //check upto the frame length
- {
- if(temp>ind[k]) //if new value is greater than index
- {
- temp=ind[k]; //copy the value of index
- sel=k;
- }
- }
- page[sel]=r[i]; //update the index of 'page' variable
- t++;
- ind[sel]=t;
- }
- /* else
- {
- pf++;
- }
- */ printf("\n");
- for(p=0;p<f;p++)
- {
- printf(" -- %d -- ",page[p]); // print the elements present in the frame for every pass
- }
- }
- printf("\n\n No. of Page Fault : %d \n",pf); //display total no. of page fualts
- pf=0;
- break;
- case 3 :
- printf("\n Enter Length : ");
- scanf("%d",&n);
- printf("\n Enter Frame Length : ");
- scanf("%d",&f);
- printf("\n Enter List of Reference : ");
- for(i=0;i<n;i++)
- {
- scanf("%d",&r[i]); // accept all references from user upto value of 'n'
- }
- for(i=0;i<f;i++)
- {
- page[i]=-1; // initially set all values as '-1' in frame
- ind[i]=n+1;
- }
- for(i=0;i<n;i++)
- {
- flag=0; //to add new value set flag=0
- for(j=0;j<f;j++) // upto frame length
- {
- if(r[i]==page[j]) // check whether the reference value & value present in the frame 'page' is eqaul
- {
- found=0; // if same then set 'found'=0
- for(s=i+1;s<n;s++) //check from 1st index to last index
- {
- if(r[i]==r[s]) //if value is already used then
- {
- ind[j]=s; //copy that index('s') into variabe 'ind'
- found=1; //reset the found as '1'
- break;
- }
- }
- if(found==0) //if reference value & page value is same then
- ind[j]=n+1; //add the 1st index value(of length) into 'ind'
- flag=1; //reset flag
- break;
- }
- }
- if(flag==0) //now if new value is inserted then
- {
- pf++; // increment the value of page fault
- temp=-1 ; //initialize temp to -1
- for(k=0;k<f;k++) //upto frame length
- {
- if(temp<ind[k])//if value is less than index value
- {
- temp=ind[k]; //then copy the value
- sel=k;
- }
- }
- page[sel]=r[i]; //update the index of 'page'
- found=0;
- for(s=i+1;s<n;s++) //upto no. of length
- {
- if(r[i]==r[s]) //if value of index are same
- {
- ind[sel]=s; //then copy that index int 'ind'
- found=1;
- break;
- }
- }
- if(found==0) //if values are not same then
- {
- ind[sel]=n+1; //copy n+1 value into 'ind' variable
- }
- }
- printf("\n\n");
- for(p=0;p<f;p++)
- {
- printf(" -- %d -- ",page[p]); // print the elements present in the frame for every pass
- }
- }
- printf("\n\n No. of Page Fault : %d \n",pf); //display total no. of page fualts
- pf=0;
- break;
- case 4 :
- printf("\n Exit..!");
- break;
- default :
- printf("\n Invalid Choice..!");
- }
- }while(ch!=4);
- }
- ------------------------
- END OF THE PROGRAM
- ------------------------
- /*
- ------------------------
- OUTPUT
- ------------------------
- gescoe@gescoe-Vostro-230:~/Desktop/TE$ gcc a7.c
- gescoe@gescoe-Vostro-230:~/Desktop/TE$ ./a.out
- MENU
- 1. FIFO :
- 2. LRU :
- 3. OPTIMAL :
- 4. EXIT :
- Enter Your Choice :
- 1
- Enter Length : 5
- Enter Frame Length : 3
- Enter List of Reference : 2
- 3
- 2
- 1
- 5
- -- 2 -- -- -1 -- -- -1 --
- -- 2 -- -- 3 -- -- -1 --
- -- 2 -- -- 3 -- -- -1 --
- -- 2 -- -- 3 -- -- 1 --
- -- 5 -- -- 3 -- -- 1 --
- No. of Page Fault : 4
- MENU
- 1. FIFO :
- 2. LRU :
- 3. OPTIMAL :
- 4. EXIT :
- Enter Your Choice : 2
- Enter Length : 5
- Enter Frame Length : 3
- Enter List of Reference : 2
- 3
- 2
- 1
- 5
- -- 2 -- -- -1 -- -- -1 --
- -- 2 -- -- 3 -- -- -1 --
- -- 2 -- -- 3 -- -- -1 --
- -- 2 -- -- 3 -- -- 1 --
- -- 2 -- -- 3 -- -- 5 --
- No. of Page Fault : 4
- MENU
- 1. FIFO :
- 2. LRU :
- 3. OPTIMAL :
- 4. EXIT :
- Enter Your Choice : 3
- Enter Length : 8
- Enter Frame Length : 3
- Enter List of Reference : 2
- 3
- 2
- 1
- 5
- 3
- 2
- 5
- -- 2 -- -- -1 -- -- -1 --
- -- 2 -- -- 3 -- -- -1 --
- -- 2 -- -- 3 -- -- -1 --
- -- 2 -- -- 3 -- -- 1 --
- -- 2 -- -- 3 -- -- 5 --
- -- 2 -- -- 3 -- -- 5 --
- -- 2 -- -- 3 -- -- 5 --
- -- 2 -- -- 3 -- -- 5 --
- No. of Page Fault : 4
- ------------------------
- EXIT
- ------------------------
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement