r1ch.net forums
* Home Help Search Login Register
r1ch.net  |  General  |  General Discussion  |  Topic: C Programming Question
Pages: [1]
Print
Author Topic: C Programming Question  (Read 7025 times)
GHz
Member

Posts: 28


« on: June 06, 2005, 11:09:18 pm »

Quick question for anyone familiar with C: How can I open a file in a separate process? I'm familiar with the basic file IO functions of ANSI C, but have no idea how to do this in a Win32 environment. I need to be able to open files in a separate process without lagging out my Quake2 server (it will be opening files over a high-latency networked drive). Any help would be appreciated.

Thanks,

-G
Logged
QwazyWabbit
Member

Posts: 402


« Reply #1 on: June 07, 2005, 12:06:37 am »

The Win32 functions are _beginthread, _endthread when using the STD C library. This has its own baggage when calling the library functions, see the help in the MSDN documentation on these functions.
_beginthread is comparable to the *nix fork() function.

The good news is you may not even need to spawn threads. Try opening your file with fopen() as you would a local file, let Windows buffer the data as it would normally and see how it affects your lag. Once the file is open, don't close it for the duration of the process, call _fcloseall when exiting your application or let Windows clean it all up when the process terminates abnormally. I'm not sure what would happen if your network resource were to disappear while your process was running.

Another alternative would be to call CreateProcess and use it to run a separate helper application. Pipe your stream to it and let the helper deal with latency. The interaction on the pipe if the helper can't output the file would be up to you. I suppose you could discard the data in that case and let the application think everything was OK. (Assuming the data you are saving was a non-critical log or something.) This has the advantage from a development standpoint since your helper would be a separate .exe and you can debug them in parallel in two VS instances and test them by simulating different latencies.
« Last Edit: June 07, 2005, 12:10:03 am by QwazyWabbit » Logged
GHz
Member

Posts: 28


« Reply #2 on: June 07, 2005, 02:15:43 pm »

I'm not quite sure how to pipe streams between applications; is there a way I can just stick all my load/save code in a separate thread, and then somehow inform the main thread that the file is successfully opened? I've tried the helper method, with limited success. Again, I'm not sure how 2 programs can communicate... I'd appreciate if you can give me any suggestions, or point me to somewhere that explains it. I apologize for my limited programming knowledge, I learned C from a book with no formal instruction.

Thanks for your help,

-G
Logged
QwazyWabbit
Member

Posts: 402


« Reply #3 on: June 07, 2005, 06:51:43 pm »

I'm not sure if you are using VC++ 6.0 or Visual Studio .NET. Help on the _pipe function is available there.

If not, here's a link to MSDN on the _pipe function:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt__pipe.asp

To use _pipe you would be going outside of ANSI C. (Note: Microsoft uses the leading underscore in their functions that are extensions outside the ANSI C standard library functions.)

You would also be going outside standard C to use CreateThread, but at least the Win32 API would be supported whether you are trying to code a Windows app or a console app.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp

The _beginthread documentation also comes with this warning:

"Warning   The multithread library LIBCMT.LIB includes the _beginthread and _endthread functions. The _beginthread function performs initialization without which many C run-time functions will fail. You must use _beginthread instead of CreateThread in C programs built with LIBCMT.LIB if you intend to call C run-time functions."

The return value is a handle to the thread and you communicate with that thread through the handle and through a pointer to the data you want to send to the thread. The data are allocated from the process heap that spawns the thread. Here is a simple example:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/thread_handles_and_identifiers.asp

I still think you should test your application using fopen() directly in the main thread and time the impact of latency and actually determine how long it takes Windows to open and write to a local file and a remote file before you go to the trouble of writing a thread for this. Windows does a lot of file buffering for you and I would say that you could probably write it in a single thread with little or no impact to your server. I'd like to hear what kind of delays you get when opening a network file.


Look for a file named nmpipe.c in the MSDN documentation for a sample pipe program.
Look for BOUNCE.C for a sample multithreaded application that uses _beginthread.
Logged
GHz
Member

Posts: 28


« Reply #4 on: June 07, 2005, 09:36:06 pm »

Thanks for the information. The network drive is making a connection via FTP over the internet (the application that allows this is Novell's Netdrive). It typically takes about 3 seconds for a single file to load, and it causes the server to phonejack. You can imagine the kind of lag would be caused by 10-20 players all connecting or disconnecting at once.

-G
Logged
Pages: [1]
Print
r1ch.net  |  General  |  General Discussion  |  Topic: C Programming Question
Jump to:  

Powered by SMF 1.1.19 | SMF © 2013, Simple Machines