文章标题
来源:互联网 发布:淘宝精品橱窗白底图 编辑:程序博客网 时间:2024/05/01 13:51
linux进程间通信——内存共享
1.何为内存共享:
顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存。共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式。不同进程之间共享的内存通常安排为同一段物理内存。进程可以将同一段共享内存连接到它们自己的地址空间中,所有进程都可以访问共享内存中的地址,就好像它们是由用C语言函数malloc分配的内存一样。而如果某个进程向共享内存写入数据,所做的改动将立即影响到可以访问同一段共享内存的任何其他进程。
特别提醒:共享内存并未提供同步机制,也就是说,在第一个进程结束对共享内存的写操作之前,并无自动机制可以阻止第二个进程开始对它进行读取。所以我们通常需要用其他的机制来同步对共享内存的访问,例如前面说到的信号量。
共享内存的声明头文件:
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <error.h>#include <stdlib.h>#include "shmdata.h"void main(){ int runing =1; int shmid; key_t key; void *shared_addr=NULL; struct shared_use_st *shared =NULL; char buffer[BUFSIZ]; key = ftok("/home",5); shmid = shmget(key,512,0666|IPC_CREAT); if(shmid == -1){ printf("shmget failed error is %d",error); exit(EXIT_FAILURE); } shared_addr= shmat(shmid,0,0); if(shared == (void *)-1){ printf("shmat failed error is %d\n",error); exit(EXIT_FAILURE); } printf("Memory attached at %X\n", (int)shared_addr); shared = (struct shared_use_st *)shared_addr; while(runing) { while(shared->written==1){ sleep(1); printf("waiting..."); } printf("enter some text"); fgets(buffer,BUFSIZ,stdin); strncpy(shared->text, buffer, 512); shared->written = 1; if(strncmp(buffer, "end", 3) == 0) runing = 0; } //把共享内存从当前进程中分离 if(shmdt(shared_addr) == -1) { printf("shmdt failed\n"); exit(EXIT_FAILURE); } sleep(2); exit(EXIT_SUCCESS); }
2 读函数
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <error.h>#include <stdlib.h>#include "shmdata.h"//#define Shmget_Size 512void main(){ int runing = 1;//a flag of process wherther or not execution all the time key_t key; int shmid; void *share_addr = NULL;//be used for a return addr of shmmat function struct shared_use_st *shared; //create a shm key = ftok("/home",5); shmid = shmget(key,Shmget_Size,0666|IPC_CREAT); if(shmid == -1){ printf("create shm failed.error is %d",error); exit(EXIT_FAILURE); } //associate with process share_addr = shmat(shmid,0,0); if(share_addr == (void *)-1){ printf("shmmat failed ;error is %d",error); exit(EXIT_FAILURE); } printf("\nMemory attached at %x\n",share_addr); //init shared struct :pointer point to share_addr shared = (struct shared_use_st*)share_addr; shared->written = 0; while(runing) { //read if(shared->written != 0){ printf("you wrote: %s",shared->text); sleep(rand()%3); shared->written = 0; if(strncmp(shared->text, "end",3)==0) runing = 0; } else{ sleep(1); } } //divide shared memary with the process if(shmdt(share_addr) == -1){ printf("shmdt fialed\n"); exit(EXIT_FAILURE); } //delete shared memory if(shmctl(shmid,IPC_RMID,0)==-1){ printf("shmctl failed \n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS);}
3 创建一个”shmdata.h”文件
struct shared_use_st{ int written;//flag 0:can use 1:ban option char text[1024];};
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- Java基础-封装
- c语言题库2
- c++ 产生随机数
- 使用Android Studio开发Android APP
- 第7周项目3-负数把正数赶出队列
- 文章标题
- 项目开发问题记录
- C程序的内存分区
- 第七周项目三(负数把正数赶出队列)
- 学习OpenCV(中文版)PDF及随书源码
- 利用Apptool.jar反编译Apk
- Android使用银联插件支付功能
- 项目去除SVN
- HBase索引总结