From mboxrd@z Thu Jan 1 00:00:00 1970 Date: Sun, 2 Apr 2017 15:33:59 +0300 From: Antons Suspans To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Message-ID: <20170402123359.GA18347@ax.s16> References: <57E90700-85A0-47F4-9156-DE732859A74C@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.1 (2016-04-27) Content-Transfer-Encoding: quoted-printable Subject: Re: [9fans] pipe: bug or feature? Topicbox-Message-UUID: b914fdc0-ead9-11e9-9d60-3106f5b1d025 This was writing a \0 just past end of buf[], into memory alloted for pfd= [0]. See the explanation by Alex in the 9front list: /n/9front.org/lists/9front/1490668028.00 On Sun, Apr 02, 2017 at 07:34:52AM +0000, Skip Tavakkolian wrote: > 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 symmetr= ical, > but it matters which fd of the pair is used for writing. =C2=A0if you s= witch around > pfd[0] and pfd[1], it works as you'd expect. >=20 > On Fri, Mar 31, 2017 at 5:29 PM arisawa wrote: >=20 > Hello, >=20 > I was playing with an experimental code on pipe and met with a proble= m which > I don=E2=80=99t understand. >=20 > the program reads a file and writes it to one end of pipe and then re= ads it > from another end of pipe. > the buffer for writing pipe is named buf0, and for reading pipe is na= med buf. > and I found the program does not finish unless sizeof(buf) > sizeof(b= uf0). > 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) > { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 fprint(2,"usage: %s file\n",argv0); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 exits("usage"); > } >=20 > void > main(int argc, char *argv[]) > { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 int fd,pfd[2]; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 char buf[256]; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 char buf0[256]; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 /* need to be sizeof(buf) > sizeof(buf0) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* but this condition is very curiou= s to me */ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 int n; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 char *file; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 argv0 =3D argv[0]; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 argc--;argv++; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 USED(argc); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 if(argv[0] =3D=3D nil) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 usage(); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 file =3D argv[0]; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 fd =3D open(file,OREAD); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 if(fd < 0) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sysfatal("no = such file"); >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 if(pipe(pfd) < 0) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sysfatal("pip= e error"); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 print("pfd: %d %d\n",pfd[0],pfd[1]); >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 while((n =3D read(fd,buf0,sizeof(buf0))) = > 0){ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print("read: = %d %s\n",n,file); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 n =3D write(p= fd[1],buf0,n); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print("write:= %d\n",n); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 } > =C2=A0 =C2=A0 =C2=A0 =C2=A0 close(pfd[1]); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 while((n =3D read(pfd[0],buf,sizeof(buf))= ) > 0){ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 buf[n] =3D 0; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print("%d %s\= n",n,buf); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 } > =C2=A0 =C2=A0 =C2=A0 =C2=A0 print("%d\n",n); >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 exits(nil); > } > =3D=3D=3D END a.c =3D=3D=3D >=20