System V共享内存区
2014-05-13 09:44
330 查看
1.shmget 函数:创建一个新的共享内存区,或者访问一个已经存在的共享内存区
int shmget(key_t key, size_t size, int oflag);
key 可以是ftok返回的值,也可以是IPC_PRIVATE
size 共享内存区的大小
oflag 共享内存去的操作及读写权限(IPC_CREAT、或者IPC_CREAT|IPC_EXCL,还可以与读写权限按位与)
2.shmat 函数:通过该函数附接到共享内存区(及返回其起始地址)
void* shmat(int shmid, const char* shmaddr, int flag);
shmid 创建的共享内存标示符
shmaddr 一般填NULL,由系统决定起始地址
flag 可以指定只读(SHM_RDONLY),一般赋值为0,读写权限使用shmget指定的
3.shmdt 函数:断开连接共享内存区
int shmdt(const void* shmaddr);
4.shmctl 函数:提供对共享内存区的多种操作
int shmctl(int shmid, int cmd, struct shm-d_ds* buff);
cmd:IPC_RMID、IPC_SET、IPC_STAT
IPC_RMID:从系统中删除共享内存
IPC_SET :设置共享内存shmid_ds结构的成员
IPC_STAT:返回共享内存区当前的shmid_ds结构
==============================================================
/**************************************
*Date:Fri Apr 25 10:32:47 CST 2014
*Target:test System V shared memory
***************************************/
#include <iostream>
extern "C"{
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/errno.h>
#include <unistd.h>
#include <fcntl.h>
}
#define SHM_KEY 0x10002
#define SHM_SIZE 1024*1024*1
using namespace std;
#pragma pack(push,1)
typedef struct Q30MSG{
char MsgType[3+1];
char Iverstd[3+1];
int ID;
char Reverse[1];
}Q30MSG;
#pragma pack(pop)
void write(void* ptr,int size)
{
Q30MSG* temp = (Q30MSG*)ptr;
for(int i = 0;i < size;i++){
Q30MSG msg;
strcpy(msg.MsgType,"Q30");
strcpy(msg.Iverstd,"999");
msg.ID = i + 1;
*temp++ = msg;
}
}
void read(void* ptr,int size)
{
Q30MSG* temp = (Q30MSG*)ptr;
while(size-- != 0){
Q30MSG msg = *temp;
cout << msg.ID << "\t" << msg.MsgType << "\t" << msg.Iverstd << endl;
temp++;
}
}
Q30MSG* readValue(void* ptr,int position)
{
Q30MSG* temp = (Q30MSG*)ptr;
return temp + position;
}
string getValue(void* ptr,int offset)
{
char* addr = (char*)ptr;
char msgType[3+1] = {0};
strncpy(msgType,addr+offset,3);
cout << "getValue:" << msgType << endl;
return msgType;
}
void delShm(void* ptr)
{
int res = shmdt(ptr);
if(res == -1){
cout << "failed to disconnect shm,error:" << strerror(errno) << endl;
}
}
void desShm(int shm_id)
{
int res = shmctl(shm_id,IPC_RMID,NULL);
if(res == -1){
cout << "failed to destroy shm,shm_id:" << shm_id << ",error:" << strerror(errno) << endl;
}
}
int main(int argc,char* argv[])
{
int oflag = IPC_CREAT;//|IPC_EXCL;
oflag |= 0666;
int shm_id = shmget(SHM_KEY,SHM_SIZE,oflag);
if(shm_id == -1){
if(errno == EEXIST){
cout << "this shm has exited" << endl;
}
else{
cout << "failed to create the shm,error:" << strerror(errno) << endl;
return -1;
}
}
void* ptr = NULL;
cout << "shm_id:" << shm_id << endl;
ptr = shmat(shm_id,NULL,0);
if((void*)-1 == ptr){
cout << "failed to at shm,error:" << strerror(errno) << endl;
return -1;
}
write(ptr,5);
read(ptr,5);
cout << "####################" << endl;
Q30MSG* msg = readValue(ptr,4);
if(msg != NULL){
cout << msg->ID << "\t" << msg->MsgType << "\t" << msg->Iverstd << endl;
}
cout << "####################" << endl;
string msgType = getValue(ptr,0);
msgType = getValue(ptr,0+sizeof(Q30MSG));
string iverstd = getValue(ptr,4);
cout << "####################" << endl;
delShm(ptr);
desShm(shm_id);
return 0;
}
1.shmget 函数:创建一个新的共享内存区,或者访问一个已经存在的共享内存区
int shmget(key_t key, size_t size, int oflag);
key 可以是ftok返回的值,也可以是IPC_PRIVATE
size 共享内存区的大小
oflag 共享内存去的操作及读写权限(IPC_CREAT、或者IPC_CREAT|IPC_EXCL,还可以与读写权限按位与)
2.shmat 函数:通过该函数附接到共享内存区(及返回其起始地址)
void* shmat(int shmid, const char* shmaddr, int flag);
shmid 创建的共享内存标示符
shmaddr 一般填NULL,由系统决定起始地址
flag 可以指定只读(SHM_RDONLY),一般赋值为0,读写权限使用shmget指定的
3.shmdt 函数:断开连接共享内存区
int shmdt(const void* shmaddr);
4.shmctl 函数:提供对共享内存区的多种操作
int shmctl(int shmid, int cmd, struct shm-d_ds* buff);
cmd:IPC_RMID、IPC_SET、IPC_STAT
IPC_RMID:从系统中删除共享内存
IPC_SET :设置共享内存shmid_ds结构的成员
IPC_STAT:返回共享内存区当前的shmid_ds结构
==============================================================
/**************************************
*Date:Fri Apr 25 10:32:47 CST 2014
*Target:test System V shared memory
***************************************/
#include <iostream>
extern "C"{
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/errno.h>
#include <unistd.h>
#include <fcntl.h>
}
#define SHM_KEY 0x10002
#define SHM_SIZE 1024*1024*1
using namespace std;
#pragma pack(push,1)
typedef struct Q30MSG{
char MsgType[3+1];
char Iverstd[3+1];
int ID;
char Reverse[1];
}Q30MSG;
#pragma pack(pop)
void write(void* ptr,int size)
{
Q30MSG* temp = (Q30MSG*)ptr;
for(int i = 0;i < size;i++){
Q30MSG msg;
strcpy(msg.MsgType,"Q30");
strcpy(msg.Iverstd,"999");
msg.ID = i + 1;
*temp++ = msg;
}
}
void read(void* ptr,int size)
{
Q30MSG* temp = (Q30MSG*)ptr;
while(size-- != 0){
Q30MSG msg = *temp;
cout << msg.ID << "\t" << msg.MsgType << "\t" << msg.Iverstd << endl;
temp++;
}
}
Q30MSG* readValue(void* ptr,int position)
{
Q30MSG* temp = (Q30MSG*)ptr;
return temp + position;
}
string getValue(void* ptr,int offset)
{
char* addr = (char*)ptr;
char msgType[3+1] = {0};
strncpy(msgType,addr+offset,3);
cout << "getValue:" << msgType << endl;
return msgType;
}
void delShm(void* ptr)
{
int res = shmdt(ptr);
if(res == -1){
cout << "failed to disconnect shm,error:" << strerror(errno) << endl;
}
}
void desShm(int shm_id)
{
int res = shmctl(shm_id,IPC_RMID,NULL);
if(res == -1){
cout << "failed to destroy shm,shm_id:" << shm_id << ",error:" << strerror(errno) << endl;
}
}
int main(int argc,char* argv[])
{
int oflag = IPC_CREAT;//|IPC_EXCL;
oflag |= 0666;
int shm_id = shmget(SHM_KEY,SHM_SIZE,oflag);
if(shm_id == -1){
if(errno == EEXIST){
cout << "this shm has exited" << endl;
}
else{
cout << "failed to create the shm,error:" << strerror(errno) << endl;
return -1;
}
}
void* ptr = NULL;
cout << "shm_id:" << shm_id << endl;
ptr = shmat(shm_id,NULL,0);
if((void*)-1 == ptr){
cout << "failed to at shm,error:" << strerror(errno) << endl;
return -1;
}
write(ptr,5);
read(ptr,5);
cout << "####################" << endl;
Q30MSG* msg = readValue(ptr,4);
if(msg != NULL){
cout << msg->ID << "\t" << msg->MsgType << "\t" << msg->Iverstd << endl;
}
cout << "####################" << endl;
string msgType = getValue(ptr,0);
msgType = getValue(ptr,0+sizeof(Q30MSG));
string iverstd = getValue(ptr,4);
cout << "####################" << endl;
delShm(ptr);
desShm(shm_id);
return 0;
}
相关文章推荐
- 共享内存实现进程间大数据的交换
- KSM(Kernel Samepage Merging) 剖析:Linux 内核中的内存去耦合[转]
- linux c mmap()映射共享内存实例
- 共享内存的使用
- Linux进程间通信共享内存
- VC 共享内存实现进程间大数据的交换
- 关于共享内存的一个题目(未完)
- 共享内存的删除
- 进程间通信---共享内存
- 【Java并发编程】之五:volatile变量修饰符—意料之外的问题(含代码)
- Linux XSI IPC 之共享内存
- 共享内存实现进程间大数据的交换
- 共享内存1
- 共享内存2
- linux进程间通信精讲
- 进程间通信之共享内存
- linux多线程【8】mmap实现父子进程的共享内存通信,用信号量同步
- linux多线程 & IPC【10】自己写一个系统日志服务
- linux多线程 & IPC【12】System V 共享内存
- Linux共享内存管理