Timer Usage You will be required to use a timer to accuratel

Timer Usage
You will be required to use a timer to accurately timestamp all of your simulator\'s actions. This means
that a printer operation running for 6 cycles will have to physically run for 6 printer cycle time. The
simulator must output a timestamp at the beginning of the printer operation and another timestamp at the
end of the operation. You are required to use the system clock. The timer itself must be measurable to the
microsecond level and accurate to the millisecond level. For example, the output below should run for 7.387 seconds, outputting the correct timestamps for each operation.

   clock_t start=clock();

double testTime=static_cast(start)/CLOCKS_PER_SEC;
usleep(100000);
start=clock();
   double testTime=static_cast(start)/CLOCKS_PER_SEC;

In C++, when i used usleep(), it seems that it stops the system clock. If i used usleep(), I can make the program run for 7.387 seconds, but I cannot correctly display the timestamp. Is there any function in c++ library that I can use to replace usleep() so that I can run the program for 7.387 seconds and display the timestamp correctly?

0.000001 Simulator program starting 0.000051 OS: preparing process 1 0.000053 OS: starting process 1 0.000055 Process 1: start processing action 0.132061 Process 1 end processing action 0.132063 Process 1: allocating memory 0.182066 Process 1 memory allocated at 0x00000000 0.182069 Process 1: start monitor output 0.392073 Process 1: end monitor output 0.392074 Process 1 start hard drive input 10 0.536077 Process 1 end hard drive input 11 0.536079 Process 1 start printer output 12 5.536081 Process 1: end printer output 13 5.536085 Process 1: start processing action 14 5.608088 Process 1 end processing action 15 5.608089 Process 1: start printer output 16 6.608094 Process 1: end printer output 17 6.608097 Process 1: start memory blocking 18 6.758099 Process 1 end memory blocking 19 6.758101 Process 1: start keyboard input 20 21 6.843104 Process 1: end keyboard input 6.843106 Process 1: start memory blocking 22 6.943109 Process 1 end memory blocking 6.9431 10 Process 1 start processing action. 24 7.003114 Process 1 end processing action 25 7.003116 Process 1 start processing action. 26 7.063119 Process 1 end processing action 27 7.063127 Process 1 start hard drive output 28 7.171130 Process 1: end hard drive output 29 7.171134 Process 1 start processing action. 30 31 7.387137 Process 1: end processing action 7.387158 OS: removing process 1 32 ss 7.387433 Simulator program ending

Solution

You can use nanosleep() instead of usleep().

nanosleep() function is more accurate and precise when compared to usleep() as it stores nano values too.

Timer Usage You will be required to use a timer to accurately timestamp all of your simulator\'s actions. This means that a printer operation running for 6 cycl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site