Results 1 to 2 of 2

Thread: Recieving output from spawned Process

  1. #1

    Thread Starter
    Lively Member Brandito's Avatar
    Join Date
    Nov 2000
    Location
    Here, There, Every Where!
    Posts
    106

    Recieving output from spawned Process

    I have a question that has two parts. I am trying to spawn a process of a terminal app and wait for it to finish and returns its output.

    So, for example I want to spawn a process that runs "ls -la" and puts the output into a string variable. I am doing this for windows and linux. I have been working on the linux code. Right now I am fork()ing, exec()ing, then wait()ing. I just don't know how to capture the output of the child process.

    My second problem is that when I do a wait()... I need some kind of timer on it so I don't get deadlock in my parent.

    Does anyone have any good links or code examples for this?

    Thanks.
    Master of Cyber Fu - A Temple of Digital Chi

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Recieving output from spawned Process

    You manipulate the file descriptors to capture output. Basically, you create a pipe with pipe(). Then you fork. Then you close the child's standard output and call dup() on the write end of the pipe. The new file descriptor will have the same number as stdout and thus the process will write to the pipe. Alternatively, you can use dup2() to do this in one step. Now close the read end and the original write end of the pipe.
    In the parent, you close the write end of the pipe and receive child data from the read end.

    I'm not aware of any way of giving the wait a timeout. I don't really see any need for it, either. Why would the child never die?

    Finally, what's the purpose of the project? System commands are really, really unportable, and they're slow, too, so unless you're writing a shell-like program, it's probably a bad idea to use them.

    There is also Boost.Process, a Google SoC project that, a few weeks ago, presented a preliminary but functional version.
    http://www.crystalclearsoftware.com/...l?BoostProcess
    Boost.Process handles process creation and I/O redirection in a cross-platform way, allowing you to use the same source for both Windows and POSIX.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width