How to open a pipe to a new process?

How to open a pipe to a new process?

Post by Derekasaurus Re » Sat, 21 Jul 2001 01:59:49



Hello,

How do I go about opening a pipe to a new process? What I would
like to write a program that monitors another program. Ideally,
I would like the parent program to start the child monitor program
and then write data to it (likely via pipe), which the monitor
program would consume and use to update it's progress display.

How do I create a pipe from a parent process to a child process
that the parent starts? I don't think I can use fork() and
pipe() because my parent process uses lots of memory. As I
understand it, calling fork() gives the child process a copy of
the parent's heap, which is not a realistic option given that my
program eats as much memory as I can feed it. I think popen()
also uses fork(), which as I said before probably isn't a good
idea in my case.

Any hints that might point me in the right direction would be
tremendously appreciated.

Thanks.

Derek

 
 
 

How to open a pipe to a new process?

Post by Arthur H. Gol » Sat, 21 Jul 2001 03:09:19



> Hello,

> How do I go about opening a pipe to a new process? What I would
> like to write a program that monitors another program. Ideally,
> I would like the parent program to start the child monitor program
> and then write data to it (likely via pipe), which the monitor
> program would consume and use to update it's progress display.

> How do I create a pipe from a parent process to a child process
> that the parent starts? I don't think I can use fork() and
> pipe() because my parent process uses lots of memory. As I
> understand it, calling fork() gives the child process a copy of
> the parent's heap, which is not a realistic option given that my
> program eats as much memory as I can feed it. I think popen()
> also uses fork(), which as I said before probably isn't a good
> idea in my case.

While it is true that a `fork()-ed' process _conceptually_ receives a
copy of its parent's address space, this is normally implemented as
copy-on-write, i.e. pages are physically copied only when written to by
either parent or child.
Of course, once the child `exec*()'s, it's moot anyway.

If I understand what you're trying to do correctly (i.e. it's a one-way
pipe), popen() would be the simplest way to go. Just `popen()' in write
mode and have the monitor read from stdin.

HTH,
--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
If it's of it it's its.

 
 
 

How to open a pipe to a new process?

Post by Derekasaurus Re » Sat, 21 Jul 2001 05:23:44


Quote:"Arthur H. Gold" wrote...

<snip>

Quote:> While it is true that a `fork()-ed' process _conceptually_
> receives a copy of its parent's address space, this is normally
> implemented as copy-on-write, i.e. pages are physically copied
> only when written to by either parent or child. Of course, once
> the child `exec*()'s, it's moot anyway.

And that's exactly the problem.

Quote:> If I understand what you're trying to do correctly (i.e. it's a
> one-way pipe), popen() would be the simplest way to go. Just
> `popen()' in write mode and have the monitor read from stdin.

And while that's the simplest solution, it doesn't get around
the forking-a-huge-process problem. I could try to guarantee that
popen() is called relatively early (i.e., before the parent
process allocates all its memory), but perhaps I'm just better
off using pthreads or a messaging API.

D.

 
 
 

How to open a pipe to a new process?

Post by Barry Margoli » Sat, 21 Jul 2001 06:06:11




>"Arthur H. Gold" wrote...

><snip>

>> While it is true that a `fork()-ed' process _conceptually_
>> receives a copy of its parent's address space, this is normally
>> implemented as copy-on-write, i.e. pages are physically copied
>> only when written to by either parent or child. Of course, once
>> the child `exec*()'s, it's moot anyway.

>And that's exactly the problem.

But it's *not* a problem.  Since the child process immediately calls
exec(), it never modifies most of those shared pages.  Its memory is
instantly replaced with the program being exec'ed.

Quote:>> If I understand what you're trying to do correctly (i.e. it's a
>> one-way pipe), popen() would be the simplest way to go. Just
>> `popen()' in write mode and have the monitor read from stdin.

>And while that's the simplest solution, it doesn't get around
>the forking-a-huge-process problem.

But it's not a real problem.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to open a pipe to a new process?

Post by Arthur H. Gol » Sat, 21 Jul 2001 06:22:15



> "Arthur H. Gold" wrote...

> <snip>

> > While it is true that a `fork()-ed' process _conceptually_
> > receives a copy of its parent's address space, this is normally
> > implemented as copy-on-write, i.e. pages are physically copied
> > only when written to by either parent or child. Of course, once
> > the child `exec*()'s, it's moot anyway.

> And that's exactly the problem.

> > If I understand what you're trying to do correctly (i.e. it's a
> > one-way pipe), popen() would be the simplest way to go. Just
> > `popen()' in write mode and have the monitor read from stdin.

> And while that's the simplest solution, it doesn't get around
> the forking-a-huge-process problem. I could try to guarantee that
> popen() is called relatively early (i.e., before the parent
> process allocates all its memory), but perhaps I'm just better
> off using pthreads or a messaging API.

Actually you've missed the point. The function popen() uses vfork(),
which, as stated above, is an optimization of fork() _for exactly the
problem you mention_.

There is no `vforking-a-huge-process' problem.

HTH,
--ag

--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
This space for rent.

 
 
 

How to open a pipe to a new process?

Post by Derekasaurus Re » Sat, 21 Jul 2001 07:53:24


<snip>

Quote:> Actually you've missed the point. The function popen() uses vfork(),
> which, as stated above, is an optimization of fork() _for exactly the
> problem you mention_.

> There is no `vforking-a-huge-process' problem.

Appologies. I was being a space cadet when I wrote my reply. Everything
makes sense now. A million thanks.
 
 
 

How to open a pipe to a new process?

Post by Arthur H. Gol » Sat, 21 Jul 2001 08:10:13




> <snip>

> > Actually you've missed the point. The function popen() uses vfork(),
> > which, as stated above, is an optimization of fork() _for exactly the
> > problem you mention_.

> > There is no `vforking-a-huge-process' problem.

> Appologies. I was being a space cadet when I wrote my reply. Everything
> makes sense now. A million thanks.

...and all without the application of a clue-stick! ;-)

Cool.

Now go forth and write code!

Cheers,
--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
"I wonder why I wonder why I wonder why..." -- S. Ulam

 
 
 

1. "could not create pipe for forking process: too many files open"

Hello,

I'm getting quite a few of these messages in the system log:

     "could not create pipe for forking process: too many files open"

What's strange about it (to me, anyways) is that I've really got not
much going on except a Web server serving static pages (and very few
hits at that). This is the Apache 1.3.12 that was default installed with
the install of Red Hat Linux version 7. I'm using a Dell PowerEdge 1300

Any clues?

-- Richard

2. D/L Linux files using Windows 98/NT

3. Why can't my child process open a named pipe?

4. fsp + term ?

5. Voodoo3, How? What? etc

6. problem opening multiple pipes with pipe(2) in 2.4.1[78]

7. old problem. new answers?

8. map a process open file to a open socket ?

9. dump pipe gzip pipe ssh pipe dd... blocksize?

10. How can a process tell if another process has a file open?

11. how to open a new term from an open one and carry the settings

12. konqueror/kmail open (in new window) opens local tmp copy