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取决于以下几种情况: 时间 ...
随机推荐
- 前端JS转图片为base64并压缩、调整尺寸脚本
image to base64 to blob //////////////////////////////////////////////////////////////////////////// ...
- 【PHP】Thinkphp 七牛云API对接
访问一个网站,图片的流量占的比例是非常高的!在你的服务器硬盘上,图片占的容量也是非常高的. 如果要搞一个图片非常多,用户量又很庞大的网站,那么,得花多少钱烧在服务器上? 这种时候,当然要用第三方图片存 ...
- Firebase Cloud Function 编写与部署
1.设置和初始化 Firebase SDK for Cloud Functions (1).Cloud Functions 运行的是 Node v6.14.0,因此需要安装nodejs: https: ...
- Mongodb内嵌对象关联查询
db.-10-30T00:00:00Z"),"$lt":ISODate("2018-10-30T23:59:00Z")}, "equip.$ ...
- Windows Server 2008 正式版下载汇总
windows 2008是微软推出的新一代服务器专用系统版本, 具有良好的用户体验以及应用程序,windows 2008大幅提升了web服务以及应用程序的性能, 让企业在提供和维护资源服务的时候更加得 ...
- 笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ...
- SOA:面向服务编程——竹子整理
.net中如webservice,wcf,webapi,均可作为服务层,单独部署,而界面UI则部署在另一台服务器上,所有的业务逻辑均在服务层的业务层中进行. 这样一来,我们的UI其实就可以不限制语言, ...
- Python入职面试,可能会被企业HR问到的问题,你准备好了吗
整理了一下这两次面试问的问题先说简单的: 1.是否了解互联网协议七层模型 2.简单说一下TCP协议 3.你写的项目里用户数据安全如何保证?(比如用户密码加密处理一下)开放式问题,回 ...
- Python接口测试之封装requests
首先安装requests库: pip install requests test_requests.py 首先在TestRequest类中封装get与post方法, import requests i ...
- NOS直传加速服务
本文来自网易云社区 作者:孙建良 最近团队在对存储系统做一些性能测试,期间遇到了不少问题,测试过程中得出的结果也没有很好的数据支撑,所以尝试了非常多的方法来对性能问题进行定位. 小王童鞋是挺厉害的,使 ...