Question

Implement the frame replacement algorithm for virtual memory Assume a computer system have 10 memory frames available inside the physical memory and is required to execute a process containing 20 pa...

  1. Implement the frame replacement algorithm for virtual memory

Assume a computer system have 10 memory frames available inside the physical memory and is required to execute a process containing 20 pages. Assume a process P has been executed in the system and produced a sequence of 40 page demands as follows:

Page demands trace of process P

Demand

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

cont.

Page

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

below

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

1

2

1

1

2

6

7

8

9

1

5

6

7

7

11

15

16

11

20

2

Implement the following frame replacement algorithms (either in separated or a single Java program):

  • First-in-first-out (FIFO) – select the candidate that is the first one that entered a frame
  • Least-recently-used (LRU) – select the candidate that is the least referred / demanded
  • Optimal – select the candidate based on future reference which such process will be the least immediately referred / demanded.

The program for each algorithm should allow the user to specify:

  • the total of frames provided in memory (F)
  • the total of page demands (N)
  • a list of N page demands.

Use the demands’ sequence to calculate the number of page faults produced by each algorithm (or your program may ask the input once and use the same input for all the algorithms).

Using the example given (10 frames, 40 page demands), compare which of the algorithms produce the least amount of page faults for process P.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

FIFO Algorithm

#include <stdio.h>
main(){
   int reference_string[100],i=0;
   printf("Enter Reference String:\nEnter -1 to exit:\nEnter No:");
   scanf("%d",&reference_string[i]);
   while(reference_string[i]!=-1){
          i++;
          printf("Enter No:");
          scanf("%d",&reference_string[i]);
   }
   int n,j=0,k,a[n],pf=0,ph=0,flag=0;
   printf("Enter no of frames:");
   scanf("%d",&n);
   printf("\t\t\n\n----------------\n\t\t reference string ");
   i = 0;
   while(reference_string[i] != -1){
           printf("%d ",reference_string[i]);
           i++;
   }
   printf("\n\n");
   for(i=0;i<n;i++){
       a[i]=-1;
   }
   printf("   |");
   for(i=0;i<n;i++){
      printf(" F%d",i+1);
   }
   printf(" status\n");
   for(i=0;reference_string[i] != -1;i++){
           flag = 0;
           for(k=0;k<n;k++){
               if(a[k]!=-1)
                printf("%2d ",a[k]);
               else
                printf("   ");
               if(a[k]==reference_string[i]){
                   flag=1;
                   ph++;
               }
           }
           if(flag==0){
               printf("PF\n");
               pf++;
               a[j]=reference_string[i];
               j=(j+1)%n;
           }else{
                  printf("PH\n");
           }
   }
   printf("No of page faults = %d\n",pf);
    printf("No of page hits = %d",ph);
}

LRU Algorithm

#include <stdio.h>
#include <string.h>
main(){
   int reference_string[100],i=0,min,full=0;
   printf("Enter Reference String:\nEnter -1 to exit:\nEnter No:");
   scanf("%d",&reference_string[i]);
   while(reference_string[i]!=-1){
       i++;
       printf("Enter No:");
       scanf("%d",&reference_string[i]);
   }
   int n,j=0,k;
   printf("Enter no of frames:");
   scanf("%d",&n);
   int a[n],st[n],pf=0,ph=0,flag=0;
   for(i=0;i<n;i++){
       a[i]=-10;
   }
   printf("Enter Reference String:\t");
   for(i=0;reference_string[i]!=-1;i++){
       printf("%d ",reference_string[i]);
   }
   printf("\n\n\n");
   for(i=0;i<n;i++){
       printf(" F%d",i+1);
   }
   printf(" status\n");
   int g,pos;
   for(i=0;reference_string[i]!=-1;i++){
       flag=0;
       for(k=0;k<n;k++){
           if(a[k]!=-10){
               printf("%2d ",a[k]);
           }
           else
               printf("   ");
           if(a[k]==reference_string[i]){
               flag=1;
               ph++;
           }
       }
       if(flag==0){
           printf("PF\n");
               for(g=0;g<n;g++){
                       st[g] = -1;
                       for(k=i;k>0;k--) {
                           if(a[g] == reference_string[k]){
                               st[g] = k;
                               break;      
                           }  
                       }
                   }
               min=st[0];
               pos = 0;
               for(k=0;k<n;k++){
                   if(st[k]<min){
                       min = st[k];
                       pos=k;
                   }
               }
           pf++;
           a[pos]=reference_string[i];
       }else{
           printf("PH\n");
       }
   }
   printf("No of page faults = %d\n",pf);
   printf("No of page hits = %d",ph);
}

