linux 有名管道FIFO

本文详细介绍了Linux中的有名管道FIFO,它允许非亲子关系的进程通过文件名实现通信。在打开管道文件时,会触发def_fifo_fops中的open方法fifo_open,该方法包括获取管道互斥锁、创建管道、根据打开方式设置权限以及唤醒被阻塞的进程等步骤。此外,还讨论了读写操作、管道状态以及信号对进程的影响。
摘要由CSDN通过智能技术生成

linux文件主要有4种:普通文件、目录文件、设备文件(字符设备文件/块设备文件)、管道文件

 

管道文件(p表示管道文件)

[redhat@localhost tmp]$ mkfifo fifo
[redhat@localhost tmp]$ ll fifo
prw-rw-r--. 1 redhat redhat 0  7月  1 22:06 fifo

文件系统(ext4/ext3/nfs)中存在管道文件,所以就通过文件名称来标识管道文件及其关联的管道FIFO;所以有名管道就可以在非亲子关系的进程间实现通信,一个进程通过文件名打开管道的写端,另一个进程通过文件名打开管道的读端,两个进程就可以通过该FIFO实现通信。

 

I.def_fifo_fops
def_fifo_fops表示管道文件的操作集,定义如下:

/* fs/fifo.c */
148 /*
149  * Dummy default file-operations: the only thing this does
150  * is contain the open that then fills in the correct operations
151  * depending on the access mode of the file...
152  */
153 const struct file_operations def_fifo_fops = {
154         .open           = fifo_open,    /* will set read_ or write_pipefifo_fops */
155 };

当打开管道文件时会触发def_fifo_fops中的open方法fifo_open,代码跟踪如下:
文件方法集:
  open->do_sys_open->do_filp_open->path_lookup_open->do_path_lookup->path_walk->link_path_walk->__link_path_walk->do_lookup->real_lookup
  ext4为例:
  ext4_lookup->ext4_iget->init_special_inode->def_fifo_fops
open回调:
open->do_sys_open->do_filp_open->nameidata_to_filp->__dentry_open->fifo_open

 

II.fifo_open

 18 static void wait_for_partner(struct inode* inode, unsigned int *cnt)
 19 {
 20
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值