Re: coprocess question



if(write(fd1[1],line,n)!=n){
perror("write error");
exit(-1);
}
//** Here is the question:
//Is it possible that the result is
//more than one digit, but only
//one digit is read from the pipe?
//For example:
//input is ``11 22\nC-d"
//will the output be "3"?
if((n=read(fd2[0],line,MAXLINE))<0){
perror("read error");
exit(-1);
}

POSIX [and all Unix] guarantees that write() on a pipe is atomic
up to length _POSIX_PIPE_BUF; see /usr/include/bits/posix1_lim.h.
POSIX requires that this limit be at least 512 bytes.
Linux is more generous. Until about 2 years ago, the actual limit
was 4096. With the new pipe filesystem in 2.6.11 and later (see
http://lwn.net/Articles/119682/ ) the minimum is 64KB.

So you are OK with write() and read(), but perhaps not with *printf(),
fputs(), scanf(), gets(), etc., which offer no guarantees with respect
to atomicity. In practice you can expect atomic writes if the stream
is buffered and if you flush() before and after output. Atomic input
with anything other than read() is problematic because of buffer
boundaries.

--
.



Relevant Pages

  • Re: PROBLEM: pthread-safety bug in write(2) on Linux 2.6.x
    ... my_parallel_program | tee log ... the pipe guarantees atomicity. ... the pipe only guarantees atomicity for writes smaller than ... the first thread, 4kB from the second, and then the remaining 4kB from the ...
    (Linux-Kernel)
  • Re: Dating a Dunhill
    ... If any of you guys or gals are experts on Dunhill dating please help ... I just fished out of my pipe drawer a Dunhill Silver Banded Classic ... indicate the size (first digit), ...
    (alt.smokers.pipes)
  • Re: Causing Perl to issue a single "write" system call?
    ... >> In a POSIX system all writes are guaranteed to be atomic if the data ... The only reference I can find to atomicity in SUSv3 is when ... > writing to a pipe or fifo, when writes of less than PIPE_BUF are ... you are very probably right about regular files. ...
    (comp.lang.perl.misc)
  • Re: coprocess question
    ... Mainly, the parent deliver input to ``add2" through pipe A, and the ... //one digit is read from the pipe? ...
    (comp.os.linux.development.apps)
  • coprocess question
    ... Mainly, the parent deliver input to ``add2" through pipe A, and the result is deliver back from ``add2" through pipe B. ... //one digit is read from the pipe? ...
    (comp.os.linux.development.apps)