socketpair(2) portability (was Re: questions on fork())

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

socketpair(2) portability (was Re: questions on fork())

Edwin Eyan Moragas
On 7/4/06, Bernd Schoeller <[hidden email]> wrote:
> > 1) when it says "exact copy", does this mean just a copy of the process?
> > is it right to state that the memory allocated by the parent process is not
> > accessible to the child process?
>
> Yes, copy is not the original (though normally Unix-OSs do a lazy
> copy-on-write after a fork).
>
> If you want shared memory between partent and child, have a look at
> shmat (2).

i'm actually interested in socketpair(2). and fork(2). please see below.

>
> > 2) "The child process has its own copy of the parent's descriptors." i take
> > this to mean all file and socket descriptors which both parent and child
> > can write and read to. am i correct?
>
> It means all file descriptors of the partent process at the time of
> the fork will be copied to the child process. As a result, there will
> be two processes able to write to the same file. You might have a look
> at pipe(2) to see the benefits of this.
>

pipe(2) as implemented by openbsd is capable of bidirectional traffic
according to the manpages. this is nifty but the man pages discourages
the use of the bidirectional feature. thanks for pointing me out to this.

i was thinking of using socketpair(2) to communicate between forked
processes. the question is which is better to use for more portable
apps? pipe(2) is dictated by POSIX while socketpair(2) is X/Open.

i can probably use two pipes instead of socketpair. which would be
better. your opinions would be highly appreciated.

thank you for the replies so far.

--
salud,
eyan

Reply | Threaded
Open this post in threaded view
|

Re: socketpair(2) portability (was Re: questions on fork())

Otto Moerbeek
On Tue, 4 Jul 2006, Edwin Eyan Moragas wrote:

> On 7/4/06, Bernd Schoeller <[hidden email]> wrote:
> > > 1) when it says "exact copy", does this mean just a copy of the process?
> > > is it right to state that the memory allocated by the parent process is
> > not
> > > accessible to the child process?
> >
> > Yes, copy is not the original (though normally Unix-OSs do a lazy
> > copy-on-write after a fork).
> >
> > If you want shared memory between partent and child, have a look at
> > shmat (2).
>
> i'm actually interested in socketpair(2). and fork(2). please see below.
>
> >
> > > 2) "The child process has its own copy of the parent's descriptors." i
> > take
> > > this to mean all file and socket descriptors which both parent and child
> > > can write and read to. am i correct?
> >
> > It means all file descriptors of the partent process at the time of
> > the fork will be copied to the child process. As a result, there will
> > be two processes able to write to the same file. You might have a look
> > at pipe(2) to see the benefits of this.
> >
>
> pipe(2) as implemented by openbsd is capable of bidirectional traffic
> according to the manpages. this is nifty but the man pages discourages
> the use of the bidirectional feature. thanks for pointing me out to this.
>
> i was thinking of using socketpair(2) to communicate between forked
> processes. the question is which is better to use for more portable
> apps? pipe(2) is dictated by POSIX while socketpair(2) is X/Open.

socketpair() is also in posix.

> i can probably use two pipes instead of socketpair. which would be
> better. your opinions would be highly appreciated.

Two pairs of pipes might give you better performance, but you can't
beat the simplicity of socketpair().

        -Otto