Re: Execute an arbitrary program as a child process?
- From: Archer <champ@xxxxxxx>
- Date: Tue, 05 Feb 2008 03:15:10 -0000
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?
.
- Follow-Ups:
- Re: Execute an arbitrary program as a child process?
- From: SM Ryan
- Re: Execute an arbitrary program as a child process?
- References:
- Execute an arbitrary program as a child process?
- From: Archer
- Re: Execute an arbitrary program as a child process?
- From: SM Ryan
- Execute an arbitrary program as a child process?
- Prev by Date: Re: sigwait(&ss, SIGCONT)
- Next by Date: Re: Execute an arbitrary program as a child process?
- Previous by thread: Re: Execute an arbitrary program as a child process?
- Next by thread: Re: Execute an arbitrary program as a child process?
- Index(es):
Relevant Pages
|
|