Мне нужно организовать что то вроде shared memory только под Виндовс для интер-процессинг. После поисков в Гугле, использую File Mapping. Но ничего не нашла о реализации синхронизации между врайтером и ридером... Т.е. как ридер дожен узнать, что есть дата.. и т.д.
Вот тут код, как лучше довести его до ума...:
Код | #ifndef _SHARED_MEMORY_ #define _SHARED_MEMORY_
#include <windows.h> #include <stdio.h> #include <conio.h>
#include <string> using std::string;
class CSharedMemoryArea { public: CSharedMemoryArea(){}
HRESULT CreateSharedMemoryArea(LPCSTR FileName);
HRESULT OpenSharedMemory(LPCSTR FileName);
HRESULT DestroySharedMemoryArea ();
void WriteOnSharedMemory(int data, int position);
void ReadFromSharedMemory(int *data, int position);
private: HANDLE m_hFile; LPINT m_ipView, m_ipAux;
};
#endif
|
Код | // ------------------------------------------------------ // Shared memory functions // Sample file to create a Shared Memory area... // ------------------------------------------------------
//#include <stdafx.h>
#include "SharedMemory.h"
//________________________________________________________________ // HRESULT CSharedMemoryArea::CreateSharedMemoryArea(LPCSTR FileName) {
m_hFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 1024 * 4, FileName); if (m_hFile == NULL) { printf("Unable to create a file. Error %d", GetLastError()); return 0; //@@ } else { if (GetLastError() == ERROR_ALREADY_EXISTS) { printf("A file was already created.Error %d", GetLastError()); return 0; } } m_ipView = (LPINT) MapViewOfFile(m_hFile, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (m_ipView == NULL) { printf("Unable to create a VIEW.Error %d", GetLastError()); return 0; }
printf("Shared memory has been successfully created....\n");
m_ipAux = m_ipView; return 1; }
//__________________________________________________________ // HRESULT CSharedMemoryArea::OpenSharedMemory(LPCSTR FileName) { m_hFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, FileName); if (m_hFile == NULL) { printf("Unable to open the shared area.\n"); return 0; }
m_ipView = (LPINT) MapViewOfFile(m_hFile, FILE_MAP_ALL_ACCESS, //FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 0); if (m_ipView == NULL) { printf("Unable to create a VIEW. Error %d", GetLastError()); return 0; } printf("Shared memory has been successfully openned....\n");
m_ipAux = m_ipView; return S_OK; }
//___________________________________________________ // HRESULT CSharedMemoryArea::DestroySharedMemoryArea () {
if (!UnmapViewOfFile(m_ipView)) { printf("Could not unmap view of file. Error %d ", GetLastError()); } CloseHandle(m_hFile); printf("Shared memory has been successfully destroyed....\n"); return 1;
}
//_________________________________________________________________ // void CSharedMemoryArea::WriteOnSharedMemory(int data, int position) { printf("Writing data on the shared area...\n"); m_ipAux[position] = data; }
//___________________________________________________________________ // void CSharedMemoryArea::ReadFromSharedMemory(int *data, int position) { printf("Reading data on the shared area...\n"); *data = m_ipAux[position]; }
|
Reader
Код | // ------------------------------------------------------------ // User.cpp // This program uses a shared area // It should run AFTER the other program. // ------------------------------------------------------------ #include <stdio.h> #include "SharedMemory.h"
void main(int argc, char* argv[]) { int i, data; CSharedMemoryArea cSM; HRESULT hr; hr = cSM.OpenSharedMemory("SharedFile");
printf("Reading data from the shared memory area...\n"); for (i=0; i< 100; i++) { cSM.ReadFromSharedMemory(&data, i); printf("Data[%d] = %3d\n", i, data); } printf("The reading is done.\n"); cSM.DestroySharedMemoryArea (); printf("The End."); }
|
Writer
Код | // ------------------------------------------------------------ // Creator.cpp // This program creates a shared area. // ------------------------------------------------------------ #include <stdio.h> #include <conio.h> #include "SharedMemory.h"
void main(int argc, char* argv[]) { int i; CSharedMemoryArea cSM; HRESULT hr; // creates the area to be shared hr = cSM.CreateSharedMemoryArea("SharedFile");
printf("Writing data on the shared memory area...\n"); for (i=0; i< 100; i++) cSM.WriteOnSharedMemory(i*2, i); // performs the writing
printf("The writing is done.\n"); printf("Now you can run the other program to read the shared area.\n"); printf("When you want to close the shared area and finish this program...\n"); printf("Just press a key.\n");
_getch(); cSM.DestroySharedMemoryArea (); printf("The End."); }
|
|