Thread: Difference between Serial Port and Named Pipe

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    6

    Difference between Serial Port and Named Pipe

    Is there a difference between a Serial Port stream and a Named Pipe (FIFO)? Especially in regards to Linux?

    My understanding is that both:

    - Are full duplex
    - Can be read/written by process that are unrelated (as opposed to how regular Pipes work)

    The only difference I can think of are:

    - Serial Ports have file descriptors to actual hardware (that the hardware is reading/writting to) whereas a Named Pipe is just a 'file' created on the kernal to store a data stream then 2 (or more?) processes can connect to and read/write.
    - Any other differences if any?

    Also if I have one named pipe that I create in one process P1 (and another of my processes P2 connects to it) - can P1 use that one file descriptor to write and read to this named pipe? And P2 can do the same (both read and write). Or do I need to create 2 named pipes if I want P1 to be able to write and read to P2? The practical use is that P1 will write commands to P2 and also read results of those commands from P2 aswell.

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by HiMK View Post
    Serial Ports have file descriptors to actual hardware (that the hardware is reading/writting to)
    No, serial ports are devices. In POSIX-y systems like Linux, they are ttys, which are character devices basically just like pseudoterminals, and can be controlled via the termios interface. (The termios functions are internally implemented as ioctl()s on the device descriptor.)

    Named pipes are also called fifos, for first-in-first-out, and although they look like files a bit, they behave quite differently. They are their own beast.

    Quote Originally Posted by HiMK View Post
    if I have one named pipe that I create in one process P1 (and another of my processes P2 connects to it) - can P1 use that one file descriptor to write and read to this named pipe?
    No. Named pipes are limited in the same way as standard pipes are: one end reads, the other writes. To swap roles, both must close and reopen the pipe.

    To do what you described, you'll want to use an Unix domain socket; a variant often called a pathname socket. P1 will create the socket, bind it to a filesystem visible name; it'll look very, very much like a named pipe. Then, P1 will accept() new connections, getting a new descriptor for each new connection. This connection is bidirectional, full duplex.

    To connect, clients must create an Unix domain stream socket, and connect() to the pathname. After that, the processes can use read(), write(), readv(), writev(), send(), recv(), and so on to send data back and forth.

    Unlike named pipes, this also allows more than one different P2 to be connected to P1, without confusing the communications; for P1, each client will use a separate descriptor (acquired via accept()).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NAMED PIPE - 1 record behind
    By jmar2014 in forum C Programming
    Replies: 2
    Last Post: 09-25-2014, 10:08 AM
  2. Named Pipe problem again
    By rahul_c in forum C Programming
    Replies: 4
    Last Post: 02-22-2012, 12:24 PM
  3. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  4. Named Pipe Problems.
    By Mastadex in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2006, 08:35 AM
  5. named pipe problem
    By fnoyan in forum Linux Programming
    Replies: 0
    Last Post: 05-28-2006, 05:54 AM

Tags for this Thread