From owner-freebsd-net Thu Oct 12 0:47:26 2000 Delivered-To: freebsd-net@freebsd.org Received: from center.mshindo.net (center.mshindo.net [210.231.221.221]) by hub.freebsd.org (Postfix) with ESMTP id 4315937B502 for ; Thu, 12 Oct 2000 00:47:22 -0700 (PDT) Received: from cosinecom.com (proxy61.cosinecom.com [63.88.104.61]) by center.mshindo.net (8.9.3/3.7W/00041811) with SMTP id QAA11109 for ; Thu, 12 Oct 2000 16:54:32 +0900 (JST) Date: Thu, 12 Oct 2000 16:47:26 +0900 (JST) Message-Id: <20001012.164726.41626950.mshindo@mshindo.net> To: freebsd-net@freebsd.org Subject: Re: pty question From: Motonori Shindo In-Reply-To: <20001012.063016.112630716.mshindo@mshindo.net> References: <20001012.063016.112630716.mshindo@mshindo.net> X-Mailer: Mew version 1.95b30 on XEmacs 21.1 (Canyonlands) X-PGP-fingerprint: 06 B0 B1 A4 06 C1 6A 14 63 C0 D7 18 01 CD D9 83 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-net@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org Hi, From: Motonori Shindo Subject: pty question Date: Thu, 12 Oct 2000 06:30:16 +0900 (JST) > You may wonder why two pairs of ptys, but it's a differnt stroy I > guess (uun, maybe not..). This is done because if master writes data > but subsequent read by master reads the same thing it has written > before (i.e loopbaked), so I decided to use two ptys, one for read and > another for write. To see what's happening, I wrote simpler code as follows. Why does parent process read the same thing as what has been previously written? Successful write() advances the file pointer and subsequent read should read right after that, shouldn't it? I believe this is true for a normal file but unsure about pty. Any comment? BTW, I'm using FreeBSD 4.0-R and 4.1.1. They gave me the same result. #include #include #include #include #include #include #include #include #include void log(const char *str) { FILE *fp; if ((fp = fopen("/var/tmp/log", "a")) == NULL) exit(100); fprintf(fp, "%s\n", str); fflush(fp); fclose(fp); } int main() { int fd; int pid; int x; char buf[256]; bzero(buf, sizeof(buf)); pid = forkpty(&fd, NULL, NULL, NULL); if (pid == 0) { /* child */ x = read(0, buf, 5); log(buf); if (x < 0) { exit(1); } else if (x == 0) { exit(2); } else { exit(3); } } else { /* parent */ printf("writing\n"); if (write(fd, "AAAAA", 5) < 0) { perror("write"); exit(1); } x = read(fd, buf, 5); if (x < 0) { perror("read"); } else if (x == 0) { printf("EOF\n"); } else { printf("%d bytes read (%c%c%c)\n", x, buf[0], buf[1], buf[2]); } } return 0; } To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-net" in the body of the message