LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2015, 12:11 PM   #1
anuragccsu
LQ Newbie
 
Registered: Jun 2009
Posts: 14

Rep: Reputation: 0
Smile Fork does'nt duplicate the process address space


Hi all,
I am trying a fork program and my expectation is that when i write the process address space in child, their address should be differ but it's happening partially for below program:
-------------------------------------------
int main()
{
pid_t pid;
char *message;
int n;
printf("fork program starting\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the child";
n = 5;
printf ("PID = %d the addresses in child = %p and %p ", getpid(), &n, message);
break;
default:
message = "This is the parent";
n = 3;
printf ("PID = %d the addresses in parent = %p and %p ", getpid(), &n, message);
break;
}
for(; n > 0; n--) {
puts(message);
sleep(3);
}
exit(0);
}
-------------------------------------------
o/p:
fork program starting
PID = 5618 the addresses in parent = 0xbfaa0534 and 0x8048731 This is the parent
PID = 5619 the addresses in child = 0xbfaa0534 and 0x80486f2 This is the child
This is the parent
This is the child
This is the parent
This is the child
This is the child
garima@garima-online:~/Desktop/test/pgm$ This is the child
==========================================================

The address of n is same but message is different but why so?
Shouldn't n address also be different?

Thanks in Advance.
 
Old 08-13-2015, 04:28 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,759

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Address 12345 in process A is not the same memory location as 12345 in process B. Every process has its own virtual memory.
 
1 members found this post helpful.
Old 08-13-2015, 07:00 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
The only time they might be different is if the CPU doesn't have a memory management unit...
 
Old 08-13-2015, 11:36 PM   #4
anuragccsu
LQ Newbie
 
Registered: Jun 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by smallpond View Post
Address 12345 in process A is not the same memory location as 12345 in process B. Every process has its own virtual memory.
Thanks for your resonse, but i really didn't get it, i am Ubuntu-14.04, So could you provide me some method to verify it?
Here if i assume the address of n(12345) in Process-A is not same as n(12345) in Process-B then why did the child process had different address for "message" variable, If i assume the answer to be true then why to create a different address for "message", I mean it has to be both ways?
Sorry if i am not clear/couldn't get what you meant.
 
Old 08-14-2015, 06:04 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
Different message.

What you printed was the address of the string... And there are two different strings, so there are two addresses in both.

The only thing changed was the pointer to the string.

When the line
Code:
message = "This is the child"
was executed the variable "message" was given the address of the the byte containing the "T" from the beginning of the string "This is the child".

In the parent the line
Code:
message = "This is the parent";
was executed the variable "message" was given the address of the the byte containing the "T" from the beginning of the string "his is the parent".

A different string, a different address.

Then you formatted the contents of the pointer variable message.

The same thing happens when you print integers.

An introduction to how memory management works can be read at:

https://www.cs.rutgers.edu/~pxk/416/...09-memory.html

The specific part is under "Page-based virtual memory", but the preceding parts will help understanding why it is used.
 
1 members found this post helpful.
Old 08-14-2015, 10:27 AM   #6
anuragccsu
LQ Newbie
 
Registered: Jun 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by smallpond View Post
Address 12345 in process A is not the same memory location as 12345 in process B. Every process has its own virtual memory.
Yea, you're correct, so does it mean one cannot debug two such processes? because GDB can only catch virtual addresses! or may be i need to read more about GDB manuals about multiprocessing/multithreading, any such good link?
 
Old 08-14-2015, 12:21 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
Multi-threading is different that multi-processing; specially on Linux.

A Linux multi-threaded process uses shared memory between the processes; so some data addresses will be common between two (or more) threads. The unique address ranges are on the stack (and that "unique" is caused by the MMU translating the stack addressing to different physical memory).

Multi-processing just means there are multiple CPU cores available to process things in parallel.
 
1 members found this post helpful.
Old 08-14-2015, 12:33 PM   #8
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,759

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
gdb runs in the context of a process, so any command will use that process' virtual addresses.
 
Old 08-14-2015, 12:41 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
Quote:
Originally Posted by smallpond View Post
gdb runs in the context of a process, so any command will use that process' virtual addresses.
Actually, gdb runs in its own context, but has access to alternate process contexts via the ptrace system call
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Protection of Process Address Space entz General 5 06-06-2011 04:47 PM
shared memory address area in process address space nagaraju1024 Programming 3 02-22-2011 04:49 AM
How does processor know about a process's address space nitinarora Linux - Kernel 2 07-09-2010 04:23 PM
fork() address space copying polymath Programming 7 05-19-2010 01:55 PM
accessing process address space linorg Programming 5 04-15-2004 01:13 PM


All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration