shm_open函数实例及说明
来源:互联网 发布:2017网络直播平台排名 编辑:程序博客网 时间:2024/05/21 18:11
使用shm_open来操作共享内存
shm_open最主要的操作也是默认的操作就是在/dev/shm/下面,建立一个文件。
文件名字是用户自己输入的。
要点一定要用ftruncate把文件大小于设置为共享内存大小。
服务端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
char
buf[10];
char
*ptr;
int
main()
{
int
fd;
fd = shm_open(
"region"
, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if
(fd<0) {
printf
(
"error open region\n"
);
return
0;
}
ftruncate(fd, 10);
ptr = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if
(ptr == MAP_FAILED) {
printf
(
"error map\n"
);
return
0;
}
*ptr = 0x12;
return
0;
}
客户端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
char
buf[10];
char
*ptr;
int
main()
{
int
fd;
fd = shm_open(
"region"
, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if
(fd<0) {
printf
(
"error open region\n"
);
return
0;
}
ftruncate(fd, 10);
ptr = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if
(ptr == MAP_FAILED) {
printf
(
"error map\n"
);
return
0;
}
while
(*ptr != 0x12);
printf
(
"ptr : %d\n"
, *ptr);
return
0;
}
阅读全文
0 0
- shm_open函数实例及说明
- shm_open()函数
- 函数指针的实例说明及个人注释
- C++虚函数的作用及实例说明
- xpat 说明及实例
- 多线程实例函数说明
- php函数及说明
- POSIX中内存共享(shm_open)的简单实例
- Ajax简要说明及实例
- Ajax简要说明及实例
- Ajax简要说明及实例
- Enlib中文说明及实例
- WEBWORK入门及实例说明
- Ajax实例及简要说明
- WEBWORK入门及实例说明
- SQL注入,及实例说明
- httpd配置:实例及说明
- Ajax实例及简要说明
- 在windows系统中安装Sulley
- MVP其中有接口(x.http网络请求)
- android NDK
- 事务隔离级别
- 自定义view圆环进度条
- shm_open函数实例及说明
- 虚拟机Centos7配置网络
- HDOJ 1228 A + B(水题)
- 每次运行appium时需要安装unlock,setting,inputmanager三个apk文件的解决方法
- iOS推送javaPNS源码解析二,消息体类
- 经典IT书留存
- java面试宝典
- 接口回调实现登陆注册界面
- 创建结点操作表格