简体   繁体   English

C-将标准输入/输出重定向到单个双向文件描述符

[英]C - Redirect Standard Input/Output to a single, bidirectional file descriptor

In certain contexts, such as when dealing with a serial device file, I am able to open a bidirectional file descriptor. 在某些情况下,例如处理串行设备文件时,我可以打开双向文件描述符。 However, STDIN/STDOUT are two separate, unidirectional files. 但是,STDIN / STDOUT是两个单独的单向文件。 Is it possible to create a bidirectional file descriptor from STDIN/STDOUT? 是否可以从STDIN / STDOUT创建双向文件描述符? I ask for interface symmetry purposes: when writing user functions that need to both read and write , it is inconvenient to need to specify a file descriptor for both directions (only) when dealing with standard pipes. 我问界面对称性的目的:编写需要在两个用户功能时readwrite ,这是不方便需要指定一个文件描述符的两个方向(只)标准管的时候。

There is a lot of discussion available about redirecting STDIN/STDOUT (for example using dup2 ), but this discussion is mostly concerned with piping input to/from subprocesses. 关于重定向STDIN / STDOUT(例如,使用dup2 ),存在很多讨论,但此讨论主要涉及到/来自子流程的管道输入。 I haven't found any information for combining two file descriptors into one. 我还没有找到任何有关将两个文件描述符合并为一个的信息。


EDIT: Here's the original context for the question. 编辑:这是该问题的原始上下文。 I don't care about solving this particular problem ; 我不在乎解决这个特殊问题 ; there are plenty of workarounds. 有很多解决方法。 I want to know about combining two unidirectional FDs into a single bidirectional one. 我想知道有关将两个单向FD组合到一个双向FD中的信息。

I am developing a C utility to control some fancy electronics over UART on Linux, and I would like to test the input/output of my code by using canned transactions from a file. 我正在开发一个C实用程序,以通过Linux上的UART控制一些高级电子设备,并且我想通过使用文件中的固定交易来测试代码的输入/输出。 That is, my mocked device responses will come from a text file that I create beforehand and pipe in through STDIN, while the sent commands will go to STDOUT and be saved to a file. 也就是说,我模拟的设备响应将来自我预先创建并通过STDIN插入的文本文件,而发送的命令将进入STDOUT并保存到文件中。 This will allow transaction validation before connecting the real electronics, which could malfunction if mishandled. 这将允许在连接实际的电子设备之前进行交易验证,如果处理不当,可能会发生故障。

[I'm not sure this will answer your question, and I was going to just put it as a comment, but it got too long.] [我不确定这是否会回答您的问题,我只是将其作为评论,但时间太长。]

There are certainly many examples of "bidirectional" file descriptors in Unix and Linux -- serial ports and TCP streams pop immediately to mind. 在Unix和Linux中,肯定有很多“双向”文件描述符的示例-串行端口和TCP流立即浮现在脑海中。

If you have one of those, you can set up a process that both reads and writes to it (that is, that uses that one fd as both stdin and stdout) by using dup or dup2 to set it up as fd's 0, 1, and 2. 如果您有其中之一,则可以通过使用dupdup2将其设置为fd的0、1来设置一个可以对其进行读写的进程(即,将那个fd用作stdin和stdout)。 2。

Finally, if you're a process and you believe you've been given a bidirectional fd as 0, 1, and 2, you can just grab 0, 1, or 2 and both read from and write to it (that is, write to 0 or read from 1). 最后,如果您是一个进程,并且您相信自己已获得双向fd分别为0、1和2,则只需抓取0、1或2即可读取和写入它(即,写入到0或从1读取)。

But if fd's 0 and 1 aren't clones of each other, I don't know of a way to "combine" them into a single fd that permits both reading and writing. 但是,如果fd的0和1不是彼此的克隆,我不知道一种将它们“组合”为允许读取和写入的单一fd的方法。


Addendum: You mentioned the nuisance of carrying around two file descriptors if you're using a standard Unix pipe. 附录:您提到如果使用标准Unix管道,则可能会携带两个文件描述符。 I think the "bidirectional" alternative to a standard pipe would be a Unix-domain socket (evidently now called PF_LOCAL ). 我认为标准管道的“双向”替代方案将是Unix域套接字(现在显然称为PF_LOCAL )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM