Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com 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
2  
If this is not about programming, then the question belongs on superuser. –  paddy Oct 30 '12 at 23:52
show 3 more comments

2 Answers

up vote 1 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:

http://superuser.com/questions/136021/how-to-change-i-o-priority-of-a-process-or-thread-in-win7

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

http://sourceforge.net/projects/iopriority/

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

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
    }
    s.read(buffer, 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.