From mboxrd@z Thu Jan 1 00:00:00 1970 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) From: arisawa In-Reply-To: Date: Sun, 2 Apr 2017 17:01:38 +0900 Content-Transfer-Encoding: quoted-printable Message-Id: <112F60CB-D7E4-412E-9B48-BF5C38050AFA@gmail.com> References: <57E90700-85A0-47F4-9156-DE732859A74C@gmail.com> To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Subject: Re: [9fans] pipe: bug or feature? Topicbox-Message-UUID: b9102c82-ead9-11e9-9d60-3106f5b1d025 thanks, that is my fault. it should be while((n =3D read(fd,buf0,sizeof(buf0)-1)) > 0){ and while((n =3D read(pfd[0],buf,sizeof(buf)-1)) > 0){ > 2017/04/02 16:34=E3=80=81Skip Tavakkolian = =E3=81=AE=E3=83=A1=E3=83=BC=E3=83=AB=EF=BC=9A >=20 > this is either a bug in devpipe or the documentation is misleading. = the man page seems to say that the pair of fd's returned by pipe(2) are = symmetrical, but it matters which fd of the pair is used for writing. = if you switch around pfd[0] and pfd[1], it works as you'd expect. >=20 > On Fri, Mar 31, 2017 at 5:29 PM arisawa wrote: > Hello, >=20 > I was playing with an experimental code on pipe and met with a problem = which I don=E2=80=99t understand. >=20 > the program reads a file and writes it to one end of pipe and then = reads it from another end of pipe. > the buffer for writing pipe is named buf0, and for reading pipe is = named buf. > and I found the program does not finish unless sizeof(buf) > = sizeof(buf0). > is this a bug or feature of pipe? >=20 > Kenji Arisawa >=20 > =3D=3D=3D BEGIN a.c =3D=3D=3D > #include > #include >=20 > char *argv0; >=20 > void > usage(void) > { > fprint(2,"usage: %s file\n",argv0); > exits("usage"); > } >=20 > void > main(int argc, char *argv[]) > { > int fd,pfd[2]; > char buf[256]; > char buf0[256]; > /* need to be sizeof(buf) > sizeof(buf0) > * but this condition is very curious to me */ > int n; > char *file; > argv0 =3D argv[0]; > argc--;argv++; > USED(argc); > if(argv[0] =3D=3D nil) > usage(); > file =3D argv[0]; > fd =3D open(file,OREAD); > if(fd < 0) > sysfatal("no such file"); >=20 > if(pipe(pfd) < 0) > sysfatal("pipe error"); > print("pfd: %d %d\n",pfd[0],pfd[1]); >=20 > while((n =3D read(fd,buf0,sizeof(buf0))) > 0){ > print("read: %d %s\n",n,file); > n =3D write(pfd[1],buf0,n); > print("write: %d\n",n); > } > close(pfd[1]); > while((n =3D read(pfd[0],buf,sizeof(buf))) > 0){ > buf[n] =3D 0; > print("%d %s\n",n,buf); > } > print("%d\n",n); >=20 > exits(nil); > } > =3D=3D=3D END a.c =3D=3D=3D