I have two process, write and read. The two process build the shared memory with mm_create, and parameter of file point the same file. but the return point MM point the different address.
#Attached
read.c
on 2012-10-24 08:32:57//gcc write.c -o write -g #include <stdio.h> #include <string.h> #include "/usr/local/include/mm.h" struct shm{ int a; char b; float c; }*pt; int main() { size_t size_mem; MM *p_mm; size_mem = 1024; int a; char b; float c; p_mm = mm_create(size_mem,"../mm"); if(p_mm == NULL) { perror("error"); return 0; } pt = (struct shm*)p_mm; printf("addr_p_mm =%x\n",p_mm); printf("addr_pt =%x\n",pt); while(1) { mm_lock(p_mm,MM_LOCK_RW); a = pt->a; b = pt->b; c = pt->c; mm_unlock(p_mm); printf("a = %d,b = %d,c = %f \n",a,b,c); sleep(5); } return 0; }
#Attached
write.c
on 2012-10-24 08:33:12//gcc write.c -o write -g #include <stdio.h> #include <string.h> #include "mm.h" struct shm{ int a; char b; float c; }*pt; int main() { size_t size_mem; MM* p_mm; size_mem = 1024; p_mm = mm_create(size_mem,"../mm"); if(p_mm == NULL) { perror("error"); return 0; } mm_reset(p_mm); pt = (struct shm*)p_mm; printf("addr_p_mm =%x\n",p_mm); printf("addr_pt =%x\n",pt); while(1) { mm_lock(p_mm,MM_LOCK_RW); pt->a = 1; pt->b = 2; pt->c = 3; mm_unlock(p_mm); sleep(4); } mm_destroy(p_mm); return 0; }
#Attached
makefile
on 2012-10-24 08:33:27CC=cc CFLAGS=-O '-I/usr/local/include' LDFLAGS='-L/usr/local/lib' LIBS=-lm '-lmm' all: read read: read.o $(CC) $(LDFLAGS) -o read read.o $(LIBS) read.o: read.c $(CC) $(CFLAGS) -c read.c