POSIX 共享内存
来源:互联网 发布:mac osx 10.12 编辑:程序博客网 时间:2024/05/18 11:24
POSIX共享内存相关函数:
shm_open函数
功能:用来创建或打开一个共享内存对象
原型
int shm_open(const char *name, int oflag, mode_t mode);
参数
name:共享内存对象的名字
oflag:与open函数类似,可以是O_RDONLY、O_RDWR,还可以按位或上O_CREAT、O_EXCL、O_TRUNC等。
mode:此参数总是需要设置,如果oflag没有指定了O_CREAT,可以指定为0
返回值:成功返回非负整数文件描述符;失败返回-1
修改共享内存大小ftruncate
功能:修改共享内存对象大小
原型
int ftruncate(int fd, off_t length);
参数
fd: 文件描述符
length:长度
返回值:成功返回0;失败返回-1
获取共享内存对象信息
功能:获取共享内存对象信息
原型
int fstat(int fd, struct stat *buf);
参数
fd: 文件描述符
buf:返回共享内存状态
返回值:成功返回0;失败返回-1
shm_unlink函数
功能:删除一个共享内存对象
原型
int shm_unlink(const char *name);
参数
name: 共享内存对象的名字
返回值:成功返回0;失败返回-1
共享内存对象的映射
功能:将共享内存对象映射到进程地址空间。
原型
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
参数
addr: 要映射的起始地址,通常指定为NULL,让内核自动选择
len:映射到进程地址空间的字节数
prot:映射区保护方式
flags:标志
fd:文件描述符
offset:从文件头开始的偏移量
返回值:成功返回映射到的内存区的起始地址;失败返回-1
01shm_open.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
int shmid;
shmid = shm_open("/xyz", O_CREAT | O_RDWR, 0666);
if (shmid == -1)
ERR_EXIT("shm_open");
printf("shm_open succ\n");
if (ftruncate(shmid, sizeof(STU)) == -1)
ERR_EXIT("ftruncate");
struct stat buf;
if (fstat(shmid, &buf) == -1)
ERR_EXIT("fstat");
printf("size=%ld mode=%o\n", buf.st_size, buf.st_mode & 07777);
close(shmid);
return 0;
}
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
int shmid;
shmid = shm_open("/xyz", O_CREAT | O_RDWR, 0666);
if (shmid == -1)
ERR_EXIT("shm_open");
printf("shm_open succ\n");
if (ftruncate(shmid, sizeof(STU)) == -1)
ERR_EXIT("ftruncate");
struct stat buf;
if (fstat(shmid, &buf) == -1)
ERR_EXIT("fstat");
printf("size=%ld mode=%o\n", buf.st_size, buf.st_mode & 07777);
close(shmid);
return 0;
}
02shm_unlink.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
shm_unlink("/xyz");
return 0;
}
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
shm_unlink("/xyz");
return 0;
}
03shm_write.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
int shmid;
shmid = shm_open("/xyz", O_RDWR, 0);
if (shmid == -1)
ERR_EXIT("shm_open");
printf("shm_open succ\n");
struct stat buf;
if (fstat(shmid, &buf) == -1)
ERR_EXIT("fstat");
STU *p;
p = mmap(NULL, buf.st_size, PROT_WRITE, MAP_SHARED, shmid, 0);
if (p == MAP_FAILED)
ERR_EXIT("mmap");
strcpy(p->name, "test");
p->age = 20;
close(shmid);
return 0;
}
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
int shmid;
shmid = shm_open("/xyz", O_RDWR, 0);
if (shmid == -1)
ERR_EXIT("shm_open");
printf("shm_open succ\n");
struct stat buf;
if (fstat(shmid, &buf) == -1)
ERR_EXIT("fstat");
STU *p;
p = mmap(NULL, buf.st_size, PROT_WRITE, MAP_SHARED, shmid, 0);
if (p == MAP_FAILED)
ERR_EXIT("mmap");
strcpy(p->name, "test");
p->age = 20;
close(shmid);
return 0;
}
04shm_read.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
int shmid;
shmid = shm_open("/xyz", O_RDONLY, 0);
if (shmid == -1)
ERR_EXIT("shm_open");
printf("shm_open succ\n");
struct stat buf;
if (fstat(shmid, &buf) == -1)
ERR_EXIT("fstat");
STU *p;
p = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, shmid, 0);
if (p == MAP_FAILED)
ERR_EXIT("mmap");
printf("name=%s age=%d\n", p->name, p->age);
close(shmid);
return 0;
}
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct stu
{
char name[32];
int age;
} STU;
int main(void)
{
int shmid;
shmid = shm_open("/xyz", O_RDONLY, 0);
if (shmid == -1)
ERR_EXIT("shm_open");
printf("shm_open succ\n");
struct stat buf;
if (fstat(shmid, &buf) == -1)
ERR_EXIT("fstat");
STU *p;
p = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, shmid, 0);
if (p == MAP_FAILED)
ERR_EXIT("mmap");
printf("name=%s age=%d\n", p->name, p->age);
close(shmid);
return 0;
}
makefile:
.PHONY:clean all
CC=gcc
CFLAGS=-Wall -g
BIN=01shm_open 02shm_unlink 03shm_write 04shm_read
all:$(BIN)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
01shm_open:01shm_open.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
02shm_unlink:02shm_unlink.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
03shm_write:03shm_write.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
04shm_read:04shm_read.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
clean:
rm -f *.o $(BIN)
CC=gcc
CFLAGS=-Wall -g
BIN=01shm_open 02shm_unlink 03shm_write 04shm_read
all:$(BIN)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
01shm_open:01shm_open.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
02shm_unlink:02shm_unlink.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
03shm_write:03shm_write.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
04shm_read:04shm_read.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
clean:
rm -f *.o $(BIN)
0 0
- Posix多线程-共享内存
- posix 基于共享内存
- posix 共享内存
- POSIX 共享内存
- Posix共享内存区
- POSIX共享内存
- 关于Posix共享内存
- Posix共享内存区
- IPC通信:Posix共享内存
- Posix共享内存区基础知识
- POSIX共享内存(内存映射)
- Posix多线程编程(6)—共享内存
- IPC通信:Posix共享内存1
- IPC通信:Posix共享内存2
- posix 基于文件的共享内存
- IPC通信:Posix共享内存2
- IPC通信:Posix共享内存1
- Posix共享内存区的基本操作
- Fedora20连接Android真机调试
- 元编程(c++)
- linux下搭建SVN服务器完全手册
- 利用UIImageJPEGRepresentation与UIGraphicsBeginImageContext进行图片压缩的简单比较
- SpringMVC上传下载,页面不刷新提交带附件的form表单
- POSIX 共享内存
- 2C03-View-Layout-Animation-list-Reverse-Order
- Extjs4 动态生成图表chart(从后台传数据动态显示)
- MediaCoder字幕乱码问题
- Wing IDE pro 5.0.0-1版本破解方法(仅供测试,请使用正版软件)
- 基础控件的使用
- jQuery开始
- css3-rotate实现超炫环形旋转特效
- Java 字符串算术表达式求值