S-MAC Simulation Using NS2
S-MAC Simulation Using NS2
Introduction
1.1 Introduction
In the last decade, there is a rapid growth of wireless sensor network. It is the network consisting
of many small, low-power, and intelligent sensor nodes and one or more base stations. Wireless
sensor network consists of three main things called nodes (or motes), a processor and a radio
transceiver. A sensor node is a battery-powered device that is equipped with sensors such as
seismic, light, temperature, and acoustic. A processor for mapping a physical quantity taken from
the environment to a quantitative measurement as well as performing network protocol functions
and a radio transceiver for communication. Figure 1.1 shows the simple wireless sensor network.
2.3 Synchronization
2.3.1 Synchronization Related Components in S-MAC
SYNC Packet
As mentioned before, each S-MAC node needs to exchange its schedule by periodically
broadcasting a SYNC packet to its neighbors. The period of sending a SYNC packet is called the
synchronization period. The default value in ns-2 is 10 frames (one frame = one sleep and listen
cycle). Sending or receiving SYNC packets takes place in SYNC period. The definition of
SYNC packet frame in ns-2 is shown in Table 2.1.
Schedule Table
Each S-MAC node maintains one schedule table, which stores its own schedule and the
schedules of all its known neighbors. The establishment of the schedule table will be introduced
in the next subsection. There are two classes of schedules in the schedule table. We call the
schedule that the node itself is on primary schedule and other schedules in the schedule table
secondary schedules. A node may have no secondary schedule, for example, if all its neighbors
follow the same schedule. But every node should have a primary schedule. The maximum
number of records in the table is limited by a user-adjustable S-MAC parameter, which defines
the maximum number of different schedules. Table 2.2. shows how each field in the schedule
table is defined in ns2.
Table 2.2. Field definition of schedule table
Field Comment
txSync Flag indicating need to send sync
txData Flag indicating need to send data
numPeriods Counter for sending sync periodically
numNodes Number of nodes on this schedule
syncNode The node who initialized this schedule
chkSched Flag indicating need to check numNodes
Field Comment
nodeId ID of this node
schedId Schedule ID in schedule table that this node follows
active Flag indicating this node is active recently
state Flag indicating the node has changed schedule
2. This is not my first SYNC packet after I have chosen my first schedule.
We have to consider the following five possible situations. For simplification, we use N
representing the sender of this SYNC packet and S representing the schedule in the SYNC
packet. The algorithm is put in Table 2.4 below.
Table 2.4. General algorithm for processing SYNC packets
Condition Action
1 N is a known neighbor Update S in my schedule table by rescheduling its schedule
in my neighbor list and timer with the value of sleepTime in the SYNC packet. This
it has not changed its can eliminate clock drift between two nodes.
primary schedule since I
got its last SYNC
packet.
Step 2: Update the number of nodes on a certain schedule if its flag chkSched is set. This may
happen when I got a SYNC packet from one of my known neighbors, which had switched to
another schedule since I got its last SYNC packet, and I should decrease the number of nodes on
the schedule that this neighbor had switched from. But at that time I had a DATA sending
request, so I had to defer decreasing. And now is time to do it.
Step 3: Update neighbor list. Check each record in my neighbor list to see whether its active flag
is set or not.
If yes, the flag is reset. (The flag will probably be set again when I get this neighbor’s
SYNC packet next time before my next time for neighbor list updating arrives.)
If no, which means that I have not received the SYNC packet from this neighbor node for
a long time (at least a neighbor list updating period), this neighbor node has to be deleted
from my neighbor list. Accordingly, the number of nodes on the schedule that the deleted
neighbor used to follow should be decreased. If after decrease the number becomes 0, this
schedule has to be removed from my schedule table.
Step 4: If the inactive neighbor nodes that I dropped in step 3 are on my primary schedule, I have
to execute check_my_schedule, which has been introduced in Table 2.4 Part 2.
Fig. 2.6. Neighbor list updating period smaller than synchronization period
From the above table, we can see that one node uses its NAV for virtual carrier sense when it
interferes in one of its neighbor’s transmission, and uses its Neighbor NAV when it is either a
sender or a receiver during a transmission process. Besides their functions for virtual carrier
sense, the NAVs also play other roles in S-MAC listed as below.
1. Neighbor NAV will act as a timer for DATA timeout on the node, which has sent out the
CTS packet and is waiting for the arrival of the DATA packet.
2. Adaptive listening (to be introduced in latter parts of this chapter) will be triggered when
either of the two NAVs counts down to zero.
S-MAC obtains a transmission chance through contending for the medium in a contention
window. Going back to Fig. 2.2, we find that there are two contention windows defined in one
frame. One is for sending SYNC packets in the SYNC period and the other is for sending DATA
packets in the DATA period. Both contention windows have fixed size (fixed number of slots),
which must be 2n-1, e.g. 31 slots for SYNC and 63 for DATA. Actually, S-MAC can have a
third contention window in the sleep period, if adaptive listening is applied (We will discuss
adaptive listening in section 2.7). Here we consider only S-MAC without adaptive listening.
Now we take a simple example and see how carrier sense is performed before sending a SYNC
If nothing is heard throughout the whole carrier sense period, the node will assume that
the medium is free and start transmitting the packet right away.
Once the medium is sensed busy during the carrier sense period, the node will stop
sensing and defer sending the packet. When the same period in the next frame arrives, the
node will retry sending. While retrying, it will follow the same procedures described
above.
In the above discussion, S-MAC always chooses the number of slots for carrier sense within the
fixed contention window size, uniformly and independently. This is not like the binary
exponential back off introduced in 802.11.
Now we see what will happen when collision occurs. If B also sent out the RTS at almost the
same time when A sent the RTS out, two RTS packets collide. Neither of them will receive the
RTS packet from the other and send a CTS packet back. After a short wile, A’s CTS timer
expires. If now A’s primary schedule is in the sleep period, A will go to sleep by turning its radio
off. When the next DATA period on the same schedule comes, A will resend the RTS. The
maximum number of RTS retries for sending a DATA packet is user-adjustable in S-MAC.
When the retry times reach the maximum number, A will give up sending the DATA packet and
signal its upper layer about the failure of sending. The same procedures will be followed when
B’s CTS timeout timer expires.
The above figure shows a five-hop linear network. Each node can only hear its immediate
neighbors. We assume that all the nodes share the same schedule. Suppose A is communicating
with B in the same way that we discussed in Fig. 2.7. We first see which nodes should go to
sleep during this transmission.
We know that collision only happens on the receiver side. Obviously, D is supposed to go to
sleep, because its transmission interferes with B’s reception of the DATA packet. C is two hops
away from B, so C’s transmission will not interfere with B’s reception. But if C talks to E while
A is sending data to B, C will not receive any packet from E because collision happens on C. C’s
transmission is a waste of energy and it also needs to go to sleep. Both E and F are at least two
hops away from the nodes that are transmitting, and they will never produce interference.
Therefore, E and F have no need to sleep. In a word, those nodes that are the immediate
neighbors of the sender or the receiver should go to sleep.
Now we discuss when C and D should go to sleep. We first see C, who is A’s immediate
neighbor. C can hear the RTS packet that A sends to B. However, C does not go to sleep after
receiving the RTS, because at this moment the communication has not been really set up and
may be cancelled for some reason, e.g. collision on RTS packets. Therefore, it has to keep
listening for a CTS timeout period to see if it can hear the following CTS packet. If C receives
From the above discussion, we find that the message passing mechanism in S-MAC can
effectively reduce the control overhead in transmitting a long message, by sending all fragments
in a burst and using only a pair of RTS/CTS for it. When the sender fails to get an ACK for any
fragment, message passing will provide some opportunities for re-transmitting the lost or
corrupted fragment, instead of giving up the transmission immediately and re-contending for the
medium. This approach seems to be unfair to those nodes that have a short message to send.
However, in this way, application-level performance for each node can be improved greatly, if
compared to the traditional MAC protocols. That is what sensor networks desire.
Fig. 2.10. A three-hop network with one source and one sink
We first see the case shown in Fig. 2.11. When adaptive listening is not employed, each node has
at most one chance to send the data packet out in each frame, because checking to send the data
packet only takes place at the beginning of each DATA period. If the arrival of the data packet at
a certain node misses this checkpoint of this frame, it has to wait until the next DATA period
comes. That is to say, when each node strictly follows its sleep schedule, there is a potential
delay on each hop. We can see from the Fig. 2.11 that we need three frames to transmit the data
packet from the source A to the sink D. The data packet travels only one hop in each frame.
We start with the transmission between A and B. As shown in Fig. 2.12, transmitting the data
packet from A to B (including carrier sense and exchange of RTS/CTS) starts from the scheduled
DATA period and extends to the following sleep period. We have known from the previous sub-
section 2.4.1 that both the sender A and the receiver B will use their Neighbor NAVs to perform
virtual carrier sense, while C is overhearing this transmission and will use its NAV for virtual
carrier sense. Both NAV and Neighbor NAV contain the same value, which is the time reserved
for the current transmission. The expiration event of those NAVs (count down to zero, indicating
the end of the current transmission) will trigger the execution of adaptive listening on their
owner nodes. The following universal steps will be executed when adaptive listening is triggered
at a certain node.
3). If I am still sleeping, I have to wake up now. This may happen to the node, such as C in Fig.
2.12, which is sleeping because it overheard its neighbor’s previous transmission.
4). Check if the flag txData on my primary schedule is set (that is to see if I have a buffered
unicast data packet to send on my primary schedule). If yes, I will try to send out the data packet
by following the same way I do in the schedule DATA period (described in section 2.4).
Otherwise I have to keep awake, because my neighbors, who are also adaptive listening now,
may want to talk to me. Please notice that broadcast data packet will never be sent when I am
adaptive listening, because I am not sure whether all my neighbors are also adaptive listening
now. Some of my neighbors may not be aware of the previous transmission that I was involved
in, so they do not execute adaptive listening as me and may be asleep now. For the example
shown in Fig. 2.12, only B satisfies the sending condition, because it just got the data packet
from A that needs to be retransmitted to C. After exchanging RTS/CTS with C successfully, B
transmits the data packet to C, as shown in the red box in Fig. 2.12. For A, it has nothing to send
and has to keep awake. But it will hear the RTS that B sends to C. So A has to go to sleep to
avoid overhearing the transmission between B and C.
5). The adaptive listening timer set in step 2 expires. If I am still awake only because I have
nothing to send and hear nothing during this adaptive listening period, now I will go back to
sleep again. For the example in Fig. 2.12, no node goes to sleep for such reasons stated above.
Both B and C are impossible, because B is transmitting to C. And A overheard this transmission
From the above discussion, we see that adaptive listening can greatly reduce the latency caused
by the periodic sleep of each node in multi-hop networks. Consider one data packet, which is to
be transmitted through a multi-hop network. When adaptive listening is not applied, the data
packet can jump only one hop in a frame time. When adaptive listening is applied, the data
packet can be retransmitted to the next-hop node immediately after its last transmission is over.
It should be mentioned that theoretically the range of adaptive listening is limited to one hop,
because we assume that neighbors that are two hops away cannot hear each other. Under this
Chapter 3
cd Desktop
cd ns-allinone-2.30
./install
• Now, the installation will start and after some time the message named “IMPORTANT
NOTICES” will appear on screen which gives the information about the paths to be
added.
gedit ~/.bashrc
• Then the “gedit” text editor will open which is the official text editor for the GNOME
(GNU Object Model Environment) desktop.
• Keep the written things in that editor as it is and add the following lines at the end of it.
Replace /usr/local/” by installation path like “/root/Desktop”. Also, make changes
according to the version of the ns. For example, for ns-2.30, otcl’s version is 1.12, tcl’s
version is 8.4.13, etc.
#LD_LIBRARY_PATH
OTCL_LIB=/usr/local/ns-allinone-2.30/otcl-1.12
NS2_LIB=/usr/local/ns-allinone-2.30/lib
X11_LIB=/usr/X11R6/lib
USR_LOCAL_LIB=/usr/local/lib
export LD_LIBRARY_PATH = $LD_LIBRARY_PATH: $OTCL_LIB: $NS2_LIB: $X11_LIB:
$USR_LOCAL_LIB
# TCL_LIBRARY
TCL_LIB=/usr/local/ns-allinone-2.30/tcl8.4.13/library
USR_LIB=/usr/lib
export TCL_LIBRARY=$TCL_LIB:$USR_LIB
# PATH
XGRAPH=/usr/local/ns-allinone-2.30/bin:/usr/local/ns-allinone-2.30/tcl8.4.13/unix:/usr/local/ns-
source ~/.bashrc
• Write ns and press enter. If there is no error, a “%” will appear on screen.
3.4 Simulation
Following are the steps to write tcl file to in NS which is necessary to run the simulation.
• Define goal and expected result
Nodes
General tag
-t 0.267662078 - Time
cd Desktop
cd gnuplot-4.2.4
./configure
make
make install
To plot the results using Gnuplot, we must have two files on hand. Text file and log file. Text file
contains the executable statements to provide various parameters needed and log file contains the
data to be plotted. Following is the example of text file, named compare.txt.
Create a title: > set title "Comparing the performance of ADTCP and
Improved- ADTCP”
For color or mono: > set term post enh color/mono
Create file: > set out ‘comparision.eps’
Put a label on the y-axis: > set xlabel "Total Transmitted Packets (pkts)"
Put a label on the x-axis: > set ylabel "Number of Concurrent TCP Flows"
Change the x-axis range: > set xrange [0:30]
Change the y-axis range: > set yrange [210000:310000]
Change the tic-marks: > set xtics 5
Change the tic-marks: > set ytics 10000
Following is the example of the simple log file, named compare.log, related to the text file
described above.
5 263213 239261
10 285249 262446
15 285341 262813
20 299212 278267
25 302492 284342
To plot the results from the log file, following commands have to be executed in console.
gnuplot
load “compare.txt”
The Gnuplot of the above example will look like as shown below.
Name Comment
syncFlag_ If it is set to 1, S-MAC runs with periodic sleep. If it is set to 0, S-
MAC runs without periodic sleep.
dutyCycle_ The value of duty cycle in percent. It controls the length of sleep.
If not set, ns-2 uses the default value 10%. This parameter is
active only when syncFlag_ is set to 1.
selfConfigFlag_ If it is set to 1, all S-MAC nodes follow the schedule initialization
algorithm described in section 2.3.
If it is set to 0, the schedule start time (first listen period start
time) for each node is user-configurable.
Topology: we have create 2 node topology in which one node is sender and other is receiver. For
our 3 graphs we take this part. For routing node graph we have created a 3 node topology in
which one node is simply routing node and get our last graph at a routing node.
Traffic pattern: Packets are sent from the source node to the sink node. Here we used CBR
traffic and change the “packet inter arrival time” from 0.1s to the 10s. Here 0.1s means high
traffic and 10s means low traffic and see how s-mac can behave on 2 different conditions.
We can know from this trace record that at 274.090231333 s, node 3 sends an RTS packet to
node 4. Now we explain what these field mean in the usual case.
1. The first field represents the event type, whose value can be s (send), r (receive), d (drop)
or f (forward).
2. The second field the time this event happens.
3. The third field records the id of the node, on which this event takes place.
4. The fourth field shows the layer where this event happens. Its possible value may be one
of the following four: AGT (agent), RTR (router), IFQ (interface queue), and MAC
(mac).
5. The fifth field is normally a short broken line, which is reserved for special events. For
example, when collision occurs, the broken line is replaced with COL.
6. The sixth field is the global sequence number for this packet, which is the integer number
used to identify this packet in the whole network and distinguish it from others. Sequence
number is only available for data packets and not allocated for control packets, like
RTS/CTS/ACK and SYNC packets in S-MAC (using a zero instead).
7. The seventh field is the packet type. The actual value is determined by the application or
MAC layer, which creates this packet. For example, cbr represents that it is a data packet
generated by a CBR traffic source.
8. The eighth field is the packet size in bytes.
9. The ninth field including three numbers in brackets concerns MAC layer information.
Originally, there will be four numbers in the brackets. But S-MAC revises this format.
The first number is the duration field of this packet. In Fig. 4.3, the duration field of this
Appendix A
TCL File :
set opt(chan) Channel/WirelessChannel
set opt(prop) Propagation/TwoRayGround
set opt(netif) Phy/WirelessPhy
set opt(mac) Mac/802_11 ;# MAC type
set opt(mac) Mac/SMAC ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue
set opt(ll) LL
set opt(ant) Antenna/OmniAntenna
# Create god
create-god $opt(nn)
[1] W. Ye, J. Heidemann, and D. Estrin. An energy-efficient MAC protocol for wireless sensor
networks.
[2] W. Ye, J. Heidemann and D.Estrin. Medium access control with coordinated, adpative
sleeping for wireless sensor networks. ACM/IEEE Transactions Networking, 12(3):493-506,
June 2004.
[3] Y. Li, W. Ye and J. Heidemann. Energy and latency control in low duty cycle MAC
protocols. USC/ISI Technical Report ISI-TR-595, August 2004.
[4] A. M. Law, W. D. Kelton. Simulation, Modeling and Analysis, 3rd Edition.
McGraw-Hill, [2000].
[5] L. Bao and J. J. Garcia-Luna-Aceves. A New Approach to Channel Access Scheduling for
Ad Hoc Networks. In Proc. 7th Ann. Intl. Conf. on Mobile Computing and Networking, pages
210–220,Rome, Italy, July 2001. ACM.
[6] V. Kanodia, C. Li, A Sabharwal, B. Sadeghi, and E. W. Knightly. Distributed Multi-Hop
Scheduling and Medium Access with Delay and Throughput Constraints. In Proc. 7th Ann. Intl.
Conf. on Mobile Computing and Networking, pages 200–209, Rome, Italy, July 2001. ACM.
[7] C. Schurgers, V. Tsiatsis, S. Ganeriwal, and M. B. Srivastava. Optimizing Sensor Networks
in the Energy-Latency-Density Design Space. IEEE Transactions on Mobile Computing, 1(1),
2002.
[8] A. Woo and D. Culler. A Transmission Control Scheme for Media Access in Sensor
Networks. In Proc. 7th Ann. Intl. Conf. on Mobile Computing and Networking, pages 221–235,
Rome, Italy, July 2001. ACM.
[9] L. Charlie Zhong, R. C. Shah, C. Guo, and J. M. Rabaey. An Ultra-Low Power and 83
Distributed Access Protocol for Broadband Wireless Sensor Networks. In IEEE Broadband
Wireless Summit, Las Vegas, NV, May 2001.