OPT Algorithm

#include <stdio.h>
#include <string.h>
main(){
   int reference_string[100],i=0,min,full=0;
   printf("Enter Reference String:\nEnter -1 to exit:\nEnter No:");
   scanf("%d",&reference_string[i]);
   int z =0;
   while(reference_string[i]!=-1){
       i++;
       z++;
       printf("Enter No:");
       scanf("%d",&reference_string[i]);
   }
   int n,j=0,k;
   printf("Enter no of frames:");
   scanf("%d",&n);
   int a[n],st[n],pf=0,ph=0,flag=0;
   for(i=0;i<n;i++){
       a[i]=-10;
   }
   printf("Enter Reference String:\t");
   for(i=0;reference_string[i]!=-1;i++){
       printf("%d ",reference_string[i]);
   }
   printf("\n\n\n");
   for(i=0;i<n;i++){
       printf(" F%d",i+1);
   }
   printf(" status\n");
   int g,pos;
   for(i=0;reference_string[i]!=-1;i++){
       flag=0;
       int flag3 = 0;
       for(k=0;k<n;k++){
           if(a[k]!=-10){
               printf("%2d ",a[k]);
           }
           else
               printf("   ");
           if(a[k]==reference_string[i]){
               flag=1;
               ph++;
           }
       }
       if(flag==0){
           printf("PF\n");
               for(g=0;g<n;g++){
                       st[g] = -1;
                       for(k=i+1;k<z;k++) {
                           if(a[g] == reference_string[k]){
                               st[g] = k;
                               break;      
                           }  
                       }
                   }
               for(j = 0; j < n; j++){
                    if(st[j] == -1){
                        pos = j;
                        flag3 = 1;
                        break;
                    }
                }
                if(flag3 == 0) {
                   min=st[0];
                   pos = 0;
                   for(k=0;k<n;k++){
                       if(st[k]>min){
                           min = st[k];
                           pos=k;
                       }
                   }
               }
           pf++;
           a[pos]=reference_string[i];
       }else{
           printf("PH\n");
       }
   }
   printf("No of page faults = %d\n",pf);
   printf("No of page hits = %d",ph);
}

