2

I want to understand the lifetime of a pipe? http://linux.die.net/man/2/pipe

  1. Does the data in the pipe stay alive if either the sender or receiver dies/exits?
  2. Can the pipe be created if the receiver is not present? (i.e. has not been forked off yet)?

I need to send data from sender to the receiver. However, the receiver may not have been forked off yet, and may be active about (1~2 seconds after the sender). They share the parent process, but the receiver may be forked off at some point much after the sender or vice versa.

Also it is possible that the sender can finish processing and exit at any time. I'm trying to see if using pipe's instead of a shared memory queue would work for me.

tsar2512
  • 2,826
  • 3
  • 33
  • 61

1 Answers1

1

The pipe MUST be created before the fork. After the fork, each process uses either the read or the write end. It's best to close the not-used end of the pipe immediately after the fork.

If the writing process exits, the reader can read all the remaining data in the pipe, but the subsequent read system call in it returns with 0 bytes read, that's how you know it's over. If the writing process is still keeping the pipe open but does not write anything into it, read blocks until bytes become available.

If the writing process has written a lot of data into the pipe and exits, the data are still available for the reader.

If the reading process exits, the writing process is killed by a SIGPIPE signal. It has the option of handling the signal in different ways, but it's killed by default.

So the pipe may survive the writer, but not the reader. Proof of concept (cső is Hungarian for pipe):

#include <unistd.h>                       
int main(void)                            
{                                         
        int cso[2];                       
        pipe(cso);                        
        if (fork() == 0) {                
                close(cso[0]);            
                write(cso[1], "cso\n", 4);
                return 0;                 
        }                                 
        close(cso[1]);                    
        sleep(2);                         
        if (fork() == 0) {                
                char line[4];             
                read(cso[0], line, 4);    
                write(1, line, 4);        
                return 0;                 
        }                                 
        close(cso[0]);                    
        return 0;                         
}                                         
SzG
  • 12,333
  • 4
  • 28
  • 41
  • How is the "writing" and "reading" process defined? I'm guessing dependent on which end of the pipe file descriptor is "open" in the current process? – tsar2512 Nov 29 '14 at 14:55
  • Exactly. Usually you close the not-used end of the pipe immediately after the fork. – SzG Nov 29 '14 at 14:56
  • understood, so if I keep both of them open in both the sender and the reader, it should do the trick? ie. if either of them dies I will not loose data. Also I should be able to send the data on the pipe if the reader has not been forked off yet? – tsar2512 Nov 29 '14 at 14:57
  • What do you mean with "not loose data"?!?!?!?! – SzG Nov 29 '14 at 15:01
  • I've made an edit to the question, I'm trying to send data from a sender to the receiver, where both are independent processes and the receiver can start a little after the sender, they share a parent process, but are forked off at different points in the execution of the parent. – tsar2512 Nov 29 '14 at 15:04