Re: coprocess question
- From: John Reiser <jreiser@xxxxxxxxxxxx>
- Date: Thu, 21 Dec 2006 09:34:23 -0800
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.
--
.
- References:
- coprocess question
- From: Ronald
- coprocess question
- Prev by Date: coprocess question
- Next by Date: crontab
- Previous by thread: coprocess question
- Next by thread: Re: coprocess question
- Index(es):
Relevant Pages
|