Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stc++.h>
- using namespace std;
- #define ll long long
- #define smallestnum 1e18
- int findLRU(int time[], int n) {
- int i, minimum = time[0], pos = 0;
- for(i = 1; i < n; ++i) {
- if(time[i] < minimum) {
- minimum = time[i];
- pos = i;
- }
- }
- return pos;
- }
- int main() {
- int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[1], i, j, flag1, flag2;
- int faults= 0;
- printf("Enter number of frames: ");
- scanf("%d", &no_of_frames);
- printf("Enter number of pages: ");
- scanf("%d", &no_of_pages);
- //printf("Enter reference string: ");
- for(i = 0; i < no_of_pages; ++i) {
- //scanf("%d", &pages[i]);
- pages[i] = rand() % 10; // get input automatically with a random string
- }
- cout << endl;
- for(i = 0; i < no_of_frames; ++i) {
- frames[i] = -1;
- }
- for(i = 0; i < no_of_pages; ++i) {
- flag1 = flag2 = 0;
- for(j = 0; j < no_of_frames; ++j) {
- if(frames[j] == pages[i]) {
- counter++;
- time[j] = counter;
- flag1 = flag2 = 1;
- break;
- }
- }
- if(flag1 == 0) {
- for(j = 0; j < no_of_frames; ++j) {
- if(frames[j] == -1) {
- counter++;
- faults++;
- frames[j] = pages[i];
- time[j] = counter;
- flag2 = 1;
- break;
- }
- }
- }
- if(flag2 == 0) {
- int pos = findLRU(time, no_of_frames);
- counter++;
- faults++;
- frames[pos] = pages[i];
- time[pos] = counter;
- }
- printf("\n");
- for(j = 0; j < no_of_frames; ++j) {
- printf("%d\t", frames[j]);
- }
- }
- printf("\n\nTotal Page Faults = %d", faults);
- return 0;
- }
- // input: 3 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement