From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 References: <57E90700-85A0-47F4-9156-DE732859A74C@gmail.com> <20170402123359.GA18347@ax.s16> In-Reply-To: <20170402123359.GA18347@ax.s16> From: Skip Tavakkolian Date: Sun, 2 Apr 2017 16:44:51 +0000 Message-ID: To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: multipart/alternative; boundary=001a113a6e348e550b054c31c389 Subject: Re: [9fans] pipe: bug or feature? Topicbox-Message-UUID: b91a2aa2-ead9-11e9-9d60-3106f5b1d025 --001a113a6e348e550b054c31c389 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable good catch! the behavior makes sense now. On Sun, Apr 2, 2017 at 5:34 AM Antons Suspans wrote: > 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 > 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. > > > > On Fri, Mar 31, 2017 at 5:29 PM arisawa wrote: > > > > Hello, > > > > I was playing with an experimental code on pipe and met with a proble= m > which > > I don=E2=80=99t understand. > > > > 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? > > > > Kenji Arisawa > > > > =3D=3D=3D BEGIN a.c =3D=3D=3D > > #include > > #include > > > > char *argv0; > > > > void > > usage(void) > > { > > fprint(2,"usage: %s file\n",argv0); > > exits("usage"); > > } > > > > 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"); > > > > if(pipe(pfd) < 0) > > sysfatal("pipe error"); > > print("pfd: %d %d\n",pfd[0],pfd[1]); > > > > 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); > > > > exits(nil); > > } > > =3D=3D=3D END a.c =3D=3D=3D > > > > --001a113a6e348e550b054c31c389 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
good catch! the behavior makes sense now.

On Sun, Apr 2, 2017 at 5:34 AM Antons = Suspans <antox@ml.lv> wrote:
This was writing a \0 just past end of b= uf[], into memory alloted for pfd[0].
See the explanation by Alex in the 9front list:
/n/9front.org/lists/9front/149066802= 8.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. th= e man
> page seems to say that the pair of fd's returned by pipe(2) are sy= mmetrical,
> but it matters which fd of the pair is used for writing. =C2=A0if you = switch around
> pfd[0] and pfd[1], it works as you'd expect.
>
> On Fri, Mar 31, 2017 at 5:29 PM arisawa <karisawa@gmail.com>= ; wrote:
>
>=C2=A0 =C2=A0Hello,
>
>=C2=A0 =C2=A0I was playing with an experimental code on pipe and met wi= th a problem which
>=C2=A0 =C2=A0I don=E2=80=99t understand.
>
>=C2=A0 =C2=A0the program reads a file and writes it to one end of pipe = and then reads it
>=C2=A0 =C2=A0from another end of pipe.
>=C2=A0 =C2=A0the buffer for writing pipe is named buf0, and for reading= pipe is named buf.
>=C2=A0 =C2=A0and I found the program does not finish unless sizeof(buf)= > sizeof(buf0).
>=C2=A0 =C2=A0is this a bug or feature of pipe?
>
>=C2=A0 =C2=A0Kenji Arisawa
>
>=C2=A0 =C2=A0=3D=3D=3D BEGIN a.c =3D=3D=3D
>=C2=A0 =C2=A0#include <u.h>
>=C2=A0 =C2=A0#include <libc.h>
>
>=C2=A0 =C2=A0char *argv0;
>
>=C2=A0 =C2=A0void
>=C2=A0 =C2=A0usage(void)
>=C2=A0 =C2=A0{
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 fprint(2,"usage: %s file\= n",argv0);
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 exits("usage");
>=C2=A0 =C2=A0}
>
>=C2=A0 =C2=A0void
>=C2=A0 =C2=A0main(int argc, char *argv[])
>=C2=A0 =C2=A0{
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 int fd,pfd[2];
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 char buf[256];
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 char buf0[256];
>=C2=A0 =C2=A0=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 =C2=A0 =C2=A0* but this condition is = very curious to me */
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 int n;
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 char *file;
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 argv0 =3D argv[0];
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 argc--;argv++;
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 USED(argc);
>=C2=A0 =C2=A0=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 =C2=A0 =C2=A0 us= age();
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 file =3D argv[0];
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 fd =3D open(file,OREAD);
>=C2=A0 =C2=A0=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 =C2=A0 =C2=A0 sy= sfatal("no such file");
>
>=C2=A0 =C2=A0=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 =C2=A0 =C2=A0 sy= sfatal("pipe error");
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 print("pfd: %d %d\n"= ,pfd[0],pfd[1]);
>
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 while((n =3D read(fd,buf0,size= of(buf0))) > 0){
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 pr= int("read: %d %s\n",n,file);
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 n = =3D write(pfd[1],buf0,n);
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 pr= int("write: %d\n",n);
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =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 =C2=A0 =C2=A0 while((n =3D read(pfd[0],buf,s= izeof(buf))) > 0){
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 bu= f[n] =3D 0;
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 pr= int("%d %s\n",n,buf);
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 print("%d\n",n);
>
>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 exits(nil);
>=C2=A0 =C2=A0}
>=C2=A0 =C2=A0=3D=3D=3D END a.c =3D=3D=3D
>

--001a113a6e348e550b054c31c389--