I want to create a special file, of which the content will be generated upon reading from, and processed upon writing to. There should be a background process handling the file. A pipe is not preferred as it never allows dual-band (r/w simultaneously). Two named pipes seem good but they are two files not one. What I want is much like a character device file or those stuffs in /sys. How can I do so?

  • 1
    Those character device files are controlled by kernel modules, as are the files under /sys. What are you trying to accomplish? – Fox Mar 9 '17 at 5:18
  • 3
    Perhaps a unix domain socket? This allows you to have only one pipelike file for both reading and writing, and it also allows multiple programs to read and write that file without interference. – Oskar Skog Mar 9 '17 at 6:26
  • 2
    @oskarskog while a good suggestion, note that the api's used to deal with UNIX domain sockets are not the same ones as those dealing with files, so it might not work – Wouter Verhelst Mar 9 '17 at 9:09

As long as you only want to read and write streams (one character after each other), you can use a pseudoterminal. This basically works like a two-sided pipe where only one side ("slave") is visible in the filesystem (as /dev/pts/X), and the other side ("master") is visible to the program that created it. It has some additional stuff like line discipline and serial parameters on top, which you can just ignore.

There are two ways to create pseudo terminals, the more modern is to use openpty and then select on the master side. See man openpty for details. Google should turn up some example programs.

  • Problem: ls and grep generate colored output to this pty, that's not wanted. Also a shell running on the pty generates prompts (PS1) to its stderr. – iBug May 26 '17 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.