Linux-进程间通信和同步(3)-共享内存

1.创建共享内存函数 shmget()

#include<sys/ipc.h>
#include<sys/shm.h>
int shmget(key_t key,size_t size,int shmflg);

shmflg :

  IPC_CREAT : 如果内核中不存在这样的信号量,则把他创建出来       

  IPC_EXCL : 与 IPC_CREAT 一起使用时,如果信号量已经存在,则操作将失败,并返回 -1


2.获得共享内存地址函数 shmat()

void *shmat(int shmid, const void *shmaddr, int shmflg);

若果 shmaddr 参数值等于 0 ,内核将试着查找一个未映射的区域。用户可以指定一个地址,但通常该地址只用于访问所拥有的硬件资源

3.删除共享内存函数 shmdt()

int shmdt(const void *shmaddr); 
  使用该函数并未真正删除该内存段,只将该内存段的应用计数-1, 当该内存段的引用计数减为 0 时,内核才真正从删除该内存段。

4.共享内存控制函数 shmctl()

int shmctl(int shmid, int cmd,struct shmid_ds *buf);



猜你喜欢

转载自blog.csdn.net/bobbymly/article/details/79334713
今日推荐