To add on to the proposed solution, let me try to offer an explanation
of the difference between Somera's original code and Fillippos'
suggestion. In Somera's code, the line

$ns at $time "loop"

does not call the loop function. Instead, it adds the call to the
scheduling queue (or similar data structure). So there is no break
condition for the while loop, causing the script to hang.

In Fillippos' code, the "loop" call is scheduled initially with

$ns at $time "loop"

and then we call $ns run to start the simulation. At time $time, the
scheduler checks the front of the queue and sees the "loop" call. It
then evaluates the impact of the "loop" event on the simulation --
namely, that another "loop" event be called $inc ticks later. The
scheduler then checks the front of the queue again and repeats the
process.

Hopefully my explanation is correct. If someone can add to this or
correct it, it might be a good lesson describing the difference
between events scheduled by the script at configuration time and
events scheduled during the runtime of the simulation.

Thanks,
Scott

On 2/8/07, Filippos Kolovos <[EMAIL PROTECTED]> wrote:
>
> -
> At first you need to schedule the "loop" proc to execute at the
> start (or whenever you like) of the simulation.
>
> Then, you need to put the "re-scheduling" code inside the procedure itself
> in order to make it recursive. In this way the loop procedure will execute
> once
> at the start of the simulation and then every time it is executed it will
> re-schedule itself.
> You can also use the "now" method of the Simulator object, in order to get
> the exact current time
> of execution of the procedure as below.
>
> For example you need to change the code to something like this:
>
> proc loop {} {
>         global abc value inc ns
>
>         set value [$abc get_value]
>         set current_time [$ns now]
>         set time  [expr {$current_time + $inc}]
>         $ns at $time "loop"
>         exit 0
> }
>
> set time 0.2
> set inc 0.01
> $ns at $time "loop"
> }
>
>
>
> On 2/9/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> >
> > ---------- Forwarded message ----------
> > From: "Somera Javed" <[EMAIL PROTECTED]>
> > To: ns-users@ISI.EDU
> > Date: Fri, 9 Feb 2007 03:41:46 +0500
> > Subject: [ns] How to call a procedure after a constant delay
> > Hi,
> > i  am trying to call a procedure after a delay of every 0.01 sec
> >
> > proc loop {} {
> >         global abc value
> >         set value [$abc get_value]
> >         exit 0
> > }
> > set time 0.2
> > set inc 0.01
> > while 1 {
> >     $ns at $time "loop"
> >     set time [expr $time + $inc]
> >     if {$value == 1} break;
> > }
> >
> > but the program stuck while execution
> > what is the right way to do this???
> >
> >
> >
> > ---------- Forwarded message ----------
> > From: "Scott Ricketts" <[EMAIL PROTECTED]>
> > To: ns-users@ISI.EDU
> > Date: Thu, 8 Feb 2007 15:29:47 -0800
> > Subject: Re: [ns] How to call a procedure after a constant delay
> > It looks to me like you are putting the Otcl interpreter in an
> > infinite loop before the simulation starts. That is, "loop" is getting
> > scheduled every time the while loop iterates, but it does not actually
> > get called until you do $ns run, so $value never gets set.
> >
> > -Scott
> >
> > _______________________________________________
> > Ns-users mailing list
> > Ns-users@isi.edu
> > http://mailman.isi.edu/mailman/listinfo/ns-users
> >
> >
>
>
> --
> Filippos N Kolovos
>
> Software Systems Analyst & Engineer
> M.Sc. (Eng.) in Data Communications
>
> Automation & Networking Department
> University of Macedonia Library
> Egnatia 156, P.O.Box 1591
> 540 06 Thessaloniki, Greece
>
> E-Mail: [EMAIL PROTECTED],
>            [EMAIL PROTECTED]
> ----------------------------------------------
>

Reply via email to