概述
本系列主要根据内核代码,分析 消息队列 共享内存 信号量 三种 进程间通信的方法。
内核 目录下有个ipc文件夹, 实现代码在这个里面
linux/ipc$ ls
built-in.a ipc_sysctl.c modules.order mqueue.c msgutil.c sem.c syscall.c util.h
compat.c Makefile mq_sysctl.c msg.c namespace.c shm.c util.c
ipc_sysctl.c
static struct ctl_table ipc_kern_table[] = {
{
.procname = "shmmax", # 共享内存最多的条数
.data = &init_ipc_ns.shm_ctlmax,
.maxlen = sizeof(init_ipc_ns.shm_ctlmax),
.mode = 0644,
.proc_handler = proc_ipc_doulongvec_minmax,
},
device_initcall(ipc_sysctl_init);
这个文件主要是 系统启动的时候调用 device_initcall 这篇文章介绍的不错 device_initcall
register_sysctl_table(ipc_root_table); 这里会去注册ipc_kern_table
系统启动的时候会注册ipc 的配置, 初始化一些ipc的权限,权限,ipc最多的条数,超过的时候的处理函数。
syscall.c
/*定义系统调用 ipc 六个参数*/
SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second,
unsigned long, third, void __user *, ptr, long, fifth)
{
int version, ret;
version = call >> 16; /* hack for backward compatibility */
call &= 0xffff; /* 解析出系统调用的类型*/
switch (call) {
case SEMOP:
return ksys_semtimedop(first, (struct sembuf __user *)ptr,
second, NULL);
case SEMTIMEDOP:
if (IS_ENABLED(CONFIG_64BIT) || !IS_ENABLED(CONFIG_64BIT_TIME))
return ksys_semtimedop(first, ptr, second,
(const struct __kernel_timespec __user *)fifth);
else if (IS_ENABLED(CONFIG_COMPAT_32BIT_TIME))
return compat_ksys_semtimedop(first, ptr, second,
(const struct old_timespec32 __user *)fifth);
else
return -ENOSYS;
case SEMGET:
return ksys_semget(first, second, third);
case SEMCTL: {
unsigned long arg;
if (!ptr)
return -EINVAL;
if (get_user(arg, (unsigned long __user *) ptr))
return -EFAULT;
return ksys_semctl(first, second, third, arg);
}
case MSGSND:
return ksys_msgsnd(first, (struct msgbuf __user *) ptr,
second, third);
case MSGRCV:
switch (version) {
case 0: {
struct ipc_kludge tmp;
if (!ptr)
return -EINVAL;
if (copy_from_user(&tmp,
(struct ipc_kludge __user *) ptr,
sizeof(tmp)))
return -EFAULT;
return ksys_msgrcv(first, tmp.msgp, second,
tmp.msgtyp, third);
}
default:
return ksys_msgrcv(first,
(struct msgbuf __user *) ptr,
second, fifth, third);
}
/**/
case MSGGET:
return ksys_msgget((key_t) first, second);
case MSGCTL:
return ksys_msgctl(first, second,
(struct msqid_ds __user *)ptr);
case SHMAT:
switch (version) {
default: {
unsigned long raddr;
ret = do_shmat(first, (char __user *)ptr,
second, &raddr, SHMLBA);
if (ret)
return ret;
return put_user(raddr, (unsigned long __user *) third);
}
case 1:
/*
* This was the entry point for kernel-originating calls
* from iBCS2 in 2.2 days.
*/
return -EINVAL;
}
case SHMDT:
return ksys_shmdt((char __user *)ptr);
case SHMGET:
return ksys_shmget(first, second, third);
case SHMCTL:
return ksys_shmctl(first, second,
(struct shmid_ds __user *) ptr);
default:
return -ENOSYS;
}
}
msg.c
long ksys_msgget(key_t key, int msgflg)
{
struct ipc_namespace *ns;
static const struct ipc_ops msg_ops = {
.getnew = newque,
.associate = security_msg_queue_associate,
};
struct ipc_params msg_params;
ns = current->nsproxy->ipc_ns; /* 获取当前的 namespace */
msg_params.key = key;
msg_params.flg = msgflg;
return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
}
ipcget ------> ipcget_public ---->
struct kern_ipc_perm *ipcp;
int flg = params->flg;
int err;
/*
* Take the lock as a writer since we are potentially going to add
* a new entry + read locks are not "upgradable"
*/
ipcp = ipc_findkey(ids, params->key); /*查看ipc id 是否已经被使用*/
if (ipcp == NULL) {
/* key not used */
if (!(flg & IPC_CREAT))
err = -ENOENT;
else
err = ops->getnew(ns, params); /* 创建一个新的msg id*/
/**
* newque - Create a new msg queue
* @ns: namespace
* @params: ptr to the structure that contains the key and msgflg
*
* Called with msg_ids.rwsem held (writer)
*/
static int newque(struct ipc_namespace *ns, struct ipc_params *params)
{
struct msg_queue *msq;
int retval;
key_t key = params->key;
int msgflg = params->flg;
msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
if (unlikely(!msq))
return -ENOMEM;
msq->q_perm.mode = msgflg & S_IRWXUGO;
msq->q_perm.key = key;
msq->q_perm.security = NULL;
retval = security_msg_queue_alloc(&msq->q_perm);
if (retval) {
kvfree(msq);
return retval;
}
msq->q_stime = msq->q_rtime = 0;
msq->q_ctime = ktime_get_real_seconds();
msq->q_cbytes = msq->q_qnum = 0;
msq->q_qbytes = ns->msg_ctlmnb;
msq->q_lspid = msq->q_lrpid = NULL;
INIT_LIST_HEAD(&msq->q_messages);
INIT_LIST_HEAD(&msq->q_receivers);
INIT_LIST_HEAD(&msq->q_senders);
/* ipc_addid() locks msq upon success. */
retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
if (retval < 0) {
ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
return retval;
}
ipc_unlock_object(&msq->q_perm);
rcu_read_unlock();
return msq->q_perm.id;
}
最后
以上就是负责奇迹为你收集整理的进程间通信 IPC 系列一 消息队列的全部内容,希望文章能够帮你解决进程间通信 IPC 系列一 消息队列所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复