原文:http://www.oschina.net/code/snippet_237505_8645
/*
int shm_open(const char *name, int oflag, mode_t mode);
//创建或打开一个共享内存,成功返回一个整数的文件描述符,错误返回-1。
1.name:共享内存区的名字;
2.标志位;open的标志一样
3.权限位
int shm_unlink(const char *name);
编译时要加库文件-lrt
*/
标签: <无>
代码片段(2)
[文件] read.c ~ 1KB 下载(25)
01 | #include <sys/types.h> |
02 | #include <sys/stat.h> |
03 | #include <fcntl.h> |
04 | #include <stdio.h> |
05 | #include <sys/mman.h> |
06 | #include <string.h> |
07 | #include <errno.h> |
08 | #include <unistd.h> |
09 | |
10 | /* |
11 | int shm_open(const char *name, int oflag, mode_t mode); |
12 | //创建或打开一个共享内存,成功返回一个整数的文件描述符,错误返回-1。 |
13 | 1.name:共享内存区的名字; |
14 | 2.标志位;open的标志一样 |
15 | 3.权限位 |
16 | int shm_unlink(const char *name); |
17 | |
18 | 编译时要加库文件-lrt |
19 | */ |
20 | |
21 | #define SHMNAME "shm_ram" |
22 | #define OPEN_FLAG O_RDWR|O_CREAT |
23 | #define OPEN_MODE 00777 |
24 | #define FILE_SIZE 4096*4 |
25 | |
26 | |
27 | int main( void ) |
28 | { |
29 | int ret = -1; |
30 | int fd = -1; |
31 | |
32 | char buf[4096] = {0}; |
33 | void * add_r = NULL; |
34 | |
35 | //创建或者打开一个共享内存 |
36 | fd = shm_open(SHMNAME, OPEN_FLAG, OPEN_MODE); |
37 | if (-1 == (ret = fd)) |
38 | { |
39 | perror ( "shm failed: " ); |
40 | goto _OUT; |
41 | } |
42 | |
43 | //调整确定文件共享内存的空间 |
44 | ret = ftruncate(fd, FILE_SIZE); |
45 | if (-1 == ret) |
46 | { |
47 | perror ( "ftruncate faile: " ); |
48 | goto _OUT; |
49 | } |
50 | |
51 | //映射目标文件的存储区 |
52 | add_r = mmap(NULL, FILE_SIZE, PROT_READ, MAP_SHARED, fd, SEEK_SET); |
53 | if (NULL == add_r) |
54 | { |
55 | perror ( "mmap add_r failed: " ); |
56 | goto _OUT; |
57 | } |
58 | |
59 | |
60 | |
61 | //memcpy 内存共享 写入内容 |
62 | memcpy (buf, add_r, sizeof (buf)); |
63 | |
64 | printf ( "buf = %s\n" , buf); |
65 | |
66 | //取消映射 |
67 | ret = munmap(add_r, FILE_SIZE); |
68 | if (-1 == ret) |
69 | { |
70 | perror ( "munmap add_r faile: " ); |
71 | goto _OUT; |
72 | } |
73 | //删除内存共享 |
74 | shm_unlink(SHMNAME); |
75 | if (-1 == ret) |
76 | { |
77 | perror ( "shm_unlink faile: " ); |
78 | goto _OUT; |
79 | } |
80 | |
81 | _OUT: |
82 | return ret; |
83 | } |
[文件] write.c ~ 1KB 下载(14)
01 | #include <sys/types.h> |
02 | #include <sys/stat.h> |
03 | #include <fcntl.h> |
04 | #include <stdio.h> |
05 | #include <sys/mman.h> |
06 | #include <string.h> |
07 | #include <errno.h> |
08 | #include <unistd.h> |
09 | |
10 | /* |
11 | int shm_open(const char *name, int oflag, mode_t mode); |
12 | //创建或打开一个共享内存,成功返回一个整数的文件描述符,错误返回-1。 |
13 | 1.name:共享内存区的名字; |
14 | 2.标志位;open的标志一样 |
15 | 3.权限位 |
16 | int shm_unlink(const char *name); |
17 | |
18 | 编译时要加库文件-lrt |
19 | */ |
20 | |
21 | #define SHMNAME "shm_ram" |
22 | #define OPEN_FLAG O_RDWR|O_CREAT |
23 | #define OPEN_MODE 00777 |
24 | #define FILE_SIZE 4096*4 |
25 | |
26 | |
27 | int main( void ) |
28 | { |
29 | int ret = -1; |
30 | int fd = -1; |
31 | |
32 | void * add_w = NULL; |
33 | |
34 | //创建或者打开一个共享内存 |
35 | fd = shm_open(SHMNAME, OPEN_FLAG, OPEN_MODE); |
36 | if (-1 == (ret = fd)) |
37 | { |
38 | perror ( "shm failed: " ); |
39 | goto _OUT; |
40 | } |
41 | |
42 | |
43 | //调整确定文件共享内存的空间 |
44 | ret = ftruncate(fd, FILE_SIZE); |
45 | if (-1 == ret) |
46 | { |
47 | perror ( "ftruncate faile: " ); |
48 | goto _OUT; |
49 | } |
50 | |
51 | //映射目标文件的存储区 |
52 | add_w = mmap(NULL, FILE_SIZE, PROT_WRITE, MAP_SHARED, fd, SEEK_SET); |
53 | if (NULL == add_w) |
54 | { |
55 | perror ( "mmap src failed: " ); |
56 | goto _OUT; |
57 | } |
58 | |
59 | |
60 | //memcpy 内存共享 写入内容 |
61 | memcpy (add_w, "howaylee" , sizeof ( "howaylee" )); |
62 | |
63 | //取消映射 |
64 | ret = munmap(add_w, FILE_SIZE); |
65 | if (-1 == ret) |
66 | { |
67 | perror ( "munmap add_w faile: " ); |
68 | goto _OUT; |
69 | } |
70 | //删除内存共享 |
71 | /*shm_unlink(SHMNAME); |
72 | if(-1 == ret) |
73 | { |
74 | perror("shm_unlink faile: "); |
75 | goto _OUT; |
76 | }*/ |
77 | |
78 | _OUT: |
79 | return ret; |
80 | } |