linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matti Aarnio <matti.aarnio@zmailer.org>
To: Andrew Barton <andrevv@users.sourceforge.net>
Cc: aebr@win.tue.nl, linux-kernel@vger.kernel.org
Subject: Re: forkpty with streams
Date: Fri, 25 Jul 2003 21:46:35 +0300	[thread overview]
Message-ID: <20030725184635.GC6898@mea-ext.zmailer.org> (raw)
In-Reply-To: <1059130744.13184.11.camel@localhost>

On Fri, Jul 25, 2003 at 10:59:04AM +0000, Andrew Barton wrote:
...
> > > with the standard I/O stream functions. The calls to forkpty() and
> > > fdopen() and fprintf() all return successfully, but the data never seems
> > > to get to the child process.
> > 
> > > 	pid = forkpty (&fd, 0, 0, 0);
> > > 	if (pid == 0) {
> > > 		execlp ("sh", "sh", (void *)0);
> > > 	} else {
> > > 		F = fdopen (fd, "w");
> > > 		fprintf (F, "exit\n");
> > > 		fflush (F);
> > > 		wait (0);
> > > 	}
...
> Before I tried using streams, I just used write() to communicate with
> the ptty, but I had the same problem. I found that if I put a read()
> call before and after the write(), it worked. But why? Is this some kind
> of I/O voodoo? How does the reading affect the writing?

PTY file-handle is full-duplex bi-directional thing, and sometimes
it may need reading, or you get unwanted deadlocks.

> You mentioned that things would improve if I let the parent read from
> fd. Will this work using streams? I have tried opening fd in "r+" mode,
> but in that case I end up reading my own data. Do I need to lay an
> fflush() somewhere inbetween? What is it exactly that causes the data to
> be sent to the parent?

dup() helps you to have two fd:s,  fdopen() for both, one with "w",
other "r".   Things should not need that dup() actually.
Also fcntl() the fd's to be non-blocking.

Actually I am always nervous with stdio streams in places
where I want to use non-blocking file handles, and carefull
read()ing and write()ng along with select()s to handle
non-stagnation of this type of communications.

> I appreciate the help.

/Matti Aarnio

  reply	other threads:[~2003-07-25 18:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-24 23:28 Andrew Barton
2003-07-25 13:02 ` Doug McNaught
2003-07-25 15:27 ` Andries Brouwer
2003-07-25 10:59   ` Andrew Barton
2003-07-25 18:46     ` Matti Aarnio [this message]
2003-07-25 12:53       ` Andrew Barton
2003-07-25 20:10     ` Andries Brouwer

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=20030725184635.GC6898@mea-ext.zmailer.org \
    --to=matti.aarnio@zmailer.org \
    --cc=aebr@win.tue.nl \
    --cc=andrevv@users.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: forkpty with streams' \
    /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

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).