fork download
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int i,j,flag,page[10],frame[10],n,no,arr[50],time[50],k,min_time,hit=0,temp;
  5. printf("\nEnter the number of pages");
  6. scanf("%d",&n);
  7. printf("\nEnter the reference string");
  8. for(i=0;i<n;i++)
  9. {
  10. printf("\nValue of page%d: ",i+1);
  11. scanf("%d",&page[i]);
  12. }
  13. printf("\nEnter the number of frames");
  14. scanf("%d",&no);
  15. for(i=0;i<no;i++)
  16. {
  17. frame[i]=-1;
  18. }
  19. for(i=0;i<50;i++)
  20. {
  21. arr[i]=0;
  22. }
  23. for(i=0;i<n;i++)
  24. {
  25. arr[page[i]]++;
  26. time[page[i]]=i;
  27. flag=1;
  28. k=frame[0];
  29. for(j=0;j<no;j++)
  30. {
  31. if(frame[j]==-1 || frame[j]==page[i])
  32. {
  33. if(frame[j]!=-1)
  34. {
  35. hit++;
  36. }
  37. flag=0;
  38. frame[j]=page[i];
  39. break;
  40. }
  41. if(arr[k]>arr[frame[j]])
  42. {
  43. k=frame[j];
  44. }
  45. }
  46. if(flag)
  47. {
  48. min_time=50;
  49. for(j=0;j<no;j++)
  50. {
  51. if(arr[frame[j]]==arr[k] && time[frame[j]]<min_time)
  52. {
  53. temp=j;
  54. min_time=time[frame[j]];
  55. }
  56. }
  57. arr[frame[temp]]--;
  58. frame[temp]=page[i];
  59. }
  60. printf("\n");
  61. for(j=0;j<no;j++)
  62. {
  63. printf("%d\t",frame[j]);
  64. }
  65. }
  66. printf("\nthe number of page faults is:%d",n-hit);
  67. printf("\nthe number of page hit is:%d",hit);
  68. return 0;
  69.  
  70. }
  71.  
  72.  
  73.  
Success #stdin #stdout 0s 5476KB
stdin
15
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 
3
stdout
Enter the number of pages
Enter the reference string
Value of page1: 
Value of page2: 
Value of page3: 
Value of page4: 
Value of page5: 
Value of page6: 
Value of page7: 
Value of page8: 
Value of page9: 
Value of page10: 
Value of page11: 
Value of page12: 
Value of page13: 
Value of page14: 
Value of page15: 
Enter the number of frames
7	-1	-1	
7	0	-1	
7	0	1	
2	0	1	
2	0	1	
2	0	3	
2	0	3	
4	0	3	
4	0	2	
3	0	2	
3	0	2	
3	0	2	
3	0	2	
3	0	2	
3	0	2	
the number of page faults is:8
the number of page hit is:7