Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

c++ - Transferring data through a memory-mapped file using Win32/WINAPI

I need to transfer some data - char buffer[100000]; - to a child process which is started by me.

Right now I'm using an ordinary file to do so, the parent process writes the data to a file on disk, and the child process reads it from disk and deletes the file. However, that causes unnecessary writes to the disk, so I want to use memory-mapped files instead.

How do I create, write to, and then read from a memory-mapped file in such a way that no data is written to disk, except when the pagefile or swap file is used?

Edit: I forgot to specify that I want to use raw WINAPI functions, so the code doesn't have dependencies. I am researching on how to do it and will post an answer myself when ready, but if someone has ready-made code, they're welcome to save me some effort :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pass INVALID_HANDLE_VALUE as the file handle when calling CreateFileMapping:

If hFile is INVALID_HANDLE_VALUE, the calling process must also specify a size for the file mapping object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. In this scenario, CreateFileMapping creates a file mapping object of a specified size that is backed by the system paging file instead of by a file in the file system.

You can use either an anonymous file mapping object (pass inheritable handle to child process), or use a named file mapping.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...