Posix共享内存区
1、概述
Posix提供了两种在无亲缘关系进程间共享内存区的方法:
(1)内存映射文件:先有open函数打开,然后调用mmap函数把得到的描述符映射到当前进程地址空间中的一个文件(上一篇笔记所用到的就是)。
(2)共享内存区对象:先有shm_open打开一个Posix IPC名字(也可以是文件系统中的一个路径名),然后调用mmap将返回的描述符映射到当前进程的地址空间。
者两种方法多需要调用mmap,差别在于作为mmap的参数之一的描述符的获取手段。
2、Posix共享内存区对象
Posix共享内存区涉及以下两个步骤要求:
(1)指定一个名字参数调用shm_open,以创建一个新的共享内存区对象或打开一个已经存在的共享内存区对象。
(2)调用mmap把这个共享内存区映射到调用进程的地址空间。
注意:mmap用于把一个内存区对象映射到调用进程地址空间的是该对象的一个已经打开描述符。
关于Posix共享内存区对象的API如下:

1 #include <sys/mman.h>
2 #include <sys/stat.h> /* For mode constants */
3 #include <fcntl.h> /* For O_* constants */
4 int shm_open(const char *name, int oflag, mode_t mode);
5 int shm_unlink(const char *name);
6 int ftruncate(int fd, off_t length);
7 int fstat(int fd, struct stat *buf);

参考网址 http://linux.die.net/man/3/shm_open http://linux.die.net/man/2/ftruncate
简单的程序:
(1)指定名字和长度创建一个共享内存区对象,程序如下:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/mman.h>
6 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
7 int main(int argc,char* argv[])
8 {
9 int c,fd,flags;
10 char *ptr;
11 off_t length;
12 flags = O_RDWR | O_CREAT;
13 while( (c = getopt(argc,argv,"e")) != -1)
14 {
15 switch(c)
16 {
17 case 'e':
18 flags |= O_EXCL;
19 break;
20 }
21 }
22 if(optind != argc -2)
23 {
24 perror("usage: shmcreate [-e] <name> <length>");
25 exit(0);
26 }
27 length = atoi(argv[optind + 1]);
28 fd = shm_open(argv[optind],flags,FILE_MODE);
29 ftruncate(fd,length);
30 ptr = mmap(NULL,length,PROT_READ | PROT_WRITE ,MAP_SHARED,fd,0);
31 exit(0);
32 }

(2)从系统中删除一个共享内存区对象的名字,程序如下:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/mman.h>
6 #include <errno.h>
7 int main(int argc,char* argv[])
8 {
9 if(argc != 2)
10 {
11 printf("usage: posixshmunlink <name>");
12 exit(0);
13 }
14 if(shm_unlink(argv[1]) == -1)
15 {
16 perror("shm_unlink error");
17 exit(-1);
18 }
19 exit(0);
20 }

(3)往共享内存区对象中写入一个模式,程序如下:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/mman.h>
6 #include <errno.h>
7 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
8 int main(int argc,char *argv[])
9 {
10 int i,fd;
11 struct stat stat;
12 unsigned char *ptr;
13 if(argc != 2)
14 {
15 printf("usage: shmwrite <name>");
16 exit(0);
17 }
18 fd = shm_open(argv[1],O_RDWR,FILE_MODE); //打开所指定的共享内存区对象
19 fstat(fd,&stat); //获取内存区的大小
20 ptr = mmap(NULL,stat.st_size,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0); //映射到共享内存区
21 close(fd);
22 for(i=0;i<stat.st_size;++i)
23 *ptr++ = i % 256;
24 exit(0);
25 }

3、例子
采用Posix共享内存实现给一个共享的计数器持续加1,它由多个进程给存放在共享内存区中的某个计数器持续加1。将计数器放在一个共享内存区中,并用一个有名信号量来同步。
创建并初始化共享内存区和信号量的程序如下:server1.c

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <semaphore.h>
10
11 #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
12
13 //计数器结构体
14 struct shmstruct
15 {
16 int count;
17 };
18 //同步有名信号量
19 sem_t *mutex;
20
21 int main(int argc,char *argv[])
22 {
23 int fd;
24 struct shmstruct *ptr;
25 if(argc != 3)
26 {
27 printf("usage: server1 <shmname> <semname>.\n");
28 exit(0);
29 }
30 //防止所需共享内存区对象已经存在
31 shm_unlink(argv[1]);
32 //创建一个新的共享内存区对象
33 if((fd = shm_open(argv[1],O_RDWR | O_CREAT | O_EXCL,FILE_MODE)) == -1)
34 {
35 perror("shm_open error");
36 exit(-1);
37 }
38 //指定新创建的共享内存区对象的大小
39 ftruncate(fd,sizeof( struct shmstruct));
40 //将新创建的共享内存区映射到调用进程的地址空间
41 if((ptr = mmap(NULL,sizeof(struct shmstruct),PROT_READ | PROT_WRITE,MAP_SHARED,fd,0)) == MAP_FAILED)
42 {
43 perror("mmap error");
44 exit(-1);
45 }
46 //关闭对象描述符
47 close(fd);
48 //防止所需的信号量已经存在
49 sem_unlink(argv[2]);
50 //创建有名信号量,作为互斥锁用
51 if((mutex = sem_open(argv[2],O_CREAT|O_EXCL,FILE_MODE,1)) == SEM_FAILED)
52 {
53 perror("sem_open error");
54 exit(-1);
55 }
56 //关闭信号量
57 sem_close(mutex);
58 exit(0);
59 }

