POSIX共享内存不需要自己手动挂载,只要打开成功,就会自动挂载.一般挂载在 /dev/shm 目录下
cd /dev/shm
od -c xyz注意:
ERRORS
EACCES A file descriptor refers to a non-regular file. Or MAP_PRIVATE
was requested, but fd is not open for reading. Or MAP_SHARED
was requested and PROT_WRITE is set, but fd is not open in
read/write (O_RDWR) mode. Or PROT_WRITE is set, but the file is
append-only.
shm_open.c
#include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #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 err"); printf("shm_open success\n"); if( ftruncate(shmid,sizeof(STU)) == -1 ) //修改共享内存对象大小 ERR_EXIT("ftruncate"); struct stat buf; if(fstat(shmid,&buf) == -1) /// get file status ERR_EXIT("fstat err"); printf("size=%ld mode=%o\n",buf.st_size,buf.st_mode & 0777); close(shmid); // 仅仅需要一个close就可以关闭 return 0; }
shm_unlink.c
#include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #define ERR_EXIT(M) do { perror(M); exit(EXIT_FAILURE); }while(0); int main(void) { shm_unlink("/xyz"); return 0; }
#include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #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_RDWR,0); // 这里必须是O_RDWR才能映射成功 if(shmid == -1) ERR_EXIT("shm_open err"); printf("shm_open succ\n"); struct stat buf; if(fstat(shmid,&buf) == -1) ERR_EXIT("fstat err"); STU *p; //将共享内存对象映射到进程地址空间. p = mmap(NULL,buf.st_size,PROT_WRITE,MAP_SHARED,shmid,0); if(p == MAP_FAILED) ERR_EXIT("mmap err"); strcpy(p->name,"cjl"); // 根据映射的地址,写入数据 p->age = 20; close(shmid); // 仅仅需要一个close就可以关闭 return 0; }
#include<stdio.h> #include<stdlib.h> #include<sys/ipc.h> #include<sys/types.h> #include<unistd.h> #include<errno.h> #include<fcntl.h> #include<sys/stat.h> #include<sys/mman.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"); struct stat buf; if (fstat(shmid, &buf) == -1) ERR_EXIT("fstat"); printf("size=%ld, mode=%o\n", buf.st_size, buf.st_mode & 0777); STU *p; p = (STU *)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; }
.PHONY:clean all CC=gcc CFLAGS=-Wall -g BIN=shm_open shm_unlink shm_write shm_read all:$(BIN) %.o:%.c $(CC) $(CFLAGS) -c $< -o $@ shm_open:shm_open.o $(CC) $(CFLAGS) $^ -o $@ -lrt shm_unlink:shm_unlink.o $(CC) $(CFLAGS) $^ -o $@ -lrt shm_write:shm_write.o $(CC) $(CFLAGS) $^ -o $@ -lrt shm_read:shm_read.o $(CC) $(CFLAGS) $^ -o $@ -lrt clean: rm -f *.o $(BIN)
原文地址:http://blog.csdn.net/u014304293/article/details/46386057