From simon.urbanek at r-project.org  Fri Mar 14 15:54:10 2014
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Fri, 14 Mar 2014 10:54:10 -0400
Subject: [Rd] Detect a terminated pipe
In-Reply-To: <5322F166.30706@ivt.baug.ethz.ch>
References: <5322F166.30706@ivt.baug.ethz.ch>
Message-ID: <034131C4-F4C4-44C6-BF23-9F8892DD9138@r-project.org>

On Mar 14, 2014, at 8:09 AM, Kirill M?ller <kirill.mueller at ivt.baug.ethz.ch> wrote:

> Hi
> 
> Is there a way to detect that the process that corresponds to a pipe has ended? On my system (Ubuntu 13.04), I see
> 
> > p <- pipe("true", "w"); Sys.sleep(1); system("ps -elf | grep true | grep -v grep"); isOpen(p)
> [1] TRUE
> 
> The "true" process has long ended (as the filtered ps system call emits no output), still R believes that the pipe p is open.
> 

As far as R is concerned, the connection is open. In addition, pipes exist even without the process - you can close one end of a pipe and it will still exist (that?s what makes pipes useful, actually, because you can choose to close arbitrary combination of the R/W ends). Detecting that the other end of the pipe has closed is generally done by sending/receiving data to/from the end of interest - i.e. reading from a pipe that has closed the write end on the other side will yield 0 bytes read. Writing to a pipe that has closed the read end on the other side will yield SIGPIPE error (note that for text connections you have to call flush() to send the buffer):

> p=pipe("true","r")
> readLines(p)
character(0)
> close(p)

> p=pipe("true","w")
> writeLines("", p)
> flush(p)
Error in flush.connection(p) : ignoring SIGPIPE signal
> close(p)

Cheers,
Simon




> Thanks for your input.
> 
> 
> Best regards
> 
> Kirill
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] Detect a terminated pipe
Simon Urbanek