tags:

views:

183

answers:

4

Suppose I have a program that runs in a given amount of time (say, three seconds). I want to run this program so that it runs n-times slower (specified on command line). How would you achieve it with (or better, without) changes to the program ?

please note that adding a sleep at the end is not a solution. The program has to run slower, not to run at full speed for the first three seconds and then do nothing for the remaining time. Also, using "nice" under unix is not a good solution either. it will run slower if other processes demand the processor, but at full speed if nothing is processor-demanding at the same time.

This is a curiosity question. Nothing serious to do related to it. The fact is that I remember 15-20 years ago games that were simply too fast to play on new processors, because they were timed with the processor clock. You had to turn off the turbo.

Let's assume the program is a C compiled program.

A: 

I ran some application in a virtual machine under ubuntu. It was really slow. You could configure the virtual machine usage of the system.

You might obfuscate the situation a little further by running a virtual machine under a virtual machine under a virtual machine, ...

Blessed Geek
... which is not possible btw.
Johannes Rudolph
+4  A: 

One idea is to write a 'ptrace runner.' ptrace is the call that allows you to implement a debugger on platforms such as Linux and Mac.

The idea is to attach to the program and then just repeatedly tell the application to run one instruction with ptrace(PTACE_SINGLESTEP). If that's not slow enough, you could add a sleep between each call to ptrace in the runner program.

R Samuel Klatchko
+4  A: 
sambowry
it's nice, but it will run in "hiccups" :)
Stefano Borini
+1  A: 

cpulimit is a tool that does something like this. It works by periodically kill -STOP and kill -CONT the process, which has the effect of it running slower (when averaged over time).

Wim