constchar g_test_data[][SHM_TEST_DATALEN] = { {"Nothing gold can stay"}, {"Nature's first green is gold"}, {"Her hardest hue to hold"}, {"Her early leaf's a flower"}, {"But only so an hour"}, {"Then leaf subsides leaf"}, {"So Eden sank to grief"}, {"So down gose down to day"}, {"Ending"} };
intshm_write(struct shm_test_st *pdata) { int num = sizeof(g_test_data) / sizeof(g_test_data[0]); int i; for (i = 0; i < num; i++) { strcpy(pdata->dat, g_test_data[i]); printf("Write - %s\n", g_test_data[i]); sleep(1); } printf("Write Over!\n"); return0; }
intshm_read(struct shm_test_st *pdata) { sleep(1); char tmp[SHM_TEST_DATALEN]; while (1) { strcpy(tmp, pdata->dat); int num = sizeof(g_test_data) / sizeof(g_test_data[0]); int ret = -1; int i; for (i = 0; i < num; i++) { if (strcmp(tmp, g_test_data[i]) == 0) { ret = 0; if (i == (num - 1)) { printf("Read Over\n"); return0; } } } if (ret < 0) { printf("Read Error - %s\n", tmp); } sleep(0); } return-1; }
intmain(int argc, char **argv) { if (argc < 2) { printf("demo w/r\n"); exit(1); } key_t ipckey = ftok("/tmp/test", ':'); int shmid = shmget(ipckey, sizeof(struct shm_test_st), 0600 | IPC_CREAT); if (shmid < 0) { printf("Error - %s\n", strerror(errno)); exit(1); } void *addr = NULL; if (argv[1][0] == 'w') { addr = shmat(shmid, NULL, 0); } else { // r addr = shmat(shmid, NULL, SHM_RDONLY); } if (addr == (void *)-1) { printf("Addr Error - %s\n", strerror(errno)); exit(1); } structshm_test_st *p_shared = (struct shm_test_st *)addr; int ret; if (argv[1][0] == 'w') { ret = shm_write(p_shared); } else { // r ret = shm_read(p_shared); } if (shmctl(shmid, IPC_RMID, NULL) < 0) { printf("Delete Error - %s\n", strerror(errno)); } if (ret < 0) { exit(1); } }
执行程序:
./demo w & ./demo r & ./demo r & ./demo r &
执行结果:可能产生读取信息错误,如下
Write - Nothing gold can stay
Write - Nature's first green is gold
Write - Her hardest hue to hold
Read Error - Nature's first gto hold
Write - Her early leaf's a flower
Write - But only so an hour
Write - Then leaf subsides leaf
Read Error - But only so an hes llower
Write - So Eden sank to grief
Write - So down gose down to day
Write - Ending
Read Over
Read Over
Read Over
Write Over!
intshm_write(struct shm_test_st *pdata, int semid) { structsembufsem[1]; sem[0].sem_num = 0; sem[0].sem_flg = SEM_UNDO; int num = sizeof(g_test_data) / sizeof(g_test_data[0]); int i; for (i = 0; i < num; i++) { sem[0].sem_op = 1; semop(semid, sem, 1); // p() printf("Write - %s\n", g_test_data[i]); strcpy(pdata->dat, g_test_data[i]); sem[0].sem_op = -1; semop(semid, sem, 1); // v() sleep(1); } printf("Write Over!\n"); return0; }
intshm_read(struct shm_test_st *pdata, int semid) { structsembufsem[1]; sem[0].sem_num = 0; sem[0].sem_flg = SEM_UNDO; sleep(1); char tmp[SHM_TEST_DATALEN]; while (1) { sem[0].sem_op = 0; semop(semid, sem, 1); // wait() strcpy(tmp, pdata->dat); int num = sizeof(g_test_data) / sizeof(g_test_data[0]); int ret = -1; int i; for (i = 0; i < num; i++) { if (strcmp(tmp, g_test_data[i]) == 0) { ret = 0; if (i == (num - 1)) { printf("Read Over\n"); return0; } } } if (ret < 0) { printf("Read Error - %s\n", tmp); } sleep(0); } return-1; }
执行程序:
./demo w & ./demo r & ./demo r & ./demo r &
执行结果:确实还是会产生错误
Write - Nothing gold can stay
Write - Nature's first green is gold
Read Error - Nothing gold canreen i
Write - Her hardest hue to hold
Write - Her early leaf's a flower
Write - But only so an hour
Write - Then leaf subsides leaf
Write - So Eden sank to grief
Write - So down gose down to day
Write - Ending
Read Over
Read Over
Read Over
Write Over!