linux SysV IPC实现

这篇博客介绍了Linux系统中System V IPC(进程间通信)的实现,包括消息队列、共享内存和信号量的创建与获取。通过msgget、shmget和semget等API进行操作,讲解了如何使用IPC_PRIVATE关键字创建新的IPC资源,以及权限和ID的管理。同时,文章详细阐述了IPC共性的属性,如key、id、mode等,并解析了权限校验的过程。
摘要由CSDN通过智能技术生成

IPC(Interprocess Communication)表示进程间通信机制;System V IPC机制主要有消息队列、共享内存、信号量,linux中实现了SysV IPC。

 

I.SysV IPC创建/获取
消息队列、共享内存、信号量的创建/获取API原型如下:
int msgget(key_t key, int msgflg);
int shmget(key_t key, size_t size, int shmflg);
int semget(key_t key, int nsems, int semflg);

以上API主要用来创建新的IPC或根据key值查找IPC,并返回IPC id;内核实现均调用ipcget:

ipc/util.c:
734 /**
735  * ipcget - Common sys_*get() code
736  * @ns : namsepace
737  * @ids : IPC identifier set
738  * @ops : operations to be called on ipc object creation, permission checks
739  *        and further checks
740  * @params : the parameters needed by the previous operations.
741  *
742  * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
743  */
744 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
745                         struct ipc_ops *ops, struct ipc_params *params)
746 {
747         if (params->key == IPC_PRIVATE)
748                 return ipcget_new(ns, ids, ops, params);
749         else
750                 return ipcget_public(ns, ids, ops, params);
751 }


i.key
key类型为key_t,即int:

/usr/include/bits/types.h:101:#define	__S32_TYPE		int
/usr/include/bits/typesizes.h:55:#define __KEY_T_TYPE		__S32_TYPE
/usr/include/bits/types.h:155:__STD_TYPE __KEY_T_TYPE __key_t;	/* Type of an IPC key.  */
/usr/include/sys/ipc.h:48:typedef __key_t key_t;
SysV IPC均用key值作为主键,系统级标识出IPC;同时每一个IPC都有一个id与之对应。
key和id都能标识出IPC,区别主要是,key由用户程序提供,以便用户程序标识IPC实现进程间通信;id由系统返回,能快速查找到IPC并使用。
key查找IPC过程:遍历IPC id基数树的叶子结点,找出key对应的IPC。实现为ipc_findkey:
170 /**
171  *      ipc_findkey     -       find a key in an ipc identifier set     
172  *      @ids: Identifier set
173  *      @key: The key to find
174  *      
175  *      Requires ipc_ids.rw_mutex locked.
176  *      Returns the LOCKED pointer to the ipc structure if found or NULL
177  *      if not.
178  *      If key is found ipc points to the owning ipc structure
179  */
180 
181 static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
182 {
183         struct kern_ipc_perm *ipc;
184         int next_id;
185         int total;
186 
187         for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
188                 ipc = idr_find(&ids->ipcs_idr, next_id);
189 
190                 if (ipc == NULL)
191                         continue;
192 
193                 if (ipc->key != key) {
194                         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值