Memory mapping files use virtual memory to map files to the address space of the process, after which the process manipulates the file just like the address in the process space, such as using memcpy and other memory operation functions in the C language. This method can be used well in situations where a file or a large file needs to be processed frequently, and the IO processing efficiency is higher than that of ordinary IO Shared memory is a special case of memory-mapped files, which map a piece of memory instead of a file on disk. The subject of shared memory is Process, and the operating system allocates a memory space to each process by default, and each process is only allowed to access a piece of memory allocated to it by the operating system, but not to other processes. And sometimes you need to access the same piece of memory between different processes, what should you do? The operating system provides an API to create access to shared memory, and processes that need to share memory can access the memory shared by multiple processes through this set of defined APIs, and each process accesses this memory as if it were accessing a file on a hard disk. The .Net 4.0 introduces the System.IO. MemoryMappedFiles namespace, a class that encapsulates the Windows Shared Memory-related APIs, making it easier for .Net programmers to use memory mapping files. Use shared memory in C#. The following code for App1 lets the user enter a line of text into shared memory; App2 constantly refreshes the console and outputs the latest shared memory content; App3 implements the same functions as App2, but the reading method is different.
Two methods were used to read the data. Since communication between processes was rarely used before, this method is only a preliminary understanding. The program is too rudimentary, and there are many things that are not judged. For example, how to create a shared memory, how to retrieve and delete it, etc.
|