From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 References: <57E90700-85A0-47F4-9156-DE732859A74C@gmail.com> In-Reply-To: <57E90700-85A0-47F4-9156-DE732859A74C@gmail.com> From: Skip Tavakkolian Date: Sun, 2 Apr 2017 07:34:52 +0000 Message-ID: To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: multipart/alternative; boundary=001a113a6e34b8b253054c2a140d Subject: Re: [9fans] pipe: bug or feature? Topicbox-Message-UUID: b90388ba-ead9-11e9-9d60-3106f5b1d025 --001a113a6e34b8b253054c2a140d Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 problem > 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 > --001a113a6e34b8b253054c2a140d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
this is either a bug in devpipe or the documentation is mi= sleading. the man page seems to say that the pair of fd's returned by p= ipe(2) are symmetrical, but it matters which fd of the pair is used for wri= ting. =C2=A0if you switch around pfd[0] and pfd[1], it works as you'd e= xpect.

On Fri, Mar 31, 2= 017 at 5:29 PM arisawa <karisawa@g= mail.com> wrote:
Hello,

I was playing with an experimental code on pipe and met with a problem whic= h 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 bu= f.
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 <u.h>
#include <libc.h>

char *argv0;

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");
}

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 curious to m= e */
=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 s= uch file");

=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("pipe= error");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print("pfd: %d %d\n",pfd[0],pfd[1]);<= br class=3D"gmail_msg">
=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(pfd[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);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 exits(nil);
}
=3D=3D=3D END a.c =3D=3D=3D
--001a113a6e34b8b253054c2a140d--