2. Time Driven Simulation
12 1 Simulation of Computer Networks
2 4
a 6
time
0 3 b c 5
Fig. 1.3. Clock advancement in a time-driven simulation
Stop and observer the system after
FIXED INTERVAL (∆)
Fig. 1.3 shows the basic idea behind time advancement in a time-driven
simulation. The curved arrows represent such advances, and a, b, and c
mark the occurrences of events. During the first interval, no event occurs.
Events (e.g, a, b,c) are assumed to
The second interval contains event a, which is not handled until the end of
the interval.
happen at the end of each interval
Time interval (∆) is an important parameter of time-driven simulation.
While a large interval can lead to loss of information, a small interval can
cause unnecessary waste of computational effort. Suppose, in Fig. 1.3, events
Implementation using “for loop”
b and c in the fifth interval are packet arrival and departure events, respec-
tively. Since these two events are considered to occur exactly at the end of
the interval (i.e., at 5∆), the system state would be as if there is no packet
www.ns2ultimate.com
arrival or departure events during [4∆, 5∆]. This is considered a loss of in-
3. Time Driven Simulation
Implementation using for loop
for t=0 to 100{
Execute the events
}
12 1 Simulation of Computer Networks
2 4
a 6
time
0 3 b c 5
Fig. 1.3. Clock advancement in a time-driven simulation
Fig. 1.3 shows the basic idea behind time advancement in www.ns2ultimate.com
a time-driven
4. • At the execution, an event may induce one or more events. The induced
events are stamped with the time when the event occurs, and again stored
in the event list. The timestamp of the induced events must not be less
than the simulation clock. This is to ensure that the simulation would
Event Driven Simulation
never go backward in time.
• An event-driven simulation starts with a set of initial events in the event
list. It runs until the list is empty or another stopping criterion is satisfied.
To the next event
time
0 a b c
Fig. 1.4. Clock advancement in an event-driven simulation
Fig. 1.4 shows a graphical representation of event-driven simulation. Here,
Move from one event to the next
events a, b, and c are executed in order. The time gap between any pair of
events is not fixed. The simulation advances from one event to another, as
opposed to one interval to another in time-driven simulation. In event-driven
NOT every fixed interval
simulation, programmers do not have to worry about optimizing time interval.
Example 1.2 Program 1.4.2 shows the skeleton of a typical event-driven
simulation program. Lines 1 and 2 initialize the system state variables and
the event list, respectively. Line 3 specifies a stopping criterion. Lines 4–6 are
How do we implement it?
executed as along as Line 3 returns true. Within this loop, the event whose
timestamp is smallest is retrieved, executed, and removed from the list. Then,
We can’t use the for loop!
the simulation clock is set to the time associated with the retrieved event.
1.5 A Simulation Example: A Single-Channel Queuing
System www.ns2ultimate.com
5. NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
Event Driven Simulation
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
We can use pointers and create
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
A Chain of Event
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
6. Implementation of Discrete-Event Simulation
in NS2
Characteristics of Event Driven
Simulation
An event has a pointer to the next event.
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
7. Implementation of Discrete-Event Simulation
in NS2
Characteristics of Event Driven
Simulation
An event has a pointer to the next event.
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
An event can induce new events
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
8. Implementation of Discrete-Event Simulation
in NS2
Characteristics of Event Driven
Simulation
An event has a pointer to the next event.
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
An event can induce new events
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
by using the “insert event” instruction
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
9. Implementation of Discrete-Event Simulation
in NS2
Main Components of Event Driven
Simulation
Events and Handlers
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
10. Implementation of Discrete-Event Simulation
in NS2
Main Components of Event Driven
Simulation
Events and Handlers
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
Simulation time line
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
11. Implementation of Discrete-Event Simulation
in NS2
Main Components of Event Driven
Simulation
Events and Handlers
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
Simulation time line
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
Scheduler:
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
- Supervises simulation
chronologically).1 In the next section, we will discuss the simulation concept of
Scheduler
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
- Keep track of simulation (virtual) time
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
- Insert/delete events
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
12. time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
Example Implementation
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
1. Prior to the simulation: Create 4 events
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
1
By execution, we mean taking actions associated with an event.
T. Issariyakul, E. Hossain, Introduction to Network Simulator NS2,
DOI: 10.1007/978-0-387-71760-9 4, c Springer Science+Business Media, LLC 2009
www.ns2ultimate.com
13. time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
Example Implementation
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
1. Prior to the simulation: Create 4 events
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
2. Main loop: Go to the first event
2.1 Execute the event
2.2 Go to the next event
1
By execution, we mean taking actions associated with an event.
2.3 Go back to (2.1) until no more events are in the chain of
T. Issariyakul, E. Hossain, Introduction to Network Simulator NS2,
DOI: 10.1007/978-0-387-71760-9 4, c Springer Science+Business Media, LLC 2009
events
www.ns2ultimate.com
14. time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
Example Implementation
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
1. Prior to the simulation: Create 4 events
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution 2. Main EventGo to the first event
time of loop: 5 is at 3.7 second).
2.1 Execute the event
2.2 Go to the next event
2.3 Go back to (2.1) until no more events are in the
chain of events
1
By execution, we mean taking actions associated with an event.
2.1.1. Event 1 induce Event 5 at t =3.7 s
T. Issariyakul, E. Hossain, Introduction to Network Simulator NS2,
2.1.2 The scheduler moves to the next event (i.e., Event 2)
DOI: 10.1007/978-0-387-71760-9 4, c Springer Science+Business Media, LLC 2009
www.ns2ultimate.com
15. For more
information about
Please see
this book
from Springer
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009
or visit www.ns2ultimate.com