4.2 Schedulers and Events

The simulator is an event-driven simulator. There are presently four schedulers available in the simulator, each of which is implemented using a different data structure: a simple linked-list, heap, calendar queue (default), and a special type called ``real-time''. Each of these are described below. The scheduler runs by selecting the next earliest event, executing it to completion, and returning to execute the next event.Unit of time used by scheduler is seconds. Presently, the simulator is single-threaded, and only one event in execution at any given time. If more than one event are scheduled to execute at the same time, their execution is performed on the first scheduled - first dispatched manner. Simultaneous events are not re-ordered anymore by schedulers (as it was in earlier versions) and all schedulers should yeild the same order of dispatching given the same input.

No partial execution of events or pre-emption is supported.

An event generally comprises a ``firing time'' and a handler function. The actual definition of an event is found in ~ns/scheduler.h:

        class Event { 
        public: 
                Event* next_;           /* event list / 
                Handler* handler_;      /* handler to call when event ready /
                double time_;           /* time at which event is ready /
                int uid_;               /* unique ID /
                Event() : time_(0), uid_(0) {}
        };   
        /*   
         * The base class for all event handlers.  When an event's scheduled
         * time arrives, it is passed to handle which must consume it.
         * {\ie, if it needs to be freed it, it must be freed by the handler.}
         */  
        class Handler {
         public: 
                virtual void handle(Event* event);
        };
Two types of objects are derived from the base Event../ns-2/scheduler.cc: packets and ``at-events''. Packets are described in detail in the next chapterChaptersec:packetclass. An at-event is a tcl procedure execution scheduled to occur at a particular time. This is frequently used in simulation scripts. A simple example of how it is used is as follows:
        \ldots
        set ns_ [new Simulator]
        $ns_ use-scheduler Heap
        $ns_ at 300.5 "$self complete_sim"
        \ldots
This tcl code fragment first creates a simulation object, then changes the default scheduler implementation to be heap-based (see below), and finally schedules the function $self complete_sim to be executed at time 300.5 (seconds)(Note that this particular code fragment expects to be encapsulated in an object instance procedure, where the appropriate reference to $self is correctly defined.). At-events are implemented as events where the handler is effectively an execution of the tcl interpreter.



Subsections
Tom Henderson 2014-12-17