9front - general discussion about 9front
 help / color / mirror / Atom feed
From: arisawa <karisawa@gmail.com>
To: 9front@9front.org
Subject: pipe: bug or feature?
Date: Tue, 28 Mar 2017 10:42:47 +0900	[thread overview]
Message-ID: <BFB26C2F-FDF7-4118-B2BD-373750346B2E@gmail.com> (raw)

Hello,

I was playing with an experimental program on pipe and met with a problem which I don’t understand.

the program reads a file and writes it back 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 a feature of pipe?

Kenji Arisawa

=== BEGIN a.c ===
#include <u.h>
#include <libc.h>

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 = argv[0];
	argc--;argv++;
	USED(argc);
	if(argv[0] == nil)
		usage();
	file = argv[0];
	fd = 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 = read(fd,buf0,sizeof(buf0))) > 0){
		print("read: %d %s\n",n,file);
		n = write(pfd[1],buf0,n);
		print("write: %d\n",n);
	}
	close(pfd[1]);
	while((n = read(pfd[0],buf,sizeof(buf))) > 0){
		buf[n] = 0;
		print("%d %s\n",n,buf);
	}
	print("%d\n",n);
		
	exits(nil);
}
=== END a.c ===



             reply	other threads:[~2017-03-28  1:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-28  1:42 arisawa [this message]
2017-03-28  2:27 ` [9front] " Alex Musolino
2017-03-28  3:33   ` arisawa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BFB26C2F-FDF7-4118-B2BD-373750346B2E@gmail.com \
    --to=karisawa@gmail.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).