This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Fork and read my child's output


Hello,

AH>So I want to do something similar in Guile. fork(), then in the child
AH>process, sanitize environment and exec(). In the parent process, read
AH>the child process stdout stream and process it. Just like
AH>open-input-pipe, but safe from shell escapes.

PN>Even in perl, you can do this by opening a pipe... You should be able to
PN>reverse it so the parent reads from the child's stdout instead of making
PN>it writeable.

Ah, of course, the pipe() primitive. Using the Perl magic form of open for
so long had left me befuddled as to these low-level methods. However, I
have hit a strange brick wall. I wrote a Guile test script with pipe and
primitive-fork which should have the child writing one line of text for
the parent to read. However, it aborts and core dumps.

Attached are two files: pipes.scm (the Guile test script to do this) and
pipes.pl (the Perl version of this, which works fine).

Again, the expected behavior:

    Process forks.
    Parent process listens for output from child process.
    Child process writes "(child pid X is sending this)" message,
        for the parent to read; then the child process exits.
    Parent reads the message.

The only functional difference between the Perl code and the Guile code is
that in the Perl code I disable output buffering on the write handle (the
lexical block where I set $| = 1 on line 10). I cannot for the life of me
figure out what is going wrong on the Guile version. Any help would be
vastly appreciated. Thanks in advance--

Humbly,

Andrew

----------------------------------------------------------------------
Andrew Ho               http://www.tellme.com/       andrew@tellme.com
Engineer                   info@tellme.com          Voice 650-930-9062
Tellme Networks, Inc.                                 Fax 650-930-9101
----------------------------------------------------------------------

pipes.pl

#!/usr/local/bin/guile -s
!#

;; pipes.scm - demonstrate IPC with fork and pipe in Guile Scheme
;; Andrew Ho (andrew@tellme.com)

(let* ( (connect-pipe (pipe))
        (reader-pipe  (car connect-pipe))
        (writer-pipe  (cdr connect-pipe))
        (child-pid    (primitive-fork)) )

  (if (= child-pid 0)
      (begin

        (close-input-port reader-pipe)
        (display (string "inside child, pid = "
                         (number->string (getpid)) #\newline ))
        (display (string "(child pid "
                         (number->string (getpid))
                         " is sending this)\n" )
                 writer-pipe )
        (close-output-port writer-pipe) )

      (begin

        (close-output-port writer-pipe)
        (display (string "inside parent, pid = "
                         (number->string (getpid))
                         ", child pid = "
                         (number->string child-pid) #\newline ))
        (display (string "reading from child: " (read-line reader-pipe)))
        (close-input-port reader-pipe)
        (waitpid child-pid 0) )))

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]