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)