Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 Oct 2000 16:47:26 +0900 (JST)
From:      Motonori Shindo <mshindo@mshindo.net>
To:        freebsd-net@freebsd.org
Subject:   Re: pty question
Message-ID:  <20001012.164726.41626950.mshindo@mshindo.net>
In-Reply-To: <20001012.063016.112630716.mshindo@mshindo.net>
References:  <20001012.063016.112630716.mshindo@mshindo.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

From: Motonori Shindo <mshindo@mshindo.net>
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 <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <termios.h>
#include <libutil.h>
#include <string.h>

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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20001012.164726.41626950.mshindo>