Re: Execute an arbitrary program as a child process?



SM Ryan <wyrmwif@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Archer <champ@xxxxxxx> wrote:

----snip----

# I'm looking for a way to execute an arbitrary program and watch
# its stdout and sdterr output. I've looked at of using fork(),
# exec() and pthreads to start a process which will execute the
# program and return, but I just can't see any way of getting
# the executed program's PID or seeing its output.

Use pipe() to create pipes that will replace stdin, stdout,
and stderr.

int pid = fork()

if (pid==0)
you are the child process
dup pipes to replace various fds 0, 1, 2
close the original pipe fds
set up any other aspect of the execution environment
exec the new program
if (pid>0)
you are the parent and pid is the child
close unused ends of pipes
write to child's stdin
and read from child's stdout and stderr
close pipes on eof
wait for child status

Ah. Nice and clean. Mille gracias.

You have to child pid for a kill call.

I don't understand that. Are you saying that I have [a] child
pid for a kill all?

If you have two or more pipes with blocking I/O in the same
thread, you are risking deadlock unless you're very careful
how you read and write. Alternatively each pipe has it own
thread, or do non-blocking I/O, possibly with select().

As with watching both stderr and sdtout?

# (Well, I do see a possibility to redirect its output to a file
# and read that, but that seems to me a very untidy hack, with

/tmp is intended for temporary files. Those you forget to unlink
are removed on the next reboot.

I will be using files for input to play and madplay, but I'll
be using /dev/shm for that (and for audacity's tmp files too),
and deleting them after use (and umounting /dev/shm on exit).

That reminds me: what test can I install to choose /tmp for
users who aren't in /etc/sudoers for mount and umount?
.



Relevant Pages

  • Re: c++: Run process and get stdout into string-variable
    ... create two pipes with pipe, one for stdin, one for stdout. ... in the child process returns 0) dup ... In the parent process (the one where fork) returns the PID of the ...
    (comp.os.linux.development.apps)
  • Re: Execute an arbitrary program as a child process?
    ... # I'm looking for a way to execute an arbitrary program and watch ... # the executed program's PID or seeing its output. ... Use pipeto create pipes that will replace stdin, stdout, ... you are the parent and pid is the child ...
    (comp.unix.programmer)
  • Question about pipes and terminals
    ... the user should be able to use the shell normally. ... that the child has finished directly it all worked well. ... pipes to redirect stdin/stdout/stderr to the executable. ... Now when i execute a shell, ...
    (comp.os.linux.development.system)
  • Re: should these be fixed for python 2.4?
    ... > stdout and stderr as strings and the exit status. ... but either you use disk files or it's more work. ... I imagine is due to limitations of pipes' ... You're trying to execute the whole ...
    (comp.lang.python)
  • Re: Change redirection of stdout of child process
    ... >> I do not want to redirect stdout to a pipe which is handled by the ... >> The parent acts as debugger an gets regularly control over the child. ... >> Maybe it would be possible to inject a thread into the childs process ... > Your first course of action is to use pipes - you really should test this ...
    (microsoft.public.win32.programmer.kernel)