c++进程间共享内存

    xiaoxiao2021-12-14  19

    进程间通信共七种方式: 第一类:传统的unix通信机制: ---------------- # 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用。进程的亲缘关系通常是指父子进程关系。 # 有名管道 (named pipe) : 有名管道也是半双工的通信方式,但是它允许无亲缘关系进程间的通信。 # 信号 ( sinal ) : 信号是一种比较复杂的通信方式,用于通知接收进程某个事件已经发生。 第二类:System V IPC ---------------- # 信号量( semophore ) : 信号量是一个计数器,可以用来控制多个进程对共享资源的访问。它常作为一种锁机制,防止某进程正在访问共享资源时,其他进程也访问该资源。因此,主要作为进程间以及同一进程内不同线程之间的同步手段。 # 消息队列( message queue ) : 消息队列是由消息的链表,存放在内核中并由消息队列标识符标识。消息队列克服了信号传递信息少、管道只能承载无格式字节流以及缓冲区大小受限等缺点。 # 共享内存( shared memory ) :共享内存就是映射一段能被其他进程所访问的内存,这段共享内存由一个进程创建,但多个进程都可以访问。共享内存是最快的 IPC 方式,它是针对其他进程间通信方式运行效率低而专门设计的。它往往与其他通信机制,如信号两,配合使用,来实现进程间的同步和通信。 第三类:BSD 套接字: ------------ # 套接字( socket ) : 套解字也是一种进程间通信机制,与其他通信机制不同的是,它可用于不同及其间的进程通信

    ========================================================================

    通过文件映射对象共享内存 1.1往共享内存里写数据 // ServerCom.cpp : Defines the entry point for the console application. #include <stdio.h> #include <windows.h> #pragma endregion #define MAP_PREFIX L"Local\\" #define MAP_NAME L"SampleMap" #define FULL_MAP_NAME MAP_PREFIX MAP_NAME // Max size of the file mapping object. #define MAP_SIZE 65536 // File offset where the view is to begin. #define VIEW_OFFSET 0 // The number of bytes of a file mapping to map to the view. All bytes of the // view must be within the maximum size of the file mapping object (MAP_SIZE). // If VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to // the end of the file mapping. #define VIEW_SIZE 1024 // Unicode string message to be written to the mapped view. Its size in byte // must be less than the view size (VIEW_SIZE). #define MESSAGE L"Message from the first process." // 实现不同的进程共享数据; //1、要先使用函数CreateFileMapping来创建一个想共享的文件数据句柄; //2、使用MapViewOfFile来获取共享的内存地址; //3、使用OpenFileMapping函数在另一个进程里打开共享文件的名称; int main(int argc, char* argv[]) { HANDLE hMapFile = NULL; PVOID pView = NULL; // Create the file mapping object. hMapFile = CreateFileMapping(//创建一个想共享的文件数据句柄; INVALID_HANDLE_VALUE, // 表示在页面文件中创建一个可共享的文件映射; NULL, // 表示使用默认安全对象; PAGE_READWRITE, // 以可读、可写方式打开映射; 0, // 文件映射的最大长度的高32位; MAP_SIZE, // 文件映射的最大长度的低32位; FULL_MAP_NAME // 指定文件映射对象的名字; ); if (hMapFile == NULL) { wprintf(L"CreateFileMapping failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file mapping (%s) is created\n", FULL_MAP_NAME); // 将一个文件映射对象映射到当前应用程序的地址空间; pView = MapViewOfFile(//使用MapViewOfFile来获取共享的内存地址; hMapFile, // 文件映像对象句柄; FILE_MAP_ALL_ACCESS, // 映射对象的文方件数据的访问式; 0, // 文件映射起始偏移的高32位; VIEW_OFFSET, // 表示文件映射起始偏移的低32位; VIEW_SIZE // 映射文件的字节数; ); if (pView == NULL) { wprintf(L"MapViewOfFile failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file view is mapped\n"); PWSTR pszMessage = MESSAGE; DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage); memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);//*********写数据; wprintf(L"This message is written to the view:\n\"%s\"\n", pszMessage); wprintf(L"Press ENTER to clean up resources and quit"); getchar(); Cleanup: if (hMapFile) { if (pView) { UnmapViewOfFile(pView);//是停止当前程序的一个内存映射;在当前应用程序的内存地址空间解除对一个文件映射对象的映射; pView = NULL; } // 关闭文件映射对象; CloseHandle(hMapFile); hMapFile = NULL; } return 0; }

    1.2、从共享内存里读数据

    // ClientCom.cpp : Defines the entry point for the console application. #pragma region #include <stdio.h> #include <windows.h> #pragma endregion //来定义可以扩展和收缩的代码区域的结尾; #define MAP_PREFIX L"Local\\" #define MAP_NAME L"SampleMap" #define FULL_MAP_NAME MAP_PREFIX MAP_NAME // File offset where the view is to begin. #define VIEW_OFFSET 0 // The number of bytes of a file mapping to map to the view. All bytes of the // view must be within the maximum size of the file mapping object. If // VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to the // end of the file mapping. #define VIEW_SIZE 1024 // 实现不同的进程共享数据; //1、要先使用函数CreateFileMapping来创建一个想共享的文件数据句柄; //2、使用MapViewOfFile来获取共享的内存地址; //3、使用OpenFileMapping函数在另一个进程里打开共享文件的名称; int _tmain(int argc, char* argv[]) { HANDLE hMapFile = NULL; PVOID pView = NULL; hMapFile = OpenFileMapping(//打开一个现成的文件映射对象的函数; FILE_MAP_READ, // Read access FALSE, // 如这个函数返回的句柄能由当前进程启动的新进程继承,则这个参数为TRUE; FULL_MAP_NAME // 指定要打开的文件映射对象名称; ); if (hMapFile == NULL) { wprintf(L"OpenFileMapping failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file mapping (%s) is opened\n", FULL_MAP_NAME); // 将一个文件映射对象映射到当前应用程序的地址空间; pView = MapViewOfFile( hMapFile, // 文件映像对象句柄; FILE_MAP_READ, // 映射对象的文件数据的访问式; 0, // 文件映射起始偏移的高32位; VIEW_OFFSET, // 表示文件映射起始偏移的低32位; VIEW_SIZE // 映射文件的字节数; ); if (pView == NULL) { wprintf(L"MapViewOfFile failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file view is mapped\n"); wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);//*****读数据; wprintf(L"Press ENTER to clean up resources and quit"); getchar(); Cleanup: if (hMapFile) { if (pView) { //是停止当前程序的一个内存映射;在当前应用程序的内存地址空间解除对一个文件映射对象的映射; UnmapViewOfFile(pView); pView = NULL; } // 关闭文件映射对象; CloseHandle(hMapFile); hMapFile = NULL; } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-963596.html

    最新回复(0)