questions on fork()

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

questions on fork()

Edwin Eyan Moragas
ey misc,

from the fork(2) man pages:

fork() causes creation of a new process.  The new process (child process)
is an exact copy of the calling process (parent process) except for the
following: <snip>

i have several questions/clarifications regarding this.

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?

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?

thank you for your patience.

--
salud,
eyan

Reply | Threaded
Open this post in threaded view
|

Re: questions on fork()

Bernd Schoeller
On Tue, Jul 04, 2006 at 03:27:17PM +0800, Edwin Eyan Moragas wrote:

> ey misc,
>
> from the fork(2) man pages:
>
> fork() causes creation of a new process.  The new process (child process)
> is an exact copy of the calling process (parent process) except for the
> following: <snip>
>
> i have several questions/clarifications regarding this.
>
> 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).

> 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.

Bernd

Reply | Threaded
Open this post in threaded view
|

Re: questions on fork()

Gilles Chehade
In reply to this post by Edwin Eyan Moragas
On Tue, 4 Jul 2006 15:27:17 +0800
"Edwin Eyan Moragas" <[hidden email]> wrote:

> ey misc,
>
> from the fork(2) man pages:
>
> fork() causes creation of a new process.  The new process (child
> process) is an exact copy of the calling process (parent process)
> except for the following: <snip>
>
> i have several questions/clarifications regarding this.
>
> 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?
>

The child process gets an exact copy of the memory allocated by the
parent process. A value available at a particular address in parent
is available at the same address in child, except that it will be a
copy and that modification in one of the processes won't affect the
other.


> 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?
>

If the parent process open()-s a file then fork()-s, the child will
also get a copy of the file descriptor on which it can perform same
operations as parent (read/write depending on open() flags).