日期:2014-05-16 浏览次数:20948 次
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ipc.h> #include <sys/shm.h> typedef struct test { char a[256]; char *b; char c[256]; } test; #define SHM_CREATE 1 #define SHM_LOAD 0 #define SHM_FILE "/var/test.shm" #define SHM_SIZE 8000 char * init_shm (const int flag, char *pathname, const unsigned long size, int shmid) { char *ret = NULL; int shm_id = 0; key_t key = ftok (pathname, 'R'); if (SHM_CREATE == flag) // 新建 { shm_id = shmget (key, size, IPC_CREAT | IPC_EXCL | 0600); if (-1 == shm_id) { shm_id = shmget (key, size, 0); if (-1 == shm_id) { perror ("init_shm shmget create"); return NULL; } } printf("shm_id = %d\n", shm_id); if ((ret = shmat (shm_id, (char *) 0xb7f17000, 0)) == (void *)-1) { perror("shmat"); exit(1); } memset (ret, 0, size); } else {/* shm_id = shmget (key, size, 0); if (shm_id == -1) { perror ("init_shm shmget load"); return NULL; } */ ret = shmat (shmid, (char *)0xb7f17000, 0); } return ret; } int main (int argc, char **argv) { int shmid; char *arg = argv[1]; if (argc != 3) { printf("Usage: %s cmd shmid\n", argv[0]); exit(1); } if (*arg == 'c') { system ("touch " SHM_FILE); // create shm file printf("SHM_FILE' size = %d\n", SHM_SIZE); test *t = (struct test *) init_shm (SHM_CREATE, SHM_FILE, SHM_SIZE, shmid); t->b = t->c; if (t) { printf("t->a size = %d\n", sizeof(t->a)); //t->b =(char *) t + sizeof(t->a); printf ("addr = 0x%08x/0x%08x\n", (unsigned int)t->b, (unsigned int)t); strcpy (t->a, "AAAA"); strcpy (t->b, "111"); printf ("a=%s,b=%s\n", t->a, t->b); } } else if (*arg == 'l') { shmid = atoi(argv[2]); test *t = (struct test *) init_shm (SHM_LOAD, SHM_FILE, SHM_SIZE, shmid); printf ("addr = 0x%08x/0x%08x\n",(unsigned int)t->b, (unsigned int)t); //t->b = (char *)t + sizeof(struct test); // 重新定位b的位置 if (t) { printf ("a=%s,b=%s\n", t->a, t->b); } } return 0; }