bad to have many files open at once?

Started by
13 comments, last by synth_cat 15 years, 10 months ago
My game currently uses streaming music. Whenever I switch from one song to another (which involves closing the first file and opening/initializing the second one) it causes a nasty lurch in the game. Is it OK for me to just open all the files at the start of the game and keep them open continuously, just streaming from one at a time? I would be having around 10-20 files open. I am using Windows, dealing with files opened via fopen() style routines. Thanks! -Greg
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith
Advertisement
Each process has a limit on the number of files it may have open at once. That limit is much much higher than 10-20, so you should be fine.

The best performance can be gained by loading all sound files into memory at once and closing all your open file handles. You should do that if you can afford it (memory-wise).
What are the risks to keeping my files open the whole time? (I can't afford to load all the data into memory - it's far too much.)
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith
It would probably be better to close them as they could cause nasty problems on some rare chance.
------------George Gough
Quote:Original post by KodeNerd
It would probably be better to close them as they could cause nasty problems on some rare chance.


For example?
Some other thread can try to access the same file, for example, and I don't think it is possible to access a file from more than one application. The handle could actually become invalid during execution and may just be accessed without error checking. That is all I can think of right off the top of my head.
------------George Gough
But is it any riskier to have 10 files open continuously than it is to have one file open continuously? Because I'm going to have to have one file open continuously no matter what, seeing as I'm streaming music.
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith
I would stream until the end of the file and then close it.

I am not entirely sure if it is riskier (though my mind says it could be a very big risk) as I have not extensively tested it.
------------George Gough
I think the culprit here is blocking I/O, which is what fopen is. This means in most implementations that all threads (maybe just the calling thread?) in a process will be halted while the I/O is performed. This is causing the lurch in your game.

In order to get around this, you need to use platform-specific asynchronous file I/O methods. Basically, these methods will not guarantee that the data is read or written to disk when the function call returns. Usually, one will be notified of the completion of a disk read or write through a callback method or event.
Quote:Original post by KodeNerd
Some other thread can try to access the same file, for example, and I don't think it is possible to access a file from more than one application. The handle could actually become invalid during execution and may just be accessed without error checking. That is all I can think of right off the top of my head.
That's guessing and speculation.
If you open the file with the GENERIC_READ, FILE_SHARE_READ passed to CreateFile then other programs can also open the file. Otherwise the other programs will simply fail to be able to open the file.

Feel free to keep them all open at once, 20 files is just fine. However, I wouldn't be too surprised if it doesn't help much, unfortunately. I suspect that the 3rd party library you are using causes the lurch because it tries to preload a decent chunk of the start of the file when you tell it to start playing.

Also check that those files aren't simply hugely fragmented on your disk.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement