Advantages of using named pipes and sockets rather than temporary files

concurrencyipcpipesocket

I have two cooperating programs. One program just writes its output to a file and the other one then reads from the file and spits the data out for the front end to work with.

I have been reading about named pipes and domain sockets, but I am having trouble seeing what advantages they offer to just using a temp file. It just seems like a formal way of communicating to me.

Best Answer

  1. If you need to save the intermediate file after the processing is done, then inter-process communication (such as through a pipe or socket) is not particularly valuable.  Similarly, if you need to run the two programs at vastly different times, you should just do it the way you're doing it now.
  2. Back when Unix was created, disks were very small, and it was common for a rather benign command to consume all the free space in a file system. For example,

    some_command_that_produces_a_lot_of_output | grep some_very_obscure_string

    produces output that's much smaller than the size of the output of the first command (i.e., the size of the intermediate file that would be created if you ran the commands the way you're running your programs).

  3. Data flowing through pipes and sockets is (probably) not written to disk at all.  Therefore, these IPC solutions may be

    • more efficient (faster) than disk-based solutions.
    • more secure than disk-based solutions, if the intermediate data are more sensitive than the end result.