LinuxQuestions.org
Visit Jeremy's Blog.
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 02-24-2010, 04:37 AM   #1
dearvivekkumar
LQ Newbie
 
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23

Rep: Reputation: 15
fork(), variable values between child and parent process????


Code:
int main()
{
int pi;
int i=10;
pid=fork();
if(pid==0)
{
printf("The initial value of the variable i in the child process is %d\n"i);
 i+=10;
printf("value of varible  in child process after incrementation is %d\n",i);
printf('cheil terminated\n");
}
else
{
wait(0)
printf("value of i in parent process is %d\n',i);
}
}

Q 1. The value of the variable pid returned by the fork() function will be greater than 0 in the parent process and equal to zero in the child process?
but during forking, there values are exactly copied so what's went wrong here?

Q 2. "changes to the variable in one process is not reflected in the other process" why it is so???
>> Even if we have variable i declared as a pointer or a global it wont make a difference.


Code:
int main()
{
int i, pid;
i=10;
printf("before fork i is %d\n",i);
pid=fork();
if(pid==0)
{
printf("in child i's address is %u\n",&i);
i=20;
printf("i is 5d\n",i);
}
else
{
printf("In pareent i's address is %u\n",&i);
printf("i is %d\n",i);
}
}
Q. Through this program it is clear that both process is using the data from the same location, so where's the original value is residing when the child process is in execution.?

what is the reason, please reply. I am trying of learning unix progrmming.So, it may be a easy one for the proficient ones. so please help me out.

thanking in anticipation!
 
Old 02-24-2010, 04:49 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,732
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
The value of the variable pid returned by the fork() function will be greater than 0 in the parent process and equal to zero in the child process?
but during forking, there values are exactly copied so what's went wrong here?
I tried your program like this:
Code:
#include <stdio.h>
int main()
{
	int pi;
	int i   = 10;
	int pid = fork();
	
	if (pid == 0)
	{
		printf ("\npid : child %d\n", pid);
	}
	else
	{
		wait(0);
		printf ("\npid : parent %d\n", pid);
	}
}
I compiled the program and got following output:
pid : child 0

pid : parent 14415


So they are different, what's the problem ?

Have I understood your question wrongly ?

Quote:
"changes to the variable in one process is not reflected in the other process" why it is so???
The child process now has a different address space so the variable changes will not be reflected !

Quote:
Through this program it is clear that both process is using the data from the same location, so where's the original value is residing when the child process is in execution.?
when the child process is created, 2 copies of the variable are created, parent can modify his own copy and child can modify his own copy ..

Last edited by Aquarius_Girl; 02-24-2010 at 04:54 AM.
 
Old 02-24-2010, 04:57 AM   #3
dearvivekkumar
LQ Newbie
 
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23

Original Poster
Rep: Reputation: 15
When we create any process any process it is exact duplicate of its parent. I want to know then why pid in child and parent process is different. it's different I knew after running the code but why it is so??
 
Old 02-24-2010, 05:03 AM   #4
dearvivekkumar
LQ Newbie
 
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23

Original Poster
Rep: Reputation: 15
Quote:
when the child process is created, 2 copies of the variable are created, parent can modify his own copy and child can modify his own copy ..
but they are using the same address location., i am trying to understand if they are operating on the same address then then where the parent process varible is while child is executing.

What is different "address space"?
I appreciate if you please explain this.

thank you anisha kaul
 
Old 02-24-2010, 05:06 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,732
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
When we create any process any process it is exact duplicate of its parent. I want to know then why pid in child and parent process is different. it's different I knew after running the code but why it is so??
Now I execute the following program with getpid() function:
Code:
#include <stdio.h>
int main()
{
	int pi;
	int i   = 10;
	int pid = fork();
	
	if (pid == 0)
	{
		printf ("\npid : child %d %d\n", pid, 	getpid());
	
	}
	else
	{
		wait(0);
		printf ("\npid : parent %d %d\n", pid, 	getpid());
	}
}
Output:
pid : child 0 14563

pid : parent 14563 14562


See closely,

Parent has received child's pid, i.e. 14563
and Parent's own pid is : 14562

Last edited by Aquarius_Girl; 02-24-2010 at 05:09 AM.
 
Old 02-24-2010, 05:11 AM   #6
dearvivekkumar
LQ Newbie
 
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23

Original Poster
Rep: Reputation: 15
thaks mam!
i have got it.
 
Old 02-24-2010, 05:18 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,732
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
but they are using the same address location., i am trying to understand if they are operating on the same address then then where the parent process varible is while child is executing.

What is different "address space"?
They are not operating on the same address space, When a child process is created it copies the parents address space and thus now there exist 2 different address spaces.

For a very very basic understanding of address space see this link : http://www.uniforum.chi.il.us/slides/oracle/sld021.htm
 
  


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
fork() // parent & child communication sudif Programming 5 05-02-2007 03:11 AM
How to share the same variable between parent and child process? zwylinux Programming 6 03-26-2007 02:47 AM
GCC fork() - Can a child change a parent variable? johnhardey Programming 4 03-10-2005 10:49 AM
Getting a parent to communicate with its child -- fork() kamel Programming 3 06-02-2004 03:04 AM
child-parent-&-fork mukul Programming 2 03-24-2001 01:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 AM.

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
Open Source Consulting | Domain Registration