日期:2014-05-16 浏览次数:20649 次
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <assert.h> #include <stdio.h> int main(int argc, char *argv[]) { key_t shmkey; int shmid; char *addr; size_t size; int rc; size = 1024 * 1024; shmkey = ftok(argv[0], 1); shmid = shmget(shmkey, size, IPC_CREAT | 0666); printf("shmid=%d\n", shmid); addr = shmat(shmid, (void *)0x0700000000000000, SHM_RND); printf("addr=%p\n", addr); assert(0 != addr); memset(addr, 0, size); rc = shmdt(addr); printf("rc=%d\n", rc); rc = shmctl(shmid, IPC_RMID, 0); assert(0 == rc); return 0; }