FIFO - Named Pipe - Linux - C

This is a discussion on FIFO - Named Pipe - Linux - C within the C Programming forums, part of the General Programming Boards category; Hey there, I want to create a fifo to pass informations from one process to another one. There will be ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    Question FIFO - Named Pipe - Linux - C

    Hey there,

    I want to create a fifo to pass informations from one process to another one. There will be many writers and one reader. My problem is, that the informations passed through the named pipe are interleaved (no border between messages) and so the reader can't identify what is send.

    Example:

    Many writer send per write the message "Hello" then it can happens:

    HelloHello
    or
    Hello
    Hello

    On the reader side, if the reader only checks for "Hello", "HelloHello" can't be identified as 2 messages. Can I send a delimiter or is there an other solution?

    thx

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    If you have 10 writers, why not just create 10 pipes. Your reader can read from all 10 of them and then there is no ambiguity about who said what.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    The number of writers is not predefined. The writers come and go.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    So then create additional named pipes as the writers come and go.

    Or if you really want to just use one pipe that each writer writes to... then use a locking mechanism, the same way you would for files. Each of the writers that can possibly write to a resource first obtains a lock on the file and then writes. He then releases the lock as soon as he's done sending his message. In Linux and POSIX systems the locking system is well-defined and managed by the system, so you should be able to use this approach without coding your own locking mechanism.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,850
    Make each client prefix the message with some ident, so perhaps
    ID1:Hello\n
    ID2:Hello\n
    ID3:World\n

    Also, be aware of the value of PIPE_BUF on your system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    890
    Quote Originally Posted by hell-student View Post
    I want to create a fifo to pass informations from one process to another one. There will be many writers and one reader.
    It sounds like a datagram socket would be a better fit. If the processes are related (one starts the others), then you can use socketpair() to create the sockets safely. Otherwise, better make them accessible via the filesystem, and use unix domain datagram sockets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with named FIFO
    By kbzium in forum C Programming
    Replies: 4
    Last Post: 12-28-2011, 10:56 AM
  2. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 06:40 PM
  3. How to flush a named pipe (FIFO) after reading from it?
    By Mr_Miguel in forum C Programming
    Replies: 1
    Last Post: 01-10-2007, 06:05 PM
  4. named pipe problem
    By fnoyan in forum Linux Programming
    Replies: 0
    Last Post: 05-28-2006, 06:54 AM
  5. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 02:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21