标签:
#include <sys/ipc.h> #include <sys/shm.h> int shmget(key_t key, size_t size, int shmflg);
#include <sys/types.h> #include <sys/shm.h> void *shmat(int shmid, const void *shmaddr, int shmflg);
第一次创建完共享内存时,它还不能被任何进程访问,shmat函数的作用就是用来启动对该共享内存的访问,并把共享内存连接到当前进程的地址空间。
调用成功时返回一个指向共享内存第一个字节的指针,如果调用失败返回-1.
#include <sys/types.h> #include <sys/shm.h>
int shmdt(const void *shmaddr);
该函数用于将共享内存从当前进程中分离。注意:将共享内存分离并不是删除它,只是使该共享内存对当前进程不再可用。
#include <sys/ipc.h> #include <sys/shm.h> int shmctl(int shmid, int cmd, struct shmid_ds *buf);
与信号量的semctl函数一样,用来控制共享内存
struct shmid_ds { struct ipc_perm shm_perm; /* Ownership and permissions */ size_t shm_segsz; /* Size of segment (bytes) */ time_t shm_atime; /* Last attach time */ time_t shm_dtime; /* Last detach time */ time_t shm_ctime; /* Last change time */ pid_t shm_cpid; /* PID of creator */ pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */ shmatt_t shm_nattch; /* No. of current attaches */ ... };
示例
memory_write.c为创建共享内存并向里面写入由中断输入得数据,memory_read.c为读取共享内存中的内容。两个程序运行在两个不相关的进程中,为了保证两个进程间的读写同步,设置了WR_RD标记。
memory_write.c的程序如下
#include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 2048 #define BUF_SIZE 2047 #define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); }while(0) struct share_memory { int WR_RD; //非0表示可写,0表示可读 char text[MAX_SIZE]; }; int main() { int shmid; char buffer[BUF_SIZE+1]; //用于保存输入的文本 shmid = shmget(1234,sizeof(struct share_memory),0666 | IPC_CREAT); //创建共享内存 if(shmid==-1) ERR_EXIT("shmget failed\n"); struct share_memory *sh_mem; void *sh=NULL; sh = shmat(shmid,0,0); //将共享内存连接到当前进程的地址空间 if(sh ==(void *)-1) ERR_EXIT("shmat failed\n"); else printf("Memory attached at9 %x\n",(int)sh); sh_mem=(struct share_memory *)sh; sh_mem->WR_RD=1; // 置为可写 int flag=1; while(1) { while(!sh_mem->WR_RD) //共享内存为可读,表示内存里的数据还未被读取,不能写,此时堵塞 { if(flag) { printf("memory is not empty,waiting.....\n"); flag=0; } sleep(1); } flag=1; /*向共享内存写入数据*/ printf("Enter some text(Enter ‘end‘ to quit): \n"); fgets(buffer,BUF_SIZE,stdin); strncpy(sh_mem->text,buffer,sizeof(buffer)); sh_mem->WR_RD=0; //写完数据之后设置共享内存可读 if(strncmp(buffer,"end",3)==0) break; //输入end结束循环 } if(shmdt(sh)==-1) ERR_EXIT("shmdt failed\n"); //将共享内存从当前进程分离 sleep(2); return 0; }
memory_read.c的程序如下
#include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #define MAX_SIZE 2048 #define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); }while(0) struct share_memory { int WR_RD; //非0表示可写,0表示可读 char text[MAX_SIZE]; }; int main() { int shmid; srand(getpid()); shmid = shmget(1234,sizeof(struct share_memory),0666 | IPC_CREAT); //创建共享内存 if(shmid==-1) ERR_EXIT("shmget failed\n"); struct share_memory *sh_mem; void *sh=NULL; sh = shmat(shmid,0,0); //将共享内存连接到当前进程的地址空间 if(sh ==(void *)-1) ERR_EXIT("shmat failed\n"); else printf("Memory attached at %x\n",(int)sh); sh_mem=(struct share_memory*)sh; //sh_mem->WR_RD=ture; // 置为可写 while(1) if(!sh_mem->WR_RD) //共享内存为可读,从共享内存读取数据 { printf("your wrote: %s", sh_mem->text); sleep(rand()%3); sh_mem->WR_RD=1; //读完数据之后设置共享内存可写 if(strncmp(sh_mem->text,"end",3)==0) break; //输入end结束循环 } else //表示内存为空,不能读,此时堵塞 { //printf("memory is empty,waiting.....\n"); sleep(1); } if(shmdt(sh)==-1) ERR_EXIT("shmdt failed\n"); //将共享内存从当前进程分离 if(shmctl(shmid,IPC_RMID,0)==-1) ERR_EXIT("shmctl failed\n"); return 0; }
Linux共享内存使用常见陷阱与分析 - 51CTO.COM http://os.51cto.com/art/201311/418977_all.htmIPC---共享内存
标签:
原文地址:http://www.cnblogs.com/wujing-hubei/p/5746929.html