Linux的pipe为啥只能单向通信有没有方法突破这个限制

pipe实现
/* * sys_pipe() is the normal C calling standard for creating * a pipe. It\u0026#39;s not the way Unix traditionally does this, though. */asmlinkage int sys_pipe(unsigned long * fildes){\tint fd;//fd表示,一个读,一个写\tint error;\terror = do_pipe(fd);\tif (!error) {\t\tif (copy_to_user(fildes, fd, 2*sizeof(int)))//从内核态拷贝到用户态\t\t\terror = -EFAULT;\t}\treturn error;}int do_pipe(int *fd){\tstruct qstr this;\tchar name;//目录项名字\tstruct dentry *dentry;//目录结构\tstruct inode * inode;//inode对应一个管道,不过管道无实际存储,不在硬盘也不在文件系统\tstruct file *f1, *f2;//父子进程操作管道对应的文件对象\tint error;\tint i,j;\terror = -ENFILE;\tf1 = get_empty_filp();//获取一个file对象,因为管道在不同的进程,两端不可以共享一个file\tif (!f1)\t\tgoto no_files;\tf2 = get_empty_filp();//获取1个file对象,同上\tif (!f2)\t\tgoto close_f1;\tinode = get_pipe_inode();//获取一个inode用于表示管道这个无形文件,分配缓存,初始化\tif (!inode)\t\tgoto close_f12;\terror = get_unused_fd();//获取一个fd用于绑定file对象\tif (error \u0026lt; 0)\t\tgoto close_f12_inode;\ti = error;\terror = get_unused_fd();//同上\tif (error \u0026lt; 0)\t\tgoto close_f12_inode_i;\tj = error;\terror = -ENOMEM;\tsprintf(name, "", inode-\u0026gt;i_ino);//操作\tthis.name = name;\tthis.len = strlen(name);\tthis.hash = inode-\u0026gt;i_ino; /* will go */\tdentry = d_alloc(pipe_mnt-\u0026gt;mnt_sb-\u0026gt;s_root, \u0026amp;this);//分配一个目录,因为file无直接指向inode.file只能通过指向目录的指针指向目录项中转找到inode\tif (!dentry)\t\tgoto close_f12_inode_i_j;\tdentry-\u0026gt;d_op = \u0026amp;pipefs_dentry_operations;\td_add(dentry, inode);//将目录与inode挂钩\tf1-\u0026gt;f_vfsmnt = f2-\u0026gt;f_vfsmnt = mntget(mntget(pipe_mnt));//挂载到vfs\tf1-\u0026gt;f_dentry = f2-\u0026gt;f_dentry = dget(dentry);//设置file对象的指向目录指针\t/*设置只读属性, read file */\tf1-\u0026gt;f_pos = f2-\u0026gt;f_pos = 0;\tf1-\u0026gt;f_flags = O_RDONLY;//设置只读属性\tf1-\u0026gt;f_op = \u0026amp;read_pipe_fops;//设置f1的读管道指针操作\tf1-\u0026gt;f_mode = 1;\tf1-\u0026gt;f_version = 0;\t/* 写文件相关对象,只写数学,操作为write_pipe_fopswrite file */\tf2-\u0026gt;f_flags = O_WRONLY;//设置只可写属性\tf2-\u0026gt;f_op = \u0026amp;write_pipe_fops;//设置写操作\tf2-\u0026gt;f_mode = 2;\tf2-\u0026gt;f_version = 0;\tfd_install(i, f1);//fd与file对象绑定\tfd_install(j, f2);//fd与file对象绑定\tfd = i;\tfd = j;\treturn 0;close_f12_inode_i_j:\tput_unused_fd(j);close_f12_inode_i:\tput_unused_fd(i);close_f12_inode:\tfree_page((unsigned long) PIPE_BASE(*inode));\tkfree(inode-\u0026gt;i_pipe);\tinode-\u0026gt;i_pipe = NULL;\tiput(inode);close_f12:\tput_filp(f2);close_f1:\tput_filp(f1);no_files:\treturn error;\t}fd1绑定的read_pipe_fops的写操作为bad_pipe_w,如果fd1使用了写操作,那么将返回错误,fd2的write_pipe_fops一样.以下就不举例了
struct file_operations read_pipe_fops = {\tllseek:\t\tpipe_lseek,\tread:\t\tpipe_read,\twrite:\t\tbad_pipe_w,\tpoll:\t\tpipe_poll,\tioctl:\t\tpipe_ioctl,\topen:\t\tpipe_read_open,\trelease:\tpipe_read_release,};


推荐阅读