Perl program to simulate Least Recently Loaded paging


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl program to simulate Least Recently Loaded paging
# 1  
Old 09-27-2011
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
Code:
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

Last edited by pludi; 09-27-2011 at 05:36 AM..
# 2  
Old 09-27-2011
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)
# 3  
Old 09-27-2011
I dont how to make list like F1 F2 F3 (F = frame ) if usre enter more frame it should add one more
# 4  
Old 09-27-2011
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

Code:
#!/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

This User Gave Thanks to Skrynesaver For This Post:
# 5  
Old 09-27-2011
OMG, Thank you soo much for ur help, I own u one time Smilie Smilie
# 6  
Old 09-27-2011
oops I forgot to generalise the frame length when setting the values.
Code:
#!/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++;
}

This User Gave Thanks to Skrynesaver For This Post:
# 7  
Old 09-27-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Shell Programming and Scripting

Perl program

can anyone help me out to write a code by connecting to the sql database and I need to print the list of tables present in the databse. any ideas please. (1 Reply)
Discussion started by: ramkumar15
1 Replies

3. Shell Programming and Scripting

Help regarding a Perl Program

I want to traverse a durectory for a particular file. Situataion is like this. Path is ABC/a/c/g. it has around 100 folders in it. Search a directory which has word "*latest*" in its path. and then from the latest go through z/x/c to file final.html. In total, i want it to go through... (4 Replies)
Discussion started by: hemasid
4 Replies

4. Shell Programming and Scripting

Putting two perl scripts together... triming whitespace off of recently parsed file

Thanks to people's help, I have composed a single line within a .sh script that Ports a file into a csv: perl -p -i -e... (5 Replies)
Discussion started by: Astrocloud
5 Replies

5. Programming

Perl program

Hi I am new to perl, i need to write a program to convert horizontal words to vertical eg: cat, dog, cow,.....(text file) this should be written as 1.cat 2.dog like this. can u pls help me to work out.. (4 Replies)
Discussion started by: nitha
4 Replies

6. Programming

Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX

Writing a Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX I have over the years come across the same issue a couple of times, and it normally is that the read speed on SAN is absolutely atrocious when doing non-sequential I/O to the disks. Problem being of... (7 Replies)
Discussion started by: vrghost
7 Replies

7. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

8. Shell Programming and Scripting

perl program

could i get any help with how to link this program together. i dont know what to put where the X's are print `flush`; thank(); #print thank header use Getopt::Std; # use declaration with the options function getopts("ld:") or usage() and exit; ... (3 Replies)
Discussion started by: livewire06
3 Replies

9. Shell Programming and Scripting

perl program

I wish to write a Perl program that will provide a listing of files in a directory. The files must be listed in sorted order by the file name. By default, the program displays only file names. By default, the program lists the files in the current directory. The program must provide the... (2 Replies)
Discussion started by: livewire06
2 Replies

10. Linux

perl update while script is loaded

Hi All, What will happen to the perl script loaded on the memory if I do perl update? Thanks. (1 Reply)
Discussion started by: itik
1 Replies
Login or Register to Ask a Question

Featured Tech Videos