共享内存 中 存结构体,多线程多进程间实现共享。
#define FILE_MSG_KEY 0xc54be5 ///< File message key. #define PROC_MEM_SIZE 4096*3 #define MAX_SHARE_PROC 9 #define IPC_CREAT 00001000 /* create if key is nonexistent */ static int mFileId,qFileId; static void *pShareMem; int pShareMemInit(int key) { int mid = shmget(key, PROC_MEM_SIZE * MAX_SHARE_PROC, IPC_CREAT | 0660); printf("shared memory size %d\n", PROC_MEM_SIZE * MAX_SHARE_PROC); if(mid < 0) mid = shmget(key, 0, 0); printf("shared memory id:%d\n",mid); if(mid < 0) return -1; return mid; } int readSysInfoCfg() { mFileId = pShareMemInit(FILE_MSG_KEY); if(mFileId < 0) return -1; pShareMem = shmat(mFileId,0,0); SysInfo *pSysInfo = (SysInfo *)pShareMem; }
时间: 2024-10-09 04:06:21