IPC(进程间通信)包括以下一种方法:
windows:http://bbs.pediy.com/showthread.php?threadid=26252
OPPOS.h
/* * OPPOS * 定义运维平台的系统 */ //平台定义 //#define OPP_WIN32 #define OPP_LINUX //#define OPP_UNIX //编译器定义 //#define CC_VC #define CC_GCC
CShareMemory.h
#ifndef _CSHAREMEMORY_H_ #define _CSHAREMEMORY_H_ /************************************************************************/ /* //共享内存操作类 //析构函数里的内存回收问题 如何使用: //linux下string类型不允许 typedef struct _Shared { int index; char msg[4096]; }ShareMemoryTest; windows: CShareMemory<ShareMemoryTest> g_smdt("test"); g_smdt->index = num; g_smdt->msg = strMsg; unix: 用posix共享内存方式 CShareMemory<ShareMemoryTest> g_smdt("myshm2"); 手动删除 /dev/shm下的共享内存的命令: 取得ipc信息: ipcs [-m|-q|-s] -m 输出有关共享内存(shared memory)的信息 -q 输出有关信息队列(message queue)的信息 -s 输出有关“遮断器”(semaphore)的信息 %ipcs -m 删除ipc ipcrm -m|-q|-s shm_id */ /************************************************************************/ #include "OPPOS.h" #ifndef OPP_WIN32 #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <fcntl.h> /* for O_* constants */ #include <string.h> using namespace std; #else #include <Windows.h> #endif //共享内存操作类 template<class ShareMemoryDataType> class CShareMemory { public: CShareMemory(); CShareMemory(char* name); ~CShareMemory(); bool createShareMemory(char* name); //用的时候需要加锁 ShareMemoryDataType *operator->(); //用的时候需要加锁 ShareMemoryDataType* getData(); private: char m_strSharedName[260]; //共享内存标识名 unsigned long m_dwMaxDataSize; //共享内存的最大值 ShareMemoryDataType* m_pData; //共享内存中存放的数据 bool m_bCreated; //共享内存是否已经被打开 #ifdef OPP_WIN32 HANDLE m_hFileMapping; //共享内存文件的句柄 #endif }; template<class ShareMemoryDataType> CShareMemory<ShareMemoryDataType>::CShareMemory() { m_pData = NULL; #ifdef OPP_WIN32 m_hFileMapping = NULL; #endif } template<class ShareMemoryDataType> CShareMemory<ShareMemoryDataType>::CShareMemory(char* name) { CShareMemory(); createShareMemory(name); } template<class ShareMemoryDataType> CShareMemory<ShareMemoryDataType>::~CShareMemory() { #ifdef OPP_WIN32 if(m_pData) { //把映射对象从当前应用程序的地址空间unmap UnmapViewOfFile(m_pData); m_pData = NULL; } if(m_hFileMapping) { CloseHandle(m_hFileMapping); } #else if(m_pData != NULL) { //释放映射对象 munmap(m_pData, 128); } //删除共享内存 shm_unlink(m_strSharedName); #endif } template<class ShareMemoryDataType> ShareMemoryDataType *CShareMemory<ShareMemoryDataType>::operator->() { return m_pData; } template<class ShareMemoryDataType> ShareMemoryDataType* CShareMemory<ShareMemoryDataType>::getData() { return m_pData; } template<class ShareMemoryDataType> bool CShareMemory<ShareMemoryDataType>::createShareMemory(char* name) { m_dwMaxDataSize = sizeof(ShareMemoryDataType); strcpy(m_strSharedName, name); /////////////////////////////////////////////////////////////////////////// #ifdef OPP_WIN32 //创建文件映射内核对象 m_hFileMapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, // 默认的安全属性 PAGE_READWRITE, // 可读写文件的数据 0, // size: high 32-bits;对于4GB以下的文件,该参数总是0 sizeof(ShareMemoryDataType), // size: low 32-bits;映射文件的字节数 name); // 命名该映射对象,用于和其他进程共享该对象 if(m_hFileMapping == NULL) { int err = GetLastError(); return false; } //判断是否第一个访问共享内存的程序 m_bCreated = (GetLastError() == ERROR_ALREADY_EXISTS); //// 设置共享内存的访问权限 //SetNamedSecurityInfo(name, // SE_KERNEL_OBJECT, // DACL_SECURITY_INFORMATION, //// change only the object's DACL // 0, // do not change owner or group // 0, // do not change owner or group // (PACL)NULL, //no DACL specified - allows everyone access // NULL); // do not change SACL系统访问控制列表 //将一个文件映射对象映射到当前应用程序的地址空间 m_pData = (ShareMemoryDataType*)MapViewOfFile(m_hFileMapping, FILE_MAP_READ | FILE_MAP_WRITE, //可以读写文件 0, // high offset: map from 0, // low offset: beginning 0); // default: map entire file if(m_pData == NULL) { CloseHandle(m_hFileMapping); return false; } //如果是第一个创建共享内存的程序,则初始化 if (!m_bCreated) memset(m_pData, '/0', m_dwMaxDataSize); #else //在目录/dev/shm/下创建共享内存 int fd = shm_open(name, O_CREAT | O_RDWR, 0777); if (fd == -1) { perror("shm_open"); return false; } const size_t regionSize = sysconf(_SC_PAGE_SIZE); //页得整数倍 int dataSize = sizeof(ShareMemoryDataType); int num = dataSize / regionSize; num += 1; int memSize = num * regionSize; //共享内存大小 int r = ftruncate(fd, memSize); //将文件映射进内存 m_pData = (ShareMemoryDataType*)mmap(0, //映射区的开始位置 memSize, //映射区的长度 PROT_READ | PROT_WRITE, //期望的内存保护类型 MAP_SHARED, //映射对象的类型 fd, //文件描述符 0); //被映射对象内容的起点 #endif return true; } #endif
CMutex.h
#ifndef _CMUTEX_H_ #define _CMUTEX_H_ /************************************************************************/ /* //进程间的互斥类 windows: CreateMutex unix: 信号量 */ /************************************************************************/ #include "OPPOS.h" #ifndef OPP_WIN32 #include <string.h> #include <stdio.h> #include <semaphore.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #else #include <Windows.h> #endif #define MUTEXNAMELENGTH 260 //进程间的加锁类 class CMutex { public: CMutex(); CMutex(char* name); ~CMutex(); void createMyMutex(char* name); void lock(); void unlock(); private: #ifdef OPP_WIN32 HANDLE m_mutex; #else sem_t *m_sem; char m_semName[MUTEXNAMELENGTH]; #endif }; #endif
CMutex.cpp
#include "CMutex.h" CMutex::CMutex() { } CMutex::~CMutex() { #ifdef OPP_WIN32 WaitForSingleObject(m_mutex, INFINITE); #else sem_unlink(m_semName); #endif } CMutex::CMutex(char* name) { CMutex(); createMyMutex(name); } void CMutex::createMyMutex(char* name) { #ifdef OPP_WIN32 m_mutex = CreateMutexA(NULL, FALSE, name); if(m_mutex == NULL) { //create error } #else //create semaphore, semaphore have a name then use in processes m_sem=sem_open(name,O_CREAT|O_RDWR,0777,1); if(m_sem == SEM_FAILED) { printf("/nsem_open failed, (%s)/n", strerror(errno)); } strcpy(m_semName, name); #endif } void CMutex::lock() { #ifdef OPP_WIN32 WaitForSingleObject(m_mutex, INFINITE); #else sem_wait(m_sem); #endif } void CMutex::unlock() { #ifdef OPP_WIN32 ReleaseMutex(m_mutex); #else sem_post(m_sem); #endif }
man.cpp
//#define OPP_LINUX //g++ main.cpp CMutex.cpp -lrt -lpthread -o main #include "CMutex.h" #include "CShareMemory.h" #include <iostream> #include <unistd.h> using namespace std; //linux下string类型不允许 typedef struct _Shared { int index; char msg[4096]; }ShareMemoryTest; int main() { char* name = "test1234"; CMutex mutex(name); CShareMemory<ShareMemoryTest> g_smdt(name); char cString[260]; int num; cout<<"start"; mutex.lock(); cout<<"read"; cout<<g_smdt->index<<":::"<<g_smdt->msg; int ii; cin>>ii; cout<<"write"; g_smdt->index = 1; for(int i=0; i<5;i++) { g_smdt->msg[i] = 'b'; } mutex.unlock(); //while(1) //{ // cout<<"1: set data /n 2: get data /n"; // int iChoice=0; // cin>>iChoice; // if(iChoice == 1) // { // mutex.lock(); // cout<<"/n input the int: /n"; // cin>>num; // cout<<"/n input the string: /n"; // cin>>cString; // g_smdt->index = num; // strcpy(g_smdt->msg, cString); // mutex.unlock(); // } // else if(iChoice == 2) // { // mutex.lock(); // cout<<g_smdt->index<<":::"<<g_smdt->msg<<"/n"; // mutex.unlock(); // } // else // break; //} system("pause"); return 0; }