[LCP]A small doubt on fork

Andrew Weaver Andrew.Weaver at tecnomen.fi
Fri Oct 19 22:22:30 UTC 2001


Yes, the child represents a copy of the parent - data and most other things.
The addresses of variables that are defined within the code body are
expressed as offsets from the load point of the code as a whole. So, since
the variables are at the same place in each chunk of code, they will have
the same offset.

The clearly cannot be the same physical memory location since, as your code
shows, you can change the value of the child's version of the variable
without affecting the parent's version.

Apart from the very early days, this technique of "Base/Offset" addressing
has been used to facilitate relocatable code in multi-processing systems.
The Base Address being the physical address of the first word of the process
and all other addresses within the code being offsets from that base. So,
the real, physical address is (Base + Offset).

Your compiler should have an option to generate the assembler into a file.
Print it and have a look.

PS. The name's Weaver :-) :-) 

> -----Original Message-----
> From:	sasidhar p [SMTP:psasidhar at hotmail.com]
> Sent:	Friday, October 19, 2001 2:57 PM
> To:	linuxcprogramming at lists.linux.org.au
> Subject:	RE: [LCP]A small doubt on fork
> 
> Hi,
>   I do agree that the process's address space is copied over the fork to 
> child, so the address of i in child process should be different....
> 
> May be I will agree with Andrew Webber that the addresses are relative...
> 
> Can somebody clarify me this point...
> 
> I am reproducing a diagram of Typical memory management from steven
> advanced 
> programming in the unix environment...p168
> 
> ------------------------------
> high address(command line args
> and environ variables)
> ------------------------------
> 
> stack
> 
> ..............................
> 
> 
> 
> 
> 
> ..............................
> 
> Heap
> 
> ------------------------------
> uninitialized data(bss)
> (initialized to zero by exec)
> ------------------------------
> initialized data
> ------------------------------
> Text
> 
> Low address
> ------------------------------
> 
> Question : Now what portions of this does the child inherit when a call to
> 
> fork is made...
> 
> I believe an entire new copy of the above is made for the child process
> and 
> hence my argument saying that the address of i in child process should be 
> different as it is in a different location...
> 
> May be Mr.Andrew Webber is right saying that the address of i is relative
> to 
> this child process. Then chances are more that you get the same address
> both 
> in parent and child... Is this argument correct??
> 
> Thanx,
> Sasi.
> 
> 
> 
> 
> 
> 
> 
> 
> >From: "Bradley, Peter" <PBradley at uwic.ac.uk>
> >Reply-To: linuxcprogramming at lists.linux.org.au
> >To: "'linuxcprogramming at lists.linux.org.au'" 
> ><linuxcprogramming at lists.linux.org.au>
> >Subject: RE: [LCP]A small doubt on fork
> >Date: Fri, 19 Oct 2001 10:12:18 +0100
> >
> >Hi,
> >
> >I think what the enquirer needs to know is that a process's address space
> 
> >is
> >copied over the fork.
> >
> >man fork says:
> >
> >       The fork() system call causes the creation of a new process. The
> new
> >
> >       child process is created wth exactly one thread or lightweight
> >
> >       process. The new child process contains a replica of the calling
> >
> >       thread (if the calling process is multi-threaded) and its entire
> >
> >       address space, possibly including the state of mutexes and other
> >
> >       resources.
> >
> >(my emphasis)
> >
> >HTH
> >
> >Peter
> >
> >  -----Original Message-----
> >From: 	Asif Raj [mailto:asif at gdit.iiit.net]
> >Sent:	19 October 2001 09:53
> >To:	linuxcprogramming at lists.linux.org.au
> >Subject:	Re: [LCP]A small doubt on fork
> >
> >It is because when the execution of the program begins, variable i is
> >initialised and assigned a particular address in the memory. when you
> >change the value of i, the value contained at that memory location
> changes
> >but the program still refers to variable i through that memory location
> >only.
> >
> >cheers
> >asif
> >
> >On Fri, 19 Oct 2001, sasidhar p wrote:
> >
> > > Hi,
> > >    The result of the following program is surprising me...
> > >
> > > main()
> > > {
> > >     int pid;
> > >     int i = 5;
> > >
> > >     switch (pid = fork()) {
> > >          case 0 : {
> > >                         i = 7;
> > >                         printf("In child address of i = %p \n",&i);
> > >                       } break;
> > >          case -1 : printf("Fork error \n"); break;
> > >          default : printf("In parent address of i = %p\n",&i);
> > >     }
> > > }
> > >
> > > Out put on my m/c:
> > >
> > > In parent address of i = effffa68
> > > In child address of i = effffa68
> > >
> > > Problem : I was expecting that the addresses shold be different
> because
> >fork
> > > creates a new process.  But the o/p is both addresses are same.
> > >
> > > Question: Why is this so?
> > >
> > > Thanx,
> > > Sasi.
> > >
> > >
> > > _________________________________________________________________
> > > Get your FREE download of MSN Explorer at 
> >http://explorer.msn.com/intl.asp
> > >
> > > _______________________________________________
> > > This is the Linux C Programming List
> > > :  http://lists.linux.org.au/listinfo/linuxcprogramming List
> > >
> >
> >_______________________________________________
> >This is the Linux C Programming List
> >:  http://lists.linux.org.au/listinfo/linuxcprogramming List
> >_______________________________________________
> >This is the Linux C Programming List
> >:  http://lists.linux.org.au/listinfo/linuxcprogramming List
> 
> 
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
> 
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List



More information about the linuxCprogramming mailing list