内存共享主要分为三个步骤:
1、创建内存共享id。
shmid = shmget((key_t)1234,sizeof(struct shared_use_st),0666 | IPC_CREAT);
2、内存连接,申请好的内存区域连接到本地程序。
shared_memory = shmat(shmid,NULL,NULL);
3、内存使用完毕进行删除,释放资源。
if(shmdt(shared_memory) == -1){ fprintf(stderr,"shmdt failed "); exit(EXIT_FAILURE); } if(shmctl(shmid, IPC_RMID, 0) == -1) { fprintf(stderr,"shmctl IPC_RMID failed! "); exit(1); }