In my OS class, we’ve started talking about some page-replacement algorithms, including FIFO, LRU, and clock. These are going to be on an upcoming exam, and so Mike (our prof) suggested that we make up our own page access sequences, and then run the algorithm. That’s all well and good, but without knowing whether or not you got it right, problems might arise.

Enter Ruby – make a quick little script that will run the algorithm for you, so as to check your results.

fifo.rb
lru.rb

Usage:

fifo.rb "123412512345" 3 # Run the sequence provided with a queue size of 3
lru.rb "293847101928" 5 # Run the provided sequence with a stack size of 5

All the page numbers do have to be single characters, but letters and symbols are also accepted.

Sample output:

Dan-Lecocqs-Mac:Desktop dlecocq$ ./lru.rb "123412512345" 4
1 * 1
2 * 2 1
3 * 3 2 1
4 * 4 3 2 1
1 1 4 3 2
2 2 1 4 3
5 * 5 2 1 4
1 1 5 2 4
2 2 1 5 4
3 * 3 2 1 5
4 * 4 3 2 1
5 * 5 4 3 2
Page faults: 8 / 12

Please note that you’ll have to change your shebang line to point to the path of your Ruby interpreter. I found Ruby in lots of places on my system, but check in your path. (Note: I’m using Ruby 1.8.4, but I don’t think I’m using a lot, or any, recent-version specific functionality)

If you’re not running it with “ruby fifo.rb …”, make sure you’ve chmod’d it to be executable.

This is our first assignment (due this coming Wednesday), and seems relatively appropriate. I’ll let Wikipedia give you background.

I pounded it out in about 10-20 minutes in Ruby, and seems like a good chance for me to refresh my Dr. Scheme (it’s been about a year, except for a pop quiz from Matt Matteson). Although it’s really not a difficult problem to solve nor that much of a feat to code for it, I really enjoy the simplicity of the solution. It reminded me a lot of a really elegant solution to the Tower Of Hanoi problem that Prof. Rader showed us in Data Structures.

Additionally, it’s always fun to take first steps in a buzz-word-titled class: Artificial Intelligence.

Tagged with: