Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. using namespace std;
  7. #define MAX 100
  8. vector<int>adjlist[MAX];
  9. vector<int>position;
  10. int cashhit = 0,pagefault = 0,current_frame_size = 0;
  11. int *frame;
  12. int framesize;
  13.  
  14. int toDigit(char ch){
  15. return ch-'0';
  16. }
  17. void Erase(int index){
  18. vector<int>temp;
  19. for(int i = 1;i<adjlist[index].size();i++){
  20. temp.push_back(adjlist[index][i]);
  21. }
  22. adjlist[index] = temp;
  23. }
  24. void Insert(int new_process,int originalIndex){
  25. if(current_frame_size<=2){
  26. frame[current_frame_size] = new_process;
  27. ++current_frame_size;
  28. Erase(new_process);
  29. }
  30. else{
  31. int optimal,indexofOptimal,mx = -1,emptyIndex = -1;
  32. for(int i = 0;i<framesize;i++){
  33. int current_process = frame[i];
  34. if(current_process==new_process){
  35. ++cashhit;
  36. Erase(new_process);
  37. position.push_back(originalIndex+1);
  38. return;
  39. }
  40. else{
  41. if(!adjlist[current_process].empty()){
  42. if(adjlist[current_process][0]>mx){
  43. mx = adjlist[current_process][0];
  44. optimal = current_process;
  45. indexofOptimal = i;
  46. }
  47. }
  48. else{
  49. emptyIndex = i;
  50. }
  51. }
  52. }
  53. if(emptyIndex!=-1){
  54. frame[emptyIndex] = new_process;
  55. }
  56. else{
  57. Erase(new_process);
  58. frame[indexofOptimal] = new_process;
  59. }
  60. }
  61. }
  62. int main(){
  63. cout<<"Enter Frame Size: ";
  64. cin>>framesize;
  65. cin.ignore();
  66. frame = (int* )calloc(framesize,sizeof (int));
  67. string input;
  68. int mx = INT_MIN;
  69. cout<<"Enter Reference String: ";
  70. getline(cin,input);
  71. for(int i = 0;i<input.size();i++){
  72. int value = toDigit(input[i]);
  73. mx = max(mx,value);
  74. adjlist[value].push_back(i);
  75. }
  76. cout<<"Condition of Cache Memory"<<endl;
  77. for(int i = 0;i<input.size();i++){
  78. int process = toDigit(input[i]);
  79. Insert(process,i);
  80. cout<<"After Inserting process # "<<process<<"- ";
  81. for(int j = 0;j<3;j++){
  82. cout<<frame[j]<<" ";
  83. }
  84. }
  85. cout<<endl<<endl;
  86. cout<<"Page Fault: "<<input.size()-cashhit<<endl;
  87. cout<<"Cash Hit: "<<cashhit<<endl;
  88. cout<<"Cash Hit Position: ";
  89. for(int v : position){
  90. cout<<v<<" ";
  91. }
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement