linux内核情景分析之命名管道
管道是一种"无名","无形文件,只可以近亲进程使用,不可以再任意两个进程通信使用,所以只能实现"有名","有形"的文件来实现就可以克服其缺点,这里的有名:一个文件应该有文件名,使得任何进程可以通过文件名或者路径找到该文件,有形指的是文件的inode应该存在与磁盘或者其他文件熊截止上.使得任何进程可以再任何时间都可以建立联系.
if (inode->i_ino == EXT2_ACL_IDX_INO ||inode->i_ino == EXT2_ACL_DATA_INO)/* Nothing to do */ ;else if (S_ISREG(inode->i_mode)) {inode->i_op = &ext2_file_inode_operations;inode->i_fop = &ext2_file_operations;inode->i_mapping->a_ops = &ext2_aops;} else if (S_ISDIR(inode->i_mode)) {inode->i_op = &ext2_dir_inode_operations;inode->i_fop = &ext2_dir_operations;} else if (S_ISLNK(inode->i_mode)) {if (!inode->i_blocks)inode->i_op = &ext2_fast_symlink_inode_operations;else {inode->i_op = &page_symlink_inode_operations;inode->i_mapping->a_ops = &ext2_aops;}} elseinit_special_inode(inode, inode->i_mode,le32_to_cpu(raw_inode->i_block[0]));
void init_special_inode(struct inode *inode, umode_t mode, int rdev){inode->i_mode = mode;if (S_ISCHR(mode)) {inode->i_fop = &def_chr_fops;inode->i_rdev = to_kdev_t(rdev);} else if (S_ISBLK(mode)) {inode->i_fop = &def_blk_fops;inode->i_rdev = to_kdev_t(rdev);inode->i_bdev = bdget(rdev);} else if (S_ISFIFO(mode))//是fifo文件,设置文件操作为def_fifo_fopsinode->i_fop = &def_fifo_fops;else if (S_ISSOCK(mode))inode->i_fop = &bad_sock_fops;elseprintk(KERN_DEBUG "init_special_inode: bogus imode (%o)\n", mode);}
/** Dummy default file-operations: the only thing this does* is contain the open that then fills in the correct operations* depending on the access mode of the file...*/struct file_operations def_fifo_fops = {open: fifo_open, /* will set read or write pipe_fops */};
static int fifo_open(struct inode *inode, struct file *filp){int ret;ret = -ERESTARTSYS;lock_kernel();if (down_interruptible(PIPE_SEM(*inode)))goto err_nolock_nocleanup;if (!inode->i_pipe) {//第一次打开fifo文件,该管道的缓冲页面没分配,以后打开会跳过ret = -ENOMEM;if(!pipe_new(inode))goto err_nocleanup;}filp->f_version = 0;switch (filp->f_mode) {case 1:/* //只读* O_RDONLY* POSIX.1 says that O_NONBLOCK means return with the FIFO* opened, even when there is no process writing the FIFO.*/filp->f_op = &read_fifo_fops;PIPE_RCOUNTER(*inode)++;//读端的记录++if (PIPE_READERS(*inode)++ == 0)//表示刚打开读端,很可能有写进程睡眠,那就要唤醒写进程wake_up_partner(inode);if (!PIPE_WRITERS(*inode)) {//如果不存在写端if ((filp->f_flags & O_NONBLOCK)) {//并且不阻塞/* suppress POLLHUP until we have* seen a writer */filp->f_version = PIPE_WCOUNTER(*inode);//写端计数} else //不存在写端并且阻塞,那就之产生一般,需要睡眠,等待生产者进程将其唤醒{wait_for_partner(inode, &PIPE_WCOUNTER(*inode));if(signal_pending(current))goto err_rd;}}break;case 2:/*只写* O_WRONLY* POSIX.1 says that O_NONBLOCK means return -1 with* errno=ENXIO when there is no process reading the FIFO.*///不存在读端并且设置了不阻塞,直接return错误ret = -ENXIO;if ((filp->f_flags & O_NONBLOCK) && !PIPE_READERS(*inode))goto err;filp->f_op = &write_fifo_fops;//设置专用写操作PIPE_WCOUNTER(*inode)++;if (!PIPE_WRITERS(*inode)++)//如果写端第一次创建,很可能有读进程在睡眠wake_up_partner(inode);//将其唤醒if (!PIPE_READERS(*inode)) {//如果不存在读端并且阻塞wait_for_partner(inode, &PIPE_RCOUNTER(*inode));//休眠等待读进程将其唤醒if (signal_pending(current))goto err_wr;}break;case 3:/*可读可写* O_RDWR* POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.* This implementation will NEVER block on a O_RDWR open, since* the process can at least talk to itself.*///相当于一个进程同时打开命名管道的两端,所以不需要等待,只要任何一端是第一次打开,就唤醒在睡眠的进程filp->f_op = &rdwr_fifo_fops;PIPE_READERS(*inode)++;PIPE_WRITERS(*inode)++;PIPE_RCOUNTER(*inode)++;PIPE_WCOUNTER(*inode)++;if (PIPE_READERS(*inode) == 1 || PIPE_WRITERS(*inode) == 1)wake_up_partner(inode);break;default:ret = -EINVAL;goto err;}/* Ok! */up(PIPE_SEM(*inode));unlock_kernel();return 0;err_rd:if (!--PIPE_READERS(*inode))wake_up_interruptible(PIPE_WAIT(*inode));ret = -ERESTARTSYS;goto err;err_wr:if (!--PIPE_WRITERS(*inode))wake_up_interruptible(PIPE_WAIT(*inode));ret = -ERESTARTSYS;goto err;err:if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {struct pipe_inode_info *info = inode->i_pipe;inode->i_pipe = NULL;free_page((unsigned long)info->base);kfree(info);}err_nocleanup:up(PIPE_SEM(*inode));err_nolock_nocleanup:unlock_kernel();return ret;}
linux内核情景分析之命名管道的更多相关文章
- linux内核情景分析之匿名管道
管道的机制由pipe()创建,由pipe()所建立的管道两端都在同一进程.所以必须在fork的配合下,才可以在具有亲缘关系的进程通信 /* * sys_pipe() is the normal C c ...
- linux内核情景分析之execve()
用来描述用户态的cpu寄存器在内核栈中保存情况.可以获取用户空间的信息 struct pt_regs { long ebx; //可执行文件路径的指针(regs.ebx中 long ecx; //命令 ...
- Linux内核情景分析之消息队列
早期的Unix通信只有管道与信号,管道的缺点: 所载送的信息是无格式的字节流,不知道分界线在哪,也没通信规范,另外缺乏控制手段,比如保温优先级,管道机制的大小只有1页,管道很容易写满而读取没有及时,发 ...
- Linux内核情景分析的alloc_pages
NUMA结构的alloc_pages ==================== mm/numa.c 43 43 ==================== 43 #ifdef CONFIG_DISCON ...
- Linux内核情景分析之异常访问,用户堆栈的扩展
情景假设: 在堆内存中申请了一块内存,然后释放掉该内存,然后再去访问这块内存.也就是所说的野指针访问. 当cpu产生页面错误时,会把失败的线性地址放在cr2寄存器.线性地址缺页异常的4种情况 1.如果 ...
- linux内核情景分析之exit与Wait
//第一层系统调用 asmlinkage long sys_exit(int error_code) { do_exit((error_code&0xff)<<8); } 其主体是 ...
- linux内核情景分析之内核中的互斥操作
信号量机制: struct sempahore是其结构,定义如下 struct semaphore { atomic_t count;//资源数目 int sleepers;//等待进程数目 wait ...
- linux内核情景分析之信号实现
信号在进程间通信是异步的,每个进程的task_struct结构有一个sig指针,指向一个signal_struct结构 定义如下 struct signal_struct { atomic_t cou ...
- linux内核情景分析之强制性调度
从系统调用返回到用户空间是否调度,从ret_with_reschedule可看出,是否真正调度,取决于当前进程的pcb中的need_resched是否设置为1,那如何设置为1取决于以下几种情况: 时间 ...
随机推荐
- php 获取 今天、昨天、这周、上周、这月、上月、近30天
<?php //今天 $today = date("Y-m-d"); //昨天 $yesterday = date("Y-m-d", strtotime( ...
- 模块之 logging模块 time 模块 random模块 sys模块 pickle模块
1.如果执行文件不在项目根目录下,需要添加项目根目录到sys.path中2.调用业务逻辑 2.logging模块 程序日志是 什么时间发生了什么事情,以及当时的情况 不是logging的话 记录日志的 ...
- 内存管理小结(2)--伙伴系统API
伙伴系统分配内存以2的整数幂次的页数为单位.提供的API主要分为分配类与释放类. 1.分配类 1.1unsigned long __get_free_pages(gfp_t gfp_mask, uns ...
- A1042 Shuffling Machine (20)
1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...
- POJ:2492-Bug's Life(二分图的判定)
Bug's Life Time Limit: 10000MS Memory Limit: 65536K Description Background Professor Hopper is resea ...
- PAT Basic 1084
1084 外观数列 外观数列是指具有以下特点的整数序列: d, d1, d111, d113, d11231, d112213111, ... 它从不等于 1 的数字 d 开始,序列的第 n+1 项是 ...
- Spark性能优化:开发调优篇
1.前言 在大数据计算领域,Spark已经成为了越来越流行.越来越受欢迎的计算平台之一.Spark的功能涵盖了大数据领域的离线批处理.SQL类处理.流式/实时计算.机器学习.图计算等各种不同类型的计算 ...
- [原]sencha touch之carousel
carousel组件是个非常不错的东东,自带可滑动的效果,效果如下图 上面部分可以左右滑动,下面部分可以上下滑动,效果还是不错的,app程序中很有用的布局 代码如下: Ext.application( ...
- 动态加载css,js
function dynamicLoadCss(url) { var head = document.getElementsByTagName('head')[0]; var link = docum ...
- mysql练习题练习
1.数据库是按照原文制作的,表格结构一样具体存储的数据有些差异 原文地址:MySQL练习题 原答案地址:MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: selec ...