473,792 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fork(): Child Memory from Parent

2 New Member
On a HPUX, a CPP program. In main() defined a variable as int x = 900, called fork(), changed value of x as 89 in child process. Printed the address of x and value of x in child process and also in the parent process after wait(0).

Doubt
--------
Both in child and parent process, the address of x (&x) was same.
Since the address of x is same, the value shows different in parent and child.
Could you please reply. I got confused.


int main()
{

int x = 900;
int *ptr = &x;
cout << "\n &x x " <<&x<< " " << x; OUTPUT=> 0x7b03a490 900

if (fork())
{
cout << "\n &x x " <<&x<< " " << x; OUTPUT=> 0x7b03a490 900
wait(0);
cout << "\n walk up parent";
cout << "\n &x x " <<&x<< " " << x; OUTPUT=> 0x7b03a490 900
return(0);
}
else
{
x = 89;
cout << "\n &x x " <<&x<< " " << x; OUTPUT=> 0x7b03a490 89
exit(0);
}
cout << "\n parent is here exit";
return(0);
}
Jun 8 '07 #1
3 2431
mac11
256 Contributor
when you fork() each process gets it's own copy of everything. If you want to have the parent process see changes to variables that the child process makes (or vice-versa) you will have to use shared memory.
Jun 9 '07 #2
bp4444
2 New Member
when you fork() each process gets it's own copy of everything. If you want to have the parent process see changes to variables that the child process makes (or vice-versa) you will have to use shared memory.

My question is though the parent and child process print the same address (&x) of the variable x, shows different values in parent and child.
Jun 9 '07 #3
JosAH
11,448 Recognized Expert MVP
My question is though the parent and child process print the same address (&x) of the variable x, shows different values in parent and child.
That's what virtual memory is all about; address 42 for your process doesn't
refer to the same real memory location as my process's address 42.

kind regards,

Jos
Jun 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2808
by: Yin | last post by:
I am writing a script that monitors a child process. If the child process dies on its own, then the parent continues on. If the child process is still alive after a timeout period, the parent will kill the child process. Enclosed is a snippet of the code I have written. For some reason, unless I put in two time.sleep(4) commands in the parent, the process will not sleep. Am I forgetting something? Any reasons for this strange...
6
2399
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
16
5811
by: mishra | last post by:
Hi, i thied the following code.. # include<stdio.h> int main() { int a; printf("Hello..."); a=fork(); printf("hi\n"); return(0);
27
5992
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork(); value = value + 2; number = number + 2;
5
1947
by: mutley | last post by:
for(i=1;i<CONFIG_POLLER_FORKS;i++) { if((pid = fork()) == 0) { server_num=i; break; } else { pids=pid;
2
2163
by: Poly-poly man | last post by:
I am working on a GTK Midi player (timidgtk.sourceforge.net). As you can see, it's just a timidity frontend. With version 0.03, I'm trying to devel it to use SDL_sound to play the midis. First off, is there a better library for this? Right now I'm writing a test program to play on console, just so that I can narrow problems down very quickly. I have a "sound.c" (basically playsound from SDL_sound, but without a lot of features. I added...
3
2722
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void main() { .... pid1 = fork(); if (pid1) pid2 = fork(); .....
5
12236
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I recently explained it to a friend of mine and at that time decided to see if I could replicate it in C#. I have created an application that, to me at least, mimics what fork would/should do on *nix/bsd systems. I know that fork spawns a new...
2
4244
by: Radz | last post by:
when the fork system call is executed, a new process is created. The original process is called the parent process whereas the new process is called the child process. The new process consists of a copy of the address space of the parent. On success, both processes continue execution at the instruction after the fork call, with one difference, the return code for the fork system call is zero for the child process, while the process identifier of...
0
9670
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.