Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to limit how fast a program can read from files without slowing the program itself down?

share|improve this question

migrated from Oct 31 '12 at 0:26

This question came from our site for professional and enthusiast programmers.

From inside or outside the program? Can you tell it to read from a new path, possibly mounted using a loopback or FUSE filesystem? – Ben Voigt Oct 30 '12 at 23:28
Is this specific to compiled C++ programs, or is the system you're writing this in implemented in C++? The tag is unclear (and possibly inapplicable). – WhozCraig Oct 30 '12 at 23:30
Why? What are you trying to accomplish? – Adrian Cornish Oct 30 '12 at 23:32
From outside I do not want to edit the program in anyway. The program would be using the standard Windows file loading functions not interfacing to the program it's self. – blood Oct 30 '12 at 23:32
@Adrian Cornish I am trying to keep select programs from hogging all the hard drive's read speed and slowing down everything else that needs files. – blood Oct 30 '12 at 23:34
up vote 2 down vote accepted

On Windows Vista and above, you can set the I/O priority of a process. This won't limit the rate, but will give other processes priority over your process and allow them to take precedence. There are hidden APIs to do so, but it seems what you're looking for is a tool. When you set the process priority, using task manager, to Below Normal or Idle, the I/O priority drops too.

This question on suepruser has more information:

It also links to this project that allows you to set I/O priority.

share|improve this answer
Thank you for the answer. – blood Oct 30 '12 at 23:59

Not sure about hardware or OS-specific solutions... But the obvious leaps out (forgive the crude implementation):

const long long maxBytesPerSec = 1048576;
static long long bytesRead = 0;
static long long secondsElapsed = 1;

istream & ReadBytesThrottled( istream & s, char * buffer, long long bytesToRead )
    while( (bytesRead+bytesToRead) / secondsElapsed > maxBytesPerSec ) {
        // Wait and update secondsElapsed
    }, bytesToRead);
    if( s.good() ) bytesRead += bytesToRead;
    return s;

void ResetThrottle()
    bytesRead = 0;
    secondsElapsed = 1;

This is only relevant if you are reading chunks. I suppose you could derive from the stream and overload the relevant methods. Starts getting a bit yuck. But it's a simple and quick workaround if you can't find a lower-level solution.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .