Advertisement
okpalan

Page Replacement working

Nov 26th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void fifo_management(int*,int);
  4. void lfu_management(int*,int,int*);
  5. void wm_with_freq(int*,int*);
  6. void wm_print(int* wm);
  7. int pointer=0; //for FIFO
  8. int frames=0;
  9. int main(){
  10. system("clear||cls");
  11. printf("Welcome to page replacement algorithms simulation\n");
  12. printf("Enter the number of frames allocated to the process:");
  13. scanf("%d",&frames);
  14. int * working_cache=malloc(sizeof(int*)*frames);
  15. int * page_freq=calloc(sizeof(int*),frames);
  16. printf("Frames allocated to the process successfully!\n");
  17. int req,ch;
  18. while(1){
  19. printf("Enter 1 to use FIFO management\n");
  20. printf("Enter 2 to use LFU management\n");
  21. printf("Enter your choice:");
  22. scanf("%d",&ch);
  23. if(ch==1 || ch==2) break;
  24. else printf("\nWrong choice!\n");
  25. }
  26. while(1){
  27. printf("Enter the page requested by the process(-1 to exit):");
  28. scanf("%d",&req);
  29. if(req==-1) break;
  30. if (ch==1) fifo_management(working_cache,req);
  31. else lfu_management(working_cache,req,page_freq);
  32. };
  33.  
  34. printf("thanks!\n");
  35. }
  36.  
  37. void fifo_management(int* work,int req){
  38. for (int i=0;i<frames;i++){
  39. if(req==work[i]){
  40. printf("\nHit!\n");
  41. return;
  42. }
  43. }
  44. printf("\nMiss!\n");
  45. work[pointer]=req;
  46. pointer=(pointer+1)%frames;
  47. wm_print(work);
  48.  
  49. }
  50.  
  51. void lfu_management(int*work,int req,int* page_freq){
  52. for (int i=0;i<frames;i++){
  53. if(req==work[i]){
  54. printf("\nHit!\n");
  55. page_freq[i]+=1;
  56. wm_with_freq(work,page_freq);
  57. return;
  58. }
  59. }
  60. printf("\nMiss!\n");
  61. int ind=0;
  62. int min=page_freq[0];
  63. for(int i=0;i<frames;i++){
  64. if(min>page_freq[i]) {
  65. min=page_freq[i];
  66. ind=i;
  67. }
  68. }
  69. work[ind]=req;
  70. page_freq[ind]=1;
  71. wm_with_freq(work,page_freq);
  72. }
  73. void wm_with_freq(int*wm,int* page){
  74. printf("\nCurrent frames with frequency:\n");
  75. for(int i=0;i<frames;i++){
  76. printf("%d : %d\n",wm[i],page[i]);
  77. }
  78. printf("************************\n\n");
  79. }
  80. void wm_print(int* wm){
  81. printf("\nCurrent frames:\n");
  82. for(int i=0;i<frames;i++){
  83. printf("%d\n",wm[i]);
  84. }
  85. printf("************************\n\n");
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement