It is currently Tue, 30 Nov 2021 18:18:34 GMT

Author Message
 fork() and grabbing stdout / stdin of child
Can anybody help me with a tricky problem that I am having using fork().

What I want to be able to do is fork() a child process, and have the
parent process attach to the stdout / stdin of the child, similar to
the way that 'expect' works, i.e.

           +--------+   (parent can read child stdin)       +-------+
           | parent |<<----------------------------------->>| child |
           |        |                                       |       |
           |        |<<----------------------------------->>|       |
           +--------+   (parent can write child stdout)     +-------+

Does anybody know the best way to achieve this, so that the following
code will work?

Where and how should the file desciptionr 'child_stdout_fd' and
'child_stdin_fd' be set?

        int child_stdout_fd,

        pid = fork ();
        if (pid == 0)
                 * child process

                 * parent process.  Wait on child.

                 char frombuf [BUFSIZ];
                 char tobuf [BUFSIZ];

                 while (wait (&status, pid) != pid)
                         * Perform interactrive I/O with child
                         read (child_stdout_fd, frombuf, BUFSIZ);

                         * Prepare a suitable response in 'tobuf'
                         write (child_stdin_fd, tobuf, BUFSIZ);

Any help / pointers/ tips/ advice is very much appreciated.


John Birtley.

 Fri, 31 Jul 1998 03:00:00 GMT   
 fork() and grabbing stdout / stdin of child
(Example omitted)

I have a similar but extended problem: I want
the child process to exec a "cp -i file1
file2". If "file2" exists, then "cp" will ask
on stderr "overwrite ?".

The problem is that "cp" first checks whether
stdin is a tty device (with "isatty()", I've
checked the sources) and it only asks
"overwrite ?" in that case. After doing the
typical pipe-fork-dup, stdin is not a tty
anymore, and "cp" will just overwrite "file2"
without asking.

Any solution? All help appreciated.

--- Manuel Panea <>

 Mon, 03 Aug 1998 03:00:00 GMT   
 fork() and grabbing stdout / stdin of child
In article <>,
Manuel Panea  <> wrote:

Option #1: use a pseudo-tty to do your communication.
Option #2: preflight the cp command, and don't call it if the user doesn't
  want to overwrite the file (you do the asking).
Option #3: replace cp with something that doesn't do isatty().
Option #4: don't disconnect fd 0 from the keyboard.  What's the pipe for?

#include <stdio.h>
#include <sys/types.h>

    pid_t pid;

    pid = fork();
    if (pid < 0) {
    if (!pid) {
        /* child */
        execl("/bin/cp", "/bin/cp", "-i", "file1", "file2", 0);
    } else {
        /* parent */
        waitpid(pid, NULL, 0);


mgate 529 /home/akbar/fadden% touch file1 file2
mgate 530 /home/akbar/fadden% ./foo
overwrite file2? n
mgate 531 /home/akbar/fadden%

-- (Andy McFadden)    [These are strictly my opinions.]     PGP

      Anyone can do any amount of work provided it isn't the work he is
        supposed to be doing at the moment.        -- Robert Benchley

 Mon, 03 Aug 1998 03:00:00 GMT   
 fork() and grabbing stdout / stdin of child

on Solaris 2.4, you can attach a TTY device file to a file descriptor
(be it a socket, pipe(), ...) using the STREAM modules, ptm(7), pts(7),
and a few ioctl()'s. See the man pages, or grab a source copy from any
like telnetd, logind, etc.

Once you have attached a TTY device file to a fd retrieved from pipe()
and dup2()'d to stdin/stdout/stderr, you invoke exec() with your target
child program 'cp', which has then its stdin/stdout/stderr attached to
a (sort of) real tty.

Rob De Langhe - consultant
tel: +32.3.774.34.31

 Sat, 08 Aug 1998 03:00:00 GMT   
 fork() and grabbing stdout / stdin of child
johnb ( schrieb:

Try this:


          int inpipe[2];        /* For writing to child */
          int outpipe[2];       /* For reading from child */


                   close( inpipe[1] );
                   dup2( inpipe[0], STDIN_FILENO );
                   close( outpipe[0] );
                   dup2( outpipe[1], STDOUT_FILENO );

                    * and here the rest of the child code ...

                   close( inpipe[0] );
                   dup2( inpipe[1], STDOUT_FILENO );
                   close( outpipe[1] );
                   dup2( outpipe[0], STDIN_FILENO );

                   * now the stdin of the parent is connected to
                   * the stdout of the child and the stdout of
                   * the parent to the stdin of the child. No need
                   * special filedescriptors anymore.

But don't wait for the child before reading and writing. Just do it
simultanously. Note that input and output will block if not both sides
are ready for reading/writing.


        Andreas Karnetzki, Munich, Germany.
        Email:, CompuServe: 74001,1534

 Thu, 13 Aug 1998 03:00:00 GMT   
 fork() and grabbing stdout / stdin of child

<long code with fork(), dup2() etc. delted>

What's wrong with p2open?


 Fri, 14 Aug 1998 03:00:00 GMT   
 fork() and grabbing stdout / stdin of child
Holger_Karl <>:
 ><long code with fork(), dup2() etc. delted>
 >What's wrong with p2open?



 Fri, 14 Aug 1998 03:00:00 GMT   
   [ 7 post ] 

Similar Threads

1. redirect stdin/stdout does not for the ftp client child process

2. redirect stdout of child to stdin of parent?

3. Taking over a child process's stdin/stdout

4. Access to stdin/stdout of a child process

5. Assigning stdin and stdout of child to pipes

6. Using separate window child process for stdin/stdout

7. Redirecting stdin and stdout of child?

8. Controlling forked processes stdin/stdout

9. grabbing stdout,stderr

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software