不多說,直接貼**就是了
#ifndef sharehelper_h_included
#define sharehelper_h_included
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;
class sharehelper
;#endif // sharehelper_h_included
#include "sharehelper.h"
key_t sharedkey = 99;
size_t maxsize = 200;
union semum
arg;
sharehelper::sharehelper()
shmptr = shmat(shmid, null, 0);
if (shmptr == (void*)-1)
union semum sem;
sem.val = 1;
semid = semget(sharedkey, 1, ipc_creat | 0777);
if (semid == -1)
semctl(semid, 0, setval, sem);
}sharehelper::~sharehelper()
bool sharehelper::setsharedstr(string str)
string sharehelper::getsharedstr()
void sharehelper::lockshare()
void sharehelper::unlockshare()
int sharehelper::delsem()
int sharehelper::psem()
; return (semop(semid, &sops, 1));
}int sharehelper::vsem()
; return (semop(semid, &sops, 1));
}
臨界區分別是lockshare和unlockshare,共享記憶體則是setsharedstr和getsharedstr,建立乙個例項呼叫即可
經驗證還是挺好用的,注意字串的長度
程序間共享記憶體
位於系統的交換分割槽 include incude int shmget key t key,size t size,int shm ipc excl ipc creat一起使用可確保共享記憶體已存在時返回錯誤 void shmat int shm id,const void shm addr,in...
程序間通訊 共享記憶體
下面是自己寫的乙個簡單的共享記憶體的程序間通訊的例子。共享記憶體是用於程序間大量資料共享的一種方法。include include include include include include int main if buf1 shmat shmid,0,0 void 1 strcpy buf1,...
linux 程序間共享記憶體
可以採用sysv的shmget shmat 實現。但是我更喜歡shm open mmap 更簡單。writer.c include include include include include include include include struct ofs stat int main voi...