Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe vs. Temporary File

Is there a big performance difference between:

  • Process A writing to a temp file, and process B reading that file
  • Process A writing to a pipe, and process B reading from that pipe

I'm curious to know what the answer is for both Windows and *nix.

EDIT: I should have asked: Does the buffer cache eliminate the difference between a temp file and a pipe?

like image 286
Matt Fichman Avatar asked Aug 08 '11 03:08

Matt Fichman


People also ask

What is the difference between a pipe and a file?

The difference between a named pipe and a regular file in Unix is that. Pipes forbid random accessing, while regular files do allow this.

What is a pipe file?

CAD file created by PIPE-FLO, a program used for designing piping systems; stores a schematic of the piping system (series, branching, or looped) and can include pipelines, pumps, compressors, control valves, and other components; used for both liquid and gas systems.

Why FIFO is better than pipes?

The pipe has no name; it is created for one use and both ends must be inherited from the single process which created the pipe. A FIFO special file is similar to a pipe, but instead of being an anonymous, temporary connection, a FIFO has a name or names like any other file.

Is a named pipe a file?

A FIFO, also known as a named pipe, is a special file similar to a pipe but with a name on the filesystem. Multiple processes can access this special file for reading and writing like any ordinary file. Thus, the name works only as a reference point for processes that need to use a name in the filesystem.


2 Answers

One big difference is that with the pipe, processes A and B can be running concurrently, so that B gets to work on the output from A before A has finished producing it. Further, the size of the pipe is limited, so A won't be able to produce vastly more data than B has consumed; it will be made to wait for B to catch up.

If the volume of data is big, then writing to the temporary file involves disk activity, even if only for creating and then destroying the file. The data might well stay in the in-memory buffer pools - so no disk I/O there - even for surprisingly large files. Writing to the pipe 'never' involves writing to disk.

like image 199
Jonathan Leffler Avatar answered Sep 21 '22 13:09

Jonathan Leffler


The big difference is that the first method actually uses on-disk storage, whereas a pipe will use memory (unless you get really pedantic and start thinking about swap space).

Performance-wise, memory is faster than disk (almost always). This should be generally true for all operating systems.

The only time when using a temp file really makes sense is if process B has to examine the data in multiple passes (like certain kinds of video encoding). For this use, the whole data stream would need to be buffered and if there were enough data yes it would probably negate the in-memory advantage. So for multi-pass (seek-bound) operations, go with a temp file.

like image 41
Chris Eberle Avatar answered Sep 20 '22 13:09

Chris Eberle