CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2002
    Posts
    53

    Scheduling Algorithm - Who Can Give Sample Program???

    DO YOU REMEMBER PAGE REPLACEMENT AGORITHMS???

    FIRST-IN-FIRST-OUT
    LEAST FREQUENTLY USED
    MOST FREQUENTLY USED

    CAN ANYONE PROVIDE ME SAMPLE PROGRAM WIH SOURCE CODE IN C OR IN C++ FOR THESE ALGORITHM?

    PLEASE IF YOU HAVE KINDLY EXTEND THEM TO ME...


    THANK YOU VERY MUCH

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Most Frequently Used is a new one - never heard of that before. You've missed out Least Recently Used. The algorithms are quite easy to implement anyway

    FIFO - just implement a queue
    LFU - increment page counter whenever a page is accessed. Take out the one with the smallest counter
    MFU - increment page counter whenever a page is accessed. Take out the one with the biggest counter
    LRU - increment a global counter whenever a page is accessed and set the page counter to that value. Take out the one with the lowest counter.

    This of course assumes that you won't page so much that it wraps past the maximum integer. It is OK for experimental purposes.
    Succinct is verbose for terse

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    You mean something like this, or did I understand your question totally wrong?
    Code:
    #include <list>
    #include <iostream>
    
    class basic_task
    {
    public:
    	virtual void operator()() = 0;
    	virtual ~basic_task(){};
    };
    
    class task_1:public basic_task
    {
    public:
    	virtual void operator()(){std::cout<<"task_1"<<std::endl;}
    	virtual ~task_1(){}
    };
    
    class task_2:public basic_task
    {
    public:
    	virtual void operator()(){std::cout<<"task_2"<<std::endl;}
    	virtual ~task_2(){}
    };
    
    class scheduler
    {
    	std::list<basic_task *> task_list;
    public:
    	void push(basic_task *t){task_list.push_back(t);}
    	void run(){
    		std::list<basic_task *>::iterator it;
    		for(it=task_list.begin(); it!=task_list.end(); ++it){
    			(**it)();
    			delete (*it);
    		}		
    		task_list.clear();
    	}
    };
    int main() 
    {
    	scheduler s;
    	s.push(new task_1());
    	s.push(new task_2());
    	s.push(new task_1());
    	s.push(new task_2());
    	s.run();
    	return 0;
    }
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    May 2002
    Posts
    53
    thanks to both of you!

    bright minds, bright ideas!


  5. #5
    Join Date
    May 2004
    Posts
    64

    Re: Scheduling Algorithm - Who Can Give Sample Program???

    How would I implement something like that in both a FIFO and LRU scenario while passing a string of predefined numbers and a set page file size. Like FIFO with string 1,2,3,4,5 and page file of say 3? And the same for LRU. I am not a programmer but would like to see how this works in a running simulation to help me better understand how it really works in an operating system. Thanks.

  6. #6
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020

    Re: Scheduling Algorithm - Who Can Give Sample Program???

    You should really make up your own thread referencing this one. While you're on this topic, you might like to look up Belady's anomaly.

    Can't give you a program but can make up a short example
    Code:
    Page
    1   LRU 1     FIFO 1
    2   LRU 2 1   FIFO 2 1
    1   LRU 1 2   FIFO 2 1
    3   LRU 3 1 2 FIFO 3 2 1
    1   LRU 1 3 2 FIFO 3 2 1
    4   LRU 4 1 3 FIFO 4 3 2
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured