TCL脚本中的浮点异常 [英] Floating point exception in TCL script
问题描述
我正在使用 ns2 模拟器和 tcl 脚本.在执行以下代码时,它引发了浮点异常错误.在我更改以下代码部分中的速率值之前
i am using ns2 simulator and tcl script.on executing the following code it has raised floating point exception error. before i have changed the rate values in the following section of the code
$ns duplex-link $n0 $n2 2.0Mb 10ms RED
$ns duplex-link $n1 $n2 2.0Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.1Mb 20ms DropTail
$ns duplex-link $n3 $n2 1.2Mb 10ms DropTail
它工作正常,但在更改为以下内容后,即在我使用变量 rate02,rate12,rate23,rate32
而不是值之后发生了浮点异常.有人可以帮忙吗?
it was working correctly but after it has been changed to the following i.e after i have used variables rate02,rate12,rate23,rate32
instead of the values floating point exception occurred. can any one help?
整个tcl代码如下:
#Create a simulator object
set ns [new Simulator]
global set rate02 2.0Mb
global set rate12 2.0Mb
global set rate23 1.1Mb
global set rate32 1.2Mb
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
$ns color 3 Green
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n2 rate02 10ms RED
$ns duplex-link $n1 $n2 rate12 10ms DropTail
$ns duplex-link $n2 $n3 rate23 20ms DropTail
$ns duplex-link $n3 $n2 rate32 10ms DropTail
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 20
$ns queue-limit $n3 $n2 22
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
$ns attach-agent $n0 $tcp1
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp1 $sink
$tcp1 set fid_ 1
#########################################
#Setup a TCP connection
set tcp3 [new Agent/TCP]
$tcp3 set class_ 2
$ns attach-agent $n3 $tcp3
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp3 $sink2
$tcp3 set fid_ 3
#Setup a FTP over TCP connection
set ftp3 [new Application/FTP]
$ftp3 attach-agent $tcp3
$ftp3 set type_ FTP
############################################
#Setup a FTP over TCP connection
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP
#Setup a TCP connection
set tcp2 [new Agent/TCP]
$tcp2 set class_ 2
$ns attach-agent $n1 $tcp2
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp2 $sink
$tcp2 set fid_ 2
#Setup a FTP over TCP connection
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set type_ FTP
proc openTrace { stopTime testName } {
$self instvar ns_
exec rm -f out.tr temp.rands
set traceFile [open out.tr w]
puts $traceFile "v testName $testName"
$ns_ at $stopTime \
"close $traceFile ; $self finish $testName"
return $traceFile
}
proc traceQueues { node traceFile } {
$self instvar ns_
foreach nbr [$node neighbors] {
$ns_ trace-queue $node $nbr $traceFile
[$ns_ link $node $nbr] trace-dynamics $ns_ $traceFile
}
}
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$ftp2 start"
$ns at 1.0 "$ftp1 start"
$ns at 1.0 "$ftp3 start"
$ns at 4.0 "$ftp2 stop"
$ns at 4.5 "$ftp1 stop"
$ns at 4.5 "$ftp3 stop"
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp1 ; $ns detach-agent $n3 $sink"
$ns at 4.5 "$ns detach-agent $n1 $tcp2 ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
$ns run
推荐答案
除非NS2"这个东西重新定义了 Tcl 的 global
命令,脚本应该是这样的:
Unless that "NS2" thing redefines the Tcl's global
command, the script should supposedly read something like this:
#Create a simulator object
set ns [new Simulator]
set rate02 2.0Mb
set rate12 2.0Mb
set rate23 1.1Mb
set rate32 1.2Mb
# ...
#Create links between the nodes
$ns duplex-link $n0 $n2 $rate02 10ms RED
$ns duplex-link $n1 $n2 $rate12 10ms DropTail
$ns duplex-link $n2 $n3 $rate23 20ms DropTail
$ns duplex-link $n3 $n2 $rate32 10ms DropTail
# ...
换句话说,你的代码有两个问题:
In other words, there are two problems with your code:
- Tcl 的
global
命令的语义定义为global varname ?varname ...?
,也就是说,它只是声明names 传递给它作为引用全局变量.因此调用global set rate02 2.0Mb
只声明名称set"、rate02"和2.0Mb"来表示全局变量.很可能不是您想要的. duplex-link
子命令可能需要一个值作为它的rate"参数,而你要传递给它一个变量的名称.em> 看起来,一些 NS2 代码然后试图将这样的字符串解释为浮点值,但由于明显的原因而失败.因此,当您想将该变量的值传递给命令时,您必须取消引用该变量以获取其值.这是使用set varname
命令或使用$
语法的速记符号完成的:$varname
(更多信息.
- The Tcl's
global
command has the semantics defined asglobal varname ?varname ...?
, that is, it just declares the names which are passed to it as referring to global variables. Hence a callglobal set rate02 2.0Mb
just declares the names "set", "rate02" and "2.0Mb" to denote global variables. Most probably not what you wanted. - the
duplex-link
subcommand probably expects a value as its "rate" argument, and you're passing it the name of a variable. Seemingly, some NS2 code then attempts to interpret such a string as a floating-point value, which fails for obvious reasons. Hence you have to dereference a variable to get its value when you want to pass that variable's value to a command. This is done using theset varname
command or the shorthand notation using the$
syntax:$varname
(more info about this).
看起来您应该从Tcl 教程开始处理这些基础知识.
Look like you should start with the Tcl tutorial which deals with these basics.
这篇关于TCL脚本中的浮点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!