LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-11-2011, 05:18 AM   #1
pavan8085
LQ Newbie
 
Registered: May 2011
Location: Bangalore, India
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 8
Strange behaviour of fork()!!


Hi All,

Here is a simple program I ran -

#include<unistd.h>
#include<stdio.h>

unsigned a =90;

int main ()
{
int pid = fork();

if ( pid == 0 )
{
a = 44;
printf ( "Addres of child copy of a = %X and value of a = %u\n", &a, a );

}
else
{
sleep(4);
printf ( "Address of parent copy of a = %X and value of a = %u\n", &a, a );
}
while(1);
return 0;
}

The output I got was :
Addres of child copy of a = 601030 and value of a = 44
Address of parent copy of a = 601030 and value of a = 90

What is strange is that the address of 'a' is the same in both the parent and the child!! And the value of 'a' is different!! fork() will ideally make a copy of even the data segment. In order to make sure my output doesn't depend on systems which implement copy on write ( i.e. till some write modifications are made, both parent and child use same copy even after fork() ) I forcibly modified value of 'a' in child. Yet address of 'a' remains the same in both child and parent.

Can anyone shed some light on fork()'s behavior?

Thanks,
Pavan
 
Old 05-11-2011, 05:37 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
What you've observed is completely correct. Consider what would happen if `a' had a new address in either the pointer or child - then any previously obtained address of `a' would become invalid, and you simply cannot have that happening.

The reason you see this behaviour is because of virtual memory. Basically, when you request a memory address from an application program, you're actually requesting a virtual address, which is converted to a physical address by the kernel (well, actually by the CPU under direction from the kernel) which is the actual byte of RAM used. This allows two processes to maintain addresses over things like a fork() call and yet both have different copies of the variable.

See the Wikipedia article on virtual memory.


Quote:
In order to make sure my output doesn't depend on systems which implement copy on write ( i.e. till some write modifications are made, both parent and child use same copy even after fork() )
Why do you think this matters? It shouldn't (and no offence, but if you don't know about virtual memory then you're probably not writing anything where it does matter...).
 
1 members found this post helpful.
Old 05-11-2011, 06:10 AM   #3
pavan8085
LQ Newbie
 
Registered: May 2011
Location: Bangalore, India
Distribution: Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 8
Thanks a lot for the quick reply John!

Just curious, so, does that imply that systems without virtual memory support cannot fork()? How do they create multiple processes then?

Thanks,
Pavan
 
Old 05-11-2011, 07:21 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by pavan8085 View Post
Just curious, so, does that imply that systems without virtual memory support cannot fork()? How do they create multiple processes then?
Most modern systems have virtual memory support. When you specify "systems without virtual memory support" you're limiting yourself to very low-powered microcontrollers and such. These are the sort of systems that generally won't have an operating system (or will have a very small, minimalistic one). If you're writing software for one of these systems you may only have one process, or use your own (or aforementioned very small OS's) minimalistic task-switching between a predefined number of tasks. Each will probably have their own fixed areas of memory they can work in, and no memory protection.
 
  


Reply

Tags
child, fork, parent, process


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
[SOLVED] fork behaviour......what is the output of this program vivignesh Linux - Newbie 9 07-06-2010 05:11 PM
a strange fork in RHEL as4u2 edenCC Linux - Server 0 06-24-2007 08:36 PM
shutdown(2) after fork(2) behaviour changed or undefined? MScheffer Linux - Networking 3 07-15-2005 09:11 AM
Wierd behaviour using fork() and pipe() cybermix Programming 4 01-08-2004 03:44 AM
Strange behaviour of fork call in Linux Uzair Programming 1 02-12-2003 01:37 PM


All times are GMT -5. The time now is 11:00 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration