shmid:shmget函数返回的共享存储标识符
flag:决定以什么样的方式来确定映射的地址(通常为0)ipcrm -m shmid 删除编号为shmid的共享内存片
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdlib.h> #include <stdio.h> int main(int argc,char * argv[]) { int shm_size = 66535; int shm_id = 0; void *shared_memory=(void *)0; shm_id=shmget(IPC_PRIVATE, shm_size, 0666 ) ; if(shm_id < 0){ //共享内存申请失败 fprintf(stderr,"shmget failure!"); return -1; } fprintf(stderr,"共享内存id: %d\n",shm_id); system( "ipcs -m"); /*映射共享内存*/ shared_memory=shmat(shm_id,(void *)0,0); system("ipcs -m"); /*删除映射共享内存*/ if(shmdt(shared_memory) == -1) { fprintf(stderr,"shmdt failed\n"); return -1; } system("ipcs -m"); /*删除共享内存*/ char str[1024]; sprintf(str,"ipcrm -m %d",shm_id); system(str); system("ipcs -m"); return 0; }