Re: Invoking external processes in threaded program



In article
<afcfa511-0d53-4ea8-a7c8-954e5b3d8d16@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Edd <edd@xxxxxxxxxxxxxxxx> wrote:

Hi folks,

I'm trying to write a high level library to invoke and communicate
with an external process. On UNIX, this implies a fork-and-exec with
some pipes (or pseudo-terminals as I learned in another thread [1]).

Now, ideally I would like to be able to start multiple processes in
different threads, but the general advice with respect to fork()ing in
a multithreaded program seems to be that one shouldn't do anything in
the child other than exec*() immediately. I can see why after thinking
about it, but I didn't see this particular trap coming... :)

So now I can't see a way to set up my communication channels in the
child e.g. calling dup2() for the standard stream descriptors and
close()ing the un-used ends of the pipes.

I don't think there's any problem with doing these things in the child
process.

When you fork, only the thread that called fork() is duplicated, so you
don't have to worry about other threads continuing to do things before
the exec().

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.



Relevant Pages

  • Re: Invoking external processes in threaded program
    ... some pipes. ... the child other than exec*immediately. ... When you fork, only the thread that called forkis duplicated, so you ... is locked before the fork by a thread other than the forking thread, ...
    (comp.unix.programmer)
  • Re: Invoking external processes in threaded program
    ... some pipes. ... the child other than exec*immediately. ... When you fork, only the thread that called forkis duplicated, so you ... is locked before the fork by a thread other than the forking thread, ...
    (comp.unix.programmer)
  • Re: Invoking external processes in threaded program
    ... some pipes. ... the child other than exec*() immediately. ... The child is a single-threaded process so that it can fork and exec ...
    (comp.unix.programmer)
  • 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)
  • =?iso-8859-1?q?Re:_The_missing_Jo=E3o_Reis_pipes...?=
    ... who purchased the pipes. ... investigation into this matter. ... purchasing insurance on a valuable item that he has purchased. ... prefer to eat my socks than communicate with the importer. ...
    (alt.smokers.pipes)