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

		MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit
 1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 
5 2 3 4 

pagefaults = 5

		MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit
 1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 
5 2 3 4 

pagefaults = 10

		MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit
 1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 
5 2 3 4 

pagefaults = 5

		MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit