Perl program to simulate Least Recently Loaded paging

Hi, If I want to write perl program that simulated Least Recently Loaded(LRL) to calculate page fault:
The user must input page access and number of page fram that need to be greater than 3
Example

Page Access: 5, 3, 4, 1, 7, 5, 3, 2, 3, 6, 7, 1, 4, 6
Frames: 3   
PA:     5 3 4 1 7 5 3 2 3 6 7 1 4 6
 F1:     5 5 5 1 1 1 3 3 3 3 3 1 1 1
 F2:     - 3 3 3 7 7 7 2 2 2 7 7 7 6
 F3:     - - 4 4 4 5 5 5 5 6 6 6 4 4
PF:  x x x x x x x x - x x x  x x

Page fault(X) = 13
PA is stand for page access
the one that not get count is when the number is duplicated

SO I think of FIFO queue to implement but I Dont really know how to do array like that

Not sure I follow your question, however to emulate a stack you can use the shift and unshift operations (perldoc -f shift to see how it is used)

I dont how to make list like F1 F2 F3 (F = frame ) if usre enter more frame it should add one more

Ahh, that's a bit more complex than the usual learning Perl question, at the risk of undoing the learning value of the exercise try the following

#!/usr/bin/perl
use strict;


# In a production system these should be validated before progessing, but for now...
print "Page access: ";
chomp(my @pa=split(/,\s+/,<STDIN>)); # assumes separated by comma and spaces as in your example
print "Frames: ";
chomp(my $frames=<STDIN>);


my @frames; # set up an array to hold an array for each frame. 
for (my$count=0;$count<$frames;$count++){ #Process the array of arrays (see perldoc perlref for details of using array references)
        for (my $index=0;$index<@pa;$index++){ #Deep copying the elements of the Page access into each frame array
                ${$frames[$count]}[$index]=$pa[$index];
        }
        @{$frames[$count]}[0 .. ($count - 1)]=split('',"-"x$count) if $count; #Set the leading irrellevant values to '-'
        for (my $index=$count;$index<@{$frames[$count]};$index+=$frames){ # go through the frame array by whatever frame size we set
                ${$frames[$count]}[$index +2 ] =${$frames[$count]}[$index+1]=$pa[$index];
        }
        #truncate the array at the size of @pa
        @{$frames[$count]}=@{$frames[$count]}[0..(@pa - 1)];
}
#Print out your results
my $count=1;
print "PA:\t",join(' ', @pa),"\n";
for my $frame_array_ref (@frames){
        print " F$count:\t",join(' ', @{$frame_array_ref}),"\n";
        $count++;
}
# I have no idea what the PF line represents, sorry
1 Like

OMG, Thank you soo much for ur help, I own u one time :slight_smile: :slight_smile:

oops I forgot to generalise the frame length when setting the values.

#!/usr/bin/perl
use strict;


# In a production system these should be validated before progessing, but for now...
print "Page access: ";
chomp(my @pa=split(/,\s+/,<STDIN>)); # assumes separated by comma and spaces as in your example
print "Frames: ";
chomp(my $frames=<STDIN>);


my @frames; # set up an array to hold an array for each frame. 
for (my$count=0;$count<$frames;$count++){ #Process the array of arrays (see perldoc perlref for details of using array references)
        for (my $index=0;$index<@pa;$index++){ #Deep copying the elements of the Page access into each frame array
                ${$frames[$count]}[$index]=$pa[$index];
        }
        @{$frames[$count]}[0 .. ($count - 1)]=split('',"-"x$count) if $count; #Set the leading irrellevant values to '-'
        for (my $index=$count;$index<@{$frames[$count]};$index+=$frames){ # go through the frame array by whatever frame size we set
                for my $x (1..($frames -1)){ #set $frame size elements
                        ${$frames[$count]}[$index + $x] =$pa[$index];
                }
        }
        #truncate the array at the size of @pa
        @{$frames[$count]}=@{$frames[$count]}[0..(@pa - 1)];
}
#Print out your results
my $count=1;
print "PA:\t",join(' ', @pa),"\n";
for my $frame_array_ref (@frames){
        print " F$count:\t",join(' ', @{$frame_array_ref}),"\n";
        $count++;
}
1 Like

Thank again, PF stand for page fault it use to cout the value of page access and as above example the one that when the number is duplicated is not count. but anyway am very considerate, Thank again