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

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

I'm trying to use pty but it doesn't work as I expect. I probably miss
something, but I can't find it for the last few days...

Here's the essence of my program. It's supposed to work in a way that
a child (slave) process reads data from starndard input, which is
actually connected to a parent process (master)'s file discriptor
'fdm_w'. However, the slave doesn't seem to read anything from the
standard input, though master did write somehting to the corresponding
file descriptor.

On the other hand, the opposite direction works, i.e, if slave writes
something, master can read it OK.

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.

Any advise will be greatly appreciated!

Best Regards,

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <libutil.h>

int main()
{
	int	fdm_r, fdm_w;
	int	fds_r, fds_w;
	char	buf[256];
	int	p;
	int 	x;

	openpty(&fdm_r, &fds_w, NULL, NULL, NULL);
	openpty(&fdm_w, &fds_r, NULL, NULL, NULL);

	bzero(buf, sizeof(buf));

	p = fork();
 
	if (p == 0) { /* child */
		close(0);
		close(1);
		/* close(2); */
		close(fdm_r);
		close(fdm_w);

		fprintf(stderr, "debug\n");
		if (dup2(fds_r, 0) == -1) {
			fprintf(stderr, "dup2\n"); 
			exit(1);
		}
		if (dup2(fds_w, 1) == -1) {
			fprintf(stderr, "dup2\n"); 
			exit(2);
		}

		fprintf(stderr, "slave reading\n");
		
		x = read(0, buf, 3);  
		fprintf(stderr, "slave %d read\n", x);
		if (buf[0] == 'A') {
			printf("BBB\n");
		} else {
			printf("ZZZ\n");
		}
		exit(1);
	} else { /* parent */
		close(fds_r);
		close(fds_w);
		printf("master writing\n");
		sleep(3);
		write(fdm_w, "AAAAA", 5);
		printf("master written\n");
		printf("master reading\n");
		read(fdm_r, buf, 5);
		printf("%c%c%c\n", 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.063016.112630716.mshindo>