fork() and pipe()

This is a discussion on fork() and pipe() within the C Programming forums, part of the General Programming Boards category; hi my question is the fallowing I have many processes all writing to the same pipe how can I synchronize ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    fork() and pipe()

    hi my question is the fallowing

    I have many processes all writing to the same pipe how can I synchronize them so I don't have multiple processes writing to the pipe at the same time?


    Thanks in advance.
    Last edited by scioner; 06-13-2008 at 11:24 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    You can't.

    IIRC, each write() is atomic. So if you prepare what you want to write in a buffer, then use a single write() call to write it, then that data will be read at the other end of the pipe as a contiguous block (that is, not mixed up with anyone elses write calls).
    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.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    reply

    sweeeet I did not know that, thats great. thanks man. i was still with thread mindset

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Salem View Post
    You can't.

    IIRC, each write() is atomic. So if you prepare what you want to write in a buffer, then use a single write() call to write it, then that data will be read at the other end of the pipe as a contiguous block (that is, not mixed up with anyone elses write calls).
    Each write is atomic up to the pipe buffer size. In <limits.h> is declared a constant PIPE_BUF. Writing up to this many bytes is atomic. Writes larger than this size will be broken into smaller pieces and are NOT atomic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2009, 06:13 PM
  2. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  3. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  4. Fork - Pipe trouble
    By Markyboy in forum C Programming
    Replies: 4
    Last Post: 07-06-2008, 11:09 PM
  5. fork and pipe
    By tbarsness in forum C Programming
    Replies: 3
    Last Post: 10-22-2007, 12:40 PM

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