LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to apply 2 timers on a Node..??? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-apply-2-timers-on-a-node-4175615981/)

reha 10-19-2017 01:01 PM

How to apply 2 timers on a Node..???
 
Dear all,

I am interested in applying two timers(T1 & T2) on a node.

1. As per the functionality of my protocol, the timer 'T1' is only called once at the start of the simulation.
2. The timer 'T2' is called again and again during the simulation.

However, till now, I am unable to implement both timers T1 & T2 together. In this context, my questions are:

Kindly is there any possible way of implementing both timers T1 & T2 together...?

Kindly is there any way of deactivating the timer T1 when I realize that its task is over that shall help me to activate T2 later on...?

Also, I am using NS-2.31 with MACNG based implementation for my simulation. Thank you...

BW-userx 10-19-2017 01:36 PM

wouldn't you be better off starting your first timer then whatever time between its stopping and starting the other timer devise a method to all it as soon as your start timer has quit. something like this?
Code:

#include <iostream>
#include <ctime>
#include <cstdlib>

void start_one(int a);
void start_two(int a);
int main()
{
start_one(2);

// start it here or call in as soon as the first one stops
 //start_two(5);

return 0;
}

// seconds laps time in prams


void start_one(int a)
{
 clock_t startTime = clock(); //Start timer
 double secondsPassed;
 double secondsToDelay = a;
 bool flag = true;

//call here to fire imminently then stay until
// times up
 while(flag)
  {
  secondsPassed = (clock() - startTime) / CLOCKS_PER_SEC;
  if(secondsPassed >= secondsToDelay)
    {
                // put in here what you want ran to
        // to start after delay
        std::cout<< " timer one"<<std::endl;
        flag = false;
    secondsPassed  = 0;
    startTime = clock();
    }
  }
  start_two(5); // here as soon as this one is over with it will start the other one
}



void start_two(int a)
{
 clock_t startTime = clock(); //Start timer
 double secondsPassed;
 double secondsToDelay = a;
 bool flag = true;



 while(flag)
  {
  secondsPassed = (clock() - startTime) / CLOCKS_PER_SEC;
  if(secondsPassed >= secondsToDelay)
    {
        // put in here what you want ran
                std::cout<< " timer two"<<std::endl;
    // no resetting flag it will run forever
    // fires every x sec elapse time
    secondsPassed  = 0;
    startTime = clock();
    }
  }
}

or just call both to start at the same time, but I am guessing this is causing conflict if it is
controlling the same thing at the same time.


All times are GMT -5. The time now is 03:27 AM.