linux内核情景分析之命名管道
管道是一种"无名","无形文件,只可以近亲进程使用,不可以再任意两个进程通信使用,所以只能实现"有名","有形"的文件来实现就可以克服其缺点,这里的有名:一个文件应该有文件名,使得任何进程可以通过文件名或者路径找到该文件,有形指的是文件的inode应该存在与磁盘或者其他文件熊截止上.使得任何进程可以再任何时间都可以建立联系.
命名管道的创建用mknod,当然也可以使用mknod函数创建命名管道.建立了这样的节点,进程就可以通过打开一个文件一样打开整个民命管道.
命名管道与匿名管道的不同之处仅仅在于打开的过程,分析如何通过open系统调用与filfo文件取得联系.进程在内核通过sys_open进入filp_open(),然后在open_namei()调用一个函数path_walk(),根据文件的路径在文件系统中找到代表这文件的inode,将磁盘的inode读取内存,根据文件类型,将inode的i_op和i_fop指针设置为对应的inode_operation与file_operation,但对于FIFO这样特殊的文件则用调用init_special_inode加以初始化,可见文件类型不属于ACL或者数据,不是普通文件,不是目录,不是符号连接,就属于FIFO文件
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;
}
} else
init_special_inode(inode, inode->i_mode,
le32_to_cpu(raw_inode->i_block[0]));
接下来查看init_special_inode
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_fops
inode->i_fop = &def_fifo_fops;
else if (S_ISSOCK(mode))
inode->i_fop = &bad_sock_fops;
else
printk(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 */
};
sys_open->filp_open->dentry_open->fifo_open()
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;
}
注意FIFO文件的inode节点存在磁盘,但那只是一个节点,而文件的数据之存在内存缓冲区.与普通管道一样