1

I have a C code in UNIX where I need to keep my processor doing nothing for 2 seconds. In order to do that, I found the assembly instruction asm volatile("nop"::). I've searched a lot but I couldn't find anywhere explaining how can I calculate the necessary number of NOPs to execute my function for exactly 2 seconds. Can anyone help me?

for(i = 0; i < COUNTER; i++){
    asm volatile ("nop"::);
}
clasimoes
  • 37
  • 1
  • 4
  • 3
    GCC doesn't execute the instructions it compiles. The CPU does. ;) – Devolus May 19 '14 at 17:04
  • Busy-waiting for more than a few nanoseconds is an extremely bad idea; why do you want to do this? Are you trying to cook an egg on the processor or something? – Eric Lippert May 19 '14 at 17:31
  • Note that in addition to executing the `nop`, the processor is also executing `i++` and `i < COUNTER` (and a conditional jump, and a goto, etc.). An individual iteration of this loop is doing a lot more than executing a `nop`. Of course, the compiler (and the CPU) can do all sorts of things to reduce these costs. – Brian May 19 '14 at 18:11

2 Answers2

5

The NOP instruction takes one cycle in most microprocessors, so do the math:

e.g., on a 8 MHz processor, one cycle takes 1 / 8 MHz = 125 ns. You then have to add few cycles more for the management of the loop.

If you are in an environment with an OS you should not rely on NOP instructions to wait for seconds and should not use an active wait. On POSIX systems (like UNIX), use POSIX nanosleep functions.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 3
    Modern CPUs are often superscalar, and NOP has no dependencies, so it could effectively take a fraction of a cycle (in terms of throughput) – Leeor May 19 '14 at 17:02
  • @Leeor: What about `LOCK nop`? – Kerrek SB May 19 '14 at 17:14
  • @KerrekSB, never seen it done, are you sure it's a valid combination? Even if it is, I wouldn't trust it to be serializing – Leeor May 19 '14 at 17:21
  • I am testing an EDF scheduling policy for real time processes where preemption is active. That's why I need to make my processes always execute for a fixed period of time. With your answer I got that a loop **asm volatile ("nop"::)** for (processor speed) times will return always something around 1 second, but this number is never exact. Do you know any other way of making a execution time of a process fixed? – clasimoes May 19 '14 at 19:42
  • 1
    use a timer. accurate timers in a PC are hard to come by though (in a generic sense across machines/versions of OS software). The right answer is to do something operating system specific if possible, find a real time operating system. – old_timer May 19 '14 at 20:42
1

just include unistd.h and use:

unsigned int sleep(unsigned int seconds);
Gadol21
  • 106
  • 8
  • Or if you really want to burn cpu time, keep calling `clock_gettime` until it returns the time you want (2 seconds past the original time) or later. – R.. GitHub STOP HELPING ICE May 19 '14 at 17:10
  • I am testing an EDF scheduling policy for real time processes. That's why I need to make my processes always execute for a fixed period of time. But since preemption will happen, I think a time function won't work for me. – clasimoes May 19 '14 at 19:15