Add a comment
Know the answer?
Add Answer to:
Implement the frame replacement algorithm for virtual memory Assume a computer system have 10 memory frames available inside the physical memory and is required to execute a process containing 20 pa...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Assume a virtual memory system with the following properties: • The physical memory available to...

    3. Assume a virtual memory system with the following properties: • The physical memory available to an application consists of four-page frames, with frame numbers 0, 1, 2, and 3. The application has six pages of data in its virtual address space, using page numbers 0, 1, 2, 3, 4, and 5. Build a table showing which frame is used to bring in each page for the following sequences of page references using FIFO, MIN, and LRU as page replacement...

  • Answer the following questions based on the Memory Management tasks of an Operating System. 5 Consider...

    Answer the following questions based on the Memory Management tasks of an Operating System. 5 Consider the following sequence of page references: 5, 6, 7, 6, 8, 7, 9, 5, 9, 6, 8, 9 Assuming that all frames are initially empty, indicate the contents of memory after each reference, and how many page faults are found for each of the following page replacement algorithms: First In First Out (FIFO):                                   [2 .5 M]                                               Reference String 5 6 7 6 8 7...

  • Name Olbinna COSC414/514 Quiz3 (1) Suppose a computer has 4 physical pages, and a process referen...

    Name Olbinna COSC414/514 Quiz3 (1) Suppose a computer has 4 physical pages, and a process references its virtual pages (page o through page 7) in the following order: 021354637473355311172341 Also suppose the first four pages have been loaded in the physical memory as the following figure: Frame 0 Frame 1 Frame 2 Frame 3 a. Suppose the kernel uses First-In-First-Out (FIFO) page replacement algorithm. How many page faults would the process have? Which page references are page faults? b. Suppose...

  • Consider a computer system that uses a paging system. The memory contains 16 frames, each frame...

    Consider a computer system that uses a paging system. The memory contains 16 frames, each frame can accommodate 512 memory locations (size of frame = 512). The page table is as follows: Page Number 0 1 2 3 4 5 6 7 Frame Number 5 3 10 0 2 9 11 14 What are the physical addresses for the following logical addresses? Show your work Logical Address Physical Address 2345 1024 6780

  • Java Looking for some help on getting this started. Directions below. Operating systems designers have long...

    Java Looking for some help on getting this started. Directions below. Operating systems designers have long used simulation as an inexpensive way to test new algorithms. In this assignment you write a Java program that prompts the user for simulation data then runs a selected page replacement algorithm on that system, reporting frame assignments and total page faults. Instructions Your program will prompt the user first for the number of frames to be used in the simulation, then for a...

  • This is JAVA programing problem. Please give me all necessary screenshot and comments. (I use Ecl...

    This is JAVA programing problem. Please give me all necessary screenshot and comments. (I use Eclipse) Thanks Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter 8 of your text. First generate a random page-reference string (this should be 20 entries long) where page numbers range from 0 to 9. Apply the random page-reference string to each algorithm, and record the number of page faults incurred by each algorithm. Implement the replacement algorithms...

  • Exercise l: Suppose that we have a virtual memory space of 28 bytes for a given process and physical memory of 4 page frames. There is no cache. Suppose that pages are 32 bytes in length. 1) How...

    Exercise l: Suppose that we have a virtual memory space of 28 bytes for a given process and physical memory of 4 page frames. There is no cache. Suppose that pages are 32 bytes in length. 1) How many bits the virtual address contain? How many bits the physical address contain? bs Suppose now that some pages from the process have been brought into main memory as shown in the following figure: Virtual memory Physical memory Page table Frame #...

  • 8. A system of 64K virtual memory with page size 4K is mapped to a 32K...

    8. A system of 64K virtual memory with page size 4K is mapped to a 32K main memory as shown below. page # 64K virtual Mem page # Mem 32K main frame # frame # 0 4K 0 2 3 0 4K 1 1 1 1 8 K 8 K 0 2 2 12 K 12 K 5 3 3 16 K 16 K 4 4 4 4 20 K 20 K 5 5 3 24 K 24 K 2...

  • QUESTION 1 Virtual Memory is a technique that makes excellent use of available space on a...

    QUESTION 1 Virtual Memory is a technique that makes excellent use of available space on a hard drive, to temporarily store data that would otherwise require massive amounts of main memory (RAM). True False 2 points    QUESTION 2 Which of these allocation schemes require the entire program to be loaded before execution can begin? I. Segmented/Demand paged II. Paged III. Segmented IV. Demand paged 2 points    QUESTION 3 Select the advantages of the First-In First-Out page replacement policy...

  • Q1. Suppose we have a virtual memory of size 2 Terabytes (2048GB, or 241 bytes), where...

    Q1. Suppose we have a virtual memory of size 2 Terabytes (2048GB, or 241 bytes), where pages are 8KB (213 bytes) each, and the machine has 4GB (232 bytes) of physical memory.                      a) Compute the number of page table entries needed if all the pages are being used.                       b) Compute the size of the page table if each page table entry also required 4 additional bits (valid, protection, dirty, use). Q2. For this problem, you are given a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT