Are FIFO, pipe & Unix domain socket the same thing in Linux kernel?

I heard that FIFOs are named pipes. And they have exactly the same semantics. On the other hand, I think Unix domain socket is quite similar to pipe (although I’ve never made use of it). So I wonder if they all refer to the same implementation in Linux kernel. Any idea?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

UNIX domain sockets and FIFO may share some part of their implementation but they are conceptually very different. FIFO functions at a very low level. One process writes bytes into the pipe and another one reads from it. A UNIX domain socket has similar behaviour as a TCP/IP or UDP/IP socket.

A socket is bidirectional and can be used by a lot of processes simultaneously. A process can accept many connections on the same socket and attend several clients simultaneously. The kernel delivers a new file descriptor each time connect(2) or accept(2) is called on the socket. The packets will always go to the right process.
On a FIFO, this would be impossible. For bidirectional communication, you need two FIFOs, and you need a pair of FIFOs for each of your clients. There is no way of writing or reading in a selective way, because they are a much more primitive way to communicate.

Anonymous pipes and FIFOs are very similar. The difference is that anonymous pipes don’t exist as files on the filesystem so no process can open(2) it. They are used by processes that share them by another method. If a process creates pipes and then performs, for example, a fork(2), its child will inherit its file descriptors and, among them, the pipe. (File descriptors to named pipes/FIFOs can also be passed in the same way.)

The UNIX domain sockets, anonymous pipes and FIFOs are similar in the fact they provide interprocess communication using file descriptors, where the kernel handles the system calls and abstracts the mechanism.

Method 2

There’s quite a good discussion of this here: http://www.slideshare.net/divyekapoor/linux-kernel-implementation-of-pipes-and-fifos

So far as I can see, both from the presentation slides, and the source @ http://lxr.free-electrons.com/source/fs/pipe.c – fifo’s are implemented as a wrapper around pipe’s, and pipe’s themselves are implemented via the pipefs virtual filesystem.

@lgeorget – The pipes appear to use kernel memory for buffers between the readers and the writers – they don’t use ‘shared memory’ as such, and copy memory between user and kernel address spaces (eg, pipe_read calls pipe_iov_copy_to_user, which calls __copy_to_user_inatomic (or copy_to_user). __copy_to_user_inatomic calls copy_user_generic, which is on of several ASM implementations.

Method 3

A “FIFO” and a “named pipe” is the same thing – though it’s quite different from how a shell handles a “pipe” (|) between two commands on the command-line.

A named pipe (FIFO) is a single “file” shared by two programs, where one writes to it and the other read from it… A socket on the other hand is a “connection” between two “files” – which may use a network and be on separate computers – where one program read/writes to one “file” and another program read/writes to the other… I don’t think they’re that similar… On the other hand both sockets and named pipes – as well as files, devices, symbolic links – all uses inodes, and they all implements some common features (like read and write).

Method 4

I don’t think so Justin. If I’m not mistaken, and I quite possibly am, I think FIFOs use a file on disk, and Unix Domain sockets use kernel memory.

Also, as an addition to the poster above who mentioned that Unix domain sockets are bi-directional, that is only the case when using a SOCK_STREAM socket. SOCK_DGRAM Unix domain sockets are, in fact, uni-directional, and can only send() from the code that called connect(), to the code that called bind().

Of course, the code that called connect() must also call bind() to create it’s own endpoint, but that has nothing to do with your question.

Method 5

My 2 cents…FIFO and UNIX socket are both bi-directional (similar) but socket have a star topology while a FIFO is just a queue (and hence cannot replace each other), yes their implementation may share code internally.

**

char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
fd = open(myfifo, O_RDWR);   //so that you can read/write to it
 ...
write(fd, buff1, sizeof(buff1));  
getchar();//wait till some one reds this and writes something else
int sz=read(fd, buff1, sizeof(buff1));  //read that something**


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x