给存放在共享内存区中的一个计数器加1程序如下:client1.c

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <semaphore.h>
10
11 #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
12
13 struct shmstruct
14 {
15 int count;
16 };
17
18 sem_t *mutex;
19
20 int main(int argc,char *argv[])
21 {
22 int fd,i,nloop;
23 pid_t pid;
24 struct shmstruct *ptr;
25 if(argc != 4)
26 {
27 printf("usage: client1 <shmname> <semname> <#loops>.\n");
28 exit(0);
29 }
30 nloop = atoi(argv[3]);
31 //打开共享内存区
32 if((fd = shm_open(argv[1],O_RDWR,FILE_MODE)) == -1)
33 {
34 perror("shm_open error");
35 exit(0);
36 }
37 //将共享内存区映射到进程地址空间
38 if((ptr = mmap(NULL,sizeof(struct shmstruct),PROT_READ | PROT_WRITE,MAP_SHARED,fd,0)) == MAP_FAILED)
39 {
40 perror("mmap error");
41 exit(-1);
42 }
43 close(fd);
44 //打开信号量
45 if((mutex = sem_open(argv[2],0)) == SEM_FAILED)
46 {
47 printf("sem_open error");
48 exit(-1);
49 }
50 pid = getpid();
51 for(i=0;i<nloop;i++)
52 {
53 sem_wait(mutex); //锁住信号量
54 printf("pid %ld: %d\n",(long) pid,ptr->count++);
55 sem_post(mutex); //释放信号量
56 }
57 exit(0);
58 }

程序执行结果如下所示:

Posix共享内存区的更多相关文章
- linux c编程:Posix共享内存区
Posix共享内存区:共享内存是最快的可用IPC形式.它允许多个不相关(无亲缘关系)的进程去访问同一部分逻辑内存.如果需要在两个进程之间传输数据,共享内存将是一种效率极高的解决方案.一旦这样的内存区映 ...
- Posix 共享内存区
要点 与mmap配合使用 open与shm_open的区别,open打开磁盘上的普通文件,shm_open创建和打开的文件在/dev/shm文件夹下,该文件夹对应的是内存 gcc编译时加参数-lrt ...
- Unix IPC之共享内存区(1)
1 共享内存区 共享内存区是可用IPC形式中最快的,只有映射和解除映射需要进入内核的系统调用,映射后对共享内存区的访问和修改不再需要系统调用(内核只要负责好页表映射和处理页面故障即可),但通常需要同步 ...
- System V 共享内存区
1.概述 系统调用mmap通过映射一个普通文件实现共享内存.System V 则是通过映射特殊文件系统shm中的文件实现进程间的共享内存通信.也就是说,每个共享内存区域对应特殊文件系统shm中的一个文 ...
- Linux环境编程之共享内存区(一):共享内存区简单介绍
共享内存区是可用IPC形式中最快的.一旦内存区映射到共享它的进程的地址空间,进程间数据的传递就不再涉及内核.然而往该共享内存区存放信息或从中取走信息的进程间通常须要某种形式的同步.不再涉及内核是指:进 ...
- Linux IPC实践(10) --Posix共享内存
1. 创建/获取一个共享内存 #include <sys/mman.h> #include <sys/stat.h> /* For mode constants */ #inc ...
- POSIX共享内存
DESCRIPTION 共享内存是最快的可用IPC形式.它允许多个不相关(无亲缘关系)的进程去访问同一部分逻辑内存. 如果需要在两个进程之间传输数据,共享内存将是一种效率极高的解决方案.一旦这样的内存 ...
- POSIX 共享内存和 系列函数
在前面介绍了system v 共享内存的相关知识,现在来稍微看看posix 共享内存 和系列函数. 共享内存简单来说就是一块真正的物理内存区域,可以使用一些函数将这块区域映射到进程的地址空间进行读写, ...
- 细说linux IPC(四):posix 共享内存
[版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet 或 .../gentleliu,文章仅供学习交流,请勿用于商业用途] 上一节讲了由open函数打开一 ...
随机推荐
- HashTable HashMap HashSet区别(java)
Hashtable: 1. key和value都不许有null值 2. 使用enumeration遍历 3. 同步的,每次只有一个线程能够访问 4. 在java中Hashtable是H大写,t小写,而 ...
- 监听视图树 ViewTreeObserver 获取View的宽高
前奏:在哪里可以获取到View的宽高 我们知道,在onCreate方法执行完毕以后,View才开始被测量,所以我们在onCreate方法里面通过view.getWidth()或view.getMeas ...
- Java基础(九):抽象类
在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不能实例化对象之外, ...
- ECMAScript5之Object学习笔记(二)
继续第二部分 Object.freeze(obj) 看字面意思就是“把一个对象冻结”. 下面我们来看个简单的例子以作说明: // a person instance var person = { na ...
- Cognos第三方用户认证(CustomJavaProvider)
关于Cognos第三方用户认证(CustomJavaProvider)的demo网上的例子很多,当然最权威的你可以从Cognos安装的SDK中去探索,本文不详细的说明代码,主要说一下认证的处理过程,以 ...
- 【React全家桶入门之十三】Redux中间件与异步action
在上一篇中我们了解到,更新Redux中状态的流程是这种:action -> reducer -> new state. 文中也讲到.action是一个普通的javascript对象.red ...
- 线性表 顺序存储 链式存储 ---java实现
首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...
- Closing a window
The obvious way to how to close a window is to click on the x mark on the titlebar. In the next exam ...
- 各种样式的table 及 代码
1.模板一 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- plsql oracle client没有正确安装(plsql连接远程数据库)
plsql oracle client没有正确安装(plsql连接远程数据库) CreateTime--2018年4月23日16:55:11 Author:Marydon 1.情景再现 2.问题解 ...