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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-24-2010, 04:37 AM
|
#1
|
LQ Newbie
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23
Rep:
|
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!
|
|
|
02-24-2010, 04:49 AM
|
#2
|
Senior Member
Registered: Dec 2008
Posts: 4,732
|
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.
|
|
|
02-24-2010, 04:57 AM
|
#3
|
LQ Newbie
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23
Original Poster
Rep:
|
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??
|
|
|
02-24-2010, 05:03 AM
|
#4
|
LQ Newbie
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23
Original Poster
Rep:
|
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
|
|
|
02-24-2010, 05:06 AM
|
#5
|
Senior Member
Registered: Dec 2008
Posts: 4,732
|
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.
|
|
|
02-24-2010, 05:11 AM
|
#6
|
LQ Newbie
Registered: Feb 2010
Location: noida/INDIA
Distribution: Ubuntu 9.10
Posts: 23
Original Poster
Rep:
|
thaks mam!
i have got it.
|
|
|
02-24-2010, 05:18 AM
|
#7
|
Senior Member
Registered: Dec 2008
Posts: 4,732
|
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
|
|
|
All times are GMT -5. The time now is 11:06 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|