Thread: Pipe(): Interprocess or Intraprocess comm?

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Pipe(): Interprocess or Intraprocess comm?

    I'm having trouble reading/writing to a pipe between two unrelated processes, and its left me wondering if pipes can be used for interprocess communication among non-related processes. Here's the pseudo code for my IPC logic (its client-server based):

    Server sets up a named pipe (fifo)
    Client connects to server fifo
    Client creates a private (anonymous) pipe.
    Client sends the output file descriptor of this pipe to server.
    Server creates a private pipe and sends output file desc to client
    Now the server has the write end of the client's private pipe and the client has the write end of the server's private pipe, while both listen on their read ends, thus creating a two-way communication between client & server right?....
    Apparently not in my case.

    Here is the code that causes trouble:
    (it differs from the pseudo in that I'm temporarily using a fifo to communicate back to the client)
    Server side:
    Code:
    int priv_serv_pipe[2];
    
    /* create private pipe for input to server */
    pipe(priv_serv_pipe);
    	
    /* send client the filedes for write-end of pipe */
    write(client_fifo, &priv_serv_pipe[1], sizeof(priv_serv_pipe[1]));
    	
    fprintf(stdout, "\nprivate pipe created and sent");
    
    /* read a client message from read-end*/
    read(priv_serv_pipe[0], &msg, sizeof(msg));
    
    fprintf(stdout, "\nmsg: %d", msg);
    Client side:
    Code:
    int priv_serv_pipe;
    
    /* read write-end file descriptor for server pipe */
    read(client_fifo, &priv_serv_pipe, sizeof(priv_serv_pipe));
    	
    /* send a message on write-end*/
    write(priv_serv_pipe, &msg, sizeof(msg));
    So far, the server succesfully communicates its file desriptor to the client, however the problem occurs when the client tries to write on that descriptor (no data is read by the server, and it continues to block on read). The client has no problems writing to the server's named fifo, so it leads me to believe that the client is incappable of writing on the server's private pipe. My question is, is it that only related process have permission to write on other processeses pipes? or am I simply doing something wrong here?
    Thanks for any help.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    File descriptors have no meaning outside the process they were created in. You can't just pass an fd to another process.

    Either the process must fork() itself into the two interacting processes (in which case they share the same open pipes -- this is how the shell does it), or the pipes must be created as a named pipes in the filesystem using the mkfifo() call -- then both sides will have to open the pipes by filename.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    thanks, that answers my question exactly!
    I guess that would also explain why I've never seen a pipe outside of a fork() or child/parent process context.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  2. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  3. Clearing a pipe used in IPC
    By cbgb in forum C Programming
    Replies: 5
    Last Post: 09-26-2006, 03:59 PM
  4. Named Pipe Problems.
    By Mastadex in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2006, 08:35 AM
  5. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM