473,837 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why i value doesn't change ?

Dear all,

consider the following program

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

main()
{
int pid,*i,j;

i = &j;
*i= 10;

pid = fork();

if(pid == 0)
{
printf("\n address of i = %p",i);
printf("\n initially i value in child = %d",*i);
*i = *i +10;
printf("\n after incrementation i value in child = %d",*i);
puts("Child terminated...." );
}
else
{
wait( (int*) 0);

printf("\n address of i = %p",i);
printf("\n value of i in parent = %d",*i);
}

i am getting o/p as:-

address of i = 0xbfe6c01c
initially i value in child = 10
after incrementation i value in child = 20
Child terminated....

address of i = 0xbfe6c01c
value of i in parent = 10

the address of i is same in both parent and child process but
the change made in child process is not reflected in the
parent process why ?
Nov 25 '07
33 1821
"Marco Manfredini" <ok**********@p hoyd.netwrote in message
news:fi******** **@aioe.org...
I'm just a curios visitor, bewildered by the customs of the local natives.
It's simple: if it's not ANSI/ISO C or K&R C, it's considered off-topic
here, though most people who will give that answer also provide a reference
to a group where the question is likely to be on-topic. For POSIX-y things,
e.g. fork(), that's usually comp.unix.progr ammer.
Anyway, I could point out that this question *is* on-topic, because
the OP found an external library function which seems to undermine C's
memory model *completely*, an inexplicable
behavior.
It's only inexplicable if that external library function is in conforming
ISO C. Of course, since ISO C does not permit fork() to do what it does,
that's obviously not correct. Specifically, ISO C does not acknowledge that
multiple programs can be running at the same time, therefore it has _no_
model of how the behavior of one program can (or can't) affect the behavior
of another running concurrently.

<OT>However, I find fork() an interesting example, because the program
before the call and the two programs after the call appear to be conforming;
they simply have two independent instances of the ISO C memory model,
initially inherited from the single initial instance but potentially
diverging. Without other non-portable interactions (e.g. IPC), they do not
interact in any way that is detectable by a conforming program.</OT>

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

Nov 27 '07 #21
<so**********@g mail.comwrote in message
news:7f******** *************** ***********@e23 g2000prf.google groups.com...
#include<unistd .h>
<unistd.hisn' t ISO C; it's probably POSIX, which means you'll get better
answers in comp.unix.progr ammer.
i am getting o/p as:-

address of i = 0xbfe6c01c
initially i value in child = 10
after incrementation i value in child = 20
Child terminated....

address of i = 0xbfe6c01c
value of i in parent = 10

the address of i is same in both parent and child process but
the change made in child process is not reflected in the
parent process why ?
<OT>
The short answer is that the parent and child each have their own copy of i
after fork() returns. That means you can change one but the other will stay
the same. On most modern machines, they will have the same virtual address
(what you see), but that's just a trick of the OS; they will have different
physical addresses*. You can't compare pointers from different processes,
since they're in different virtual address spaces.
</OT>

S

* Well, at least after one is modified. Some OSes use "copy on write" to
make forking faster. That's a minor detail; unless you're a kernel hacker,
it's fine to act as if all objects are duplicated at the time of forking.

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

Nov 27 '07 #22
In article <sl************ ********@snail. stack.nl>,
Willem <wi****@stack.n lwrote:
>Kenny wrote:
) In article <sl************ ********@snail. stack.nl>,
) Willem <wi****@stack.n lwrote:
)>Marco wrote:
)>) Then please give an implementation of fork() in ISO-C.
)>
)>Why does the implementation have to be in ISO-C ?
)
) If it isn't, we can't talk about it here.

The original claim was that fork() somehow *broke* standard-C.
I don't see how having to implement it in C has anything to do with it.
Then you haven't been around here long enough. Stick around, kid,
you'll get it soon enough.

Nov 27 '07 #23
Stephen Sprunk wrote:
Of course, since ISO C does not permit fork() to do what it
does,
We don't know what the OP's fork() usually does. We only know what it
did in this specific instance.
that's obviously not correct. Specifically, ISO C does not
acknowledge that multiple programs can be running at the same time,
therefore it has _no_ model of how the behavior of one program can (or
can't) affect the behavior of another running concurrently.
From the description of the OP is does not follow, that two programs ran
at the same time. Also, ISO-C allows the communication with a command
processor via "system" and leaves the details to the implementation
>
<OT>However, I find fork() an interesting example, because the program
before the call and the two programs after the call appear to be
conforming; they simply have two independent instances of the ISO C
memory model, initially inherited from the single initial instance but
potentially
diverging. Without other non-portable interactions (e.g. IPC), they
do not interact in any way that is detectable by a conforming
program.</OT>
The most interesing thing is, that in fact I *can* write a ISO-C program
which *may* show the observed behavior. Put this into a file named
"unistd.h":

/* implementation requirements according to 7.40.2.6/2:
works only if
- system("./f") starts the same program which calls fork()
- system("./f") does not return until said program terminates
- system("./f") continues the calling program in a conforming manner
- the file "state" is available for reading to the program which
system("./f") starts.
*/
int get_state()
{
int v;
FILE *p=fopen("state ","r");
if (p==0) return 0;
v=fgetc(p);
fclose(p);
return v;
}
void set_state(int c)
{
FILE *p=fopen("state ","w");
fputc(c,p);
fclose(p);
}
int fork()
{
FILE *p=0;
int pid;
int parent=0;
if (get_state()!=0 ) return 0;
set_state(1);
system("./f");
set_state(0);
return 1;
}

int wait(void *q)
{
}
--
IYesNo yes=YesNoFactor y.getFactoryIns tance().YES;
yes.getDescript ion().equals(ar ray[0].toUpperCase()) ;
Nov 27 '07 #24
Marco Manfredini wrote:
Kelsey Bjarnason wrote:
>Marco Manfredini wrote:
>>I'm just a curios visitor, bewildered by the customs of the local
natives. Anyway, I could point out that this question *is* on-topic,
because the OP found an external library function which seems to
undermine C's memory model *completely*, an inexplicable behavior.

Not really.
[...]

Then please give an implementation of fork() in ISO-C.
There is no such thing. fork() is not defined in ISO-C. If it was
defined it would not be implementable within the language (assuming
the usual meaning).

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 27 '07 #25
In article <47************ ***@yahoo.com>,
CBFalconer <cb********@mai neline.netwrote :
>Marco Manfredini wrote:
>Kelsey Bjarnason wrote:
>>Marco Manfredini wrote:

I'm just a curios visitor, bewildered by the customs of the local
natives. Anyway, I could point out that this question *is* on-topic,
because the OP found an external library function which seems to
undermine C's memory model *completely*, an inexplicable behavior.

Not really.
[...]

Then please give an implementation of fork() in ISO-C.

There is no such thing. fork() is not defined in ISO-C. If it was
defined it would not be implementable within the language (assuming
the usual meaning).
No, no, no. You've got it all wrong. As far as anyone in this NG is
concerned, the following is a perfectly good implementaion of "fork()"
and is absolutely to be considered as good or better than any other
implementation of "fork()":

int fork(void) { puts("Try a spoon instead!"); }

Nov 27 '07 #26
On Nov 27, 10:54 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <474C85F5.5D82D ...@yahoo.com>,
CBFalconer <cbfalco...@mai neline.netwrote :
Marco Manfredini wrote:
Then please give an implementation of fork() in ISO-C.
There is no such thing. fork() is not defined in ISO-C. If it was
defined it would not be implementable within the language (assuming
the usual meaning).

No, no, no. You've got it all wrong. As far as anyone in this NG is
concerned, the following is a perfectly good implementaion of "fork()"
and is absolutely to be considered as good or better than any other
implementation of "fork()":

int fork(void) { puts("Try a spoon instead!"); }
This invokes undefined behavior if the caller attempts to use the
return value of a call to fork().
Nov 27 '07 #27
On Tue, 27 Nov 2007 15:08:09 -0800, Francine.Neary wrote:
On Nov 27, 10:54 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
>int fork(void) { puts("Try a spoon instead!"); }

This invokes undefined behavior if the caller attempts to use the return
value of a call to fork().
No, it is the attempt to use the return value of a call to fork() that
invokes undefined behaviour. "This", the function, is entirely valid.
Nov 27 '07 #28
In article <81************ *************** *******@w34g200 0hsg.googlegrou ps.com>,
<Fr************ @googlemail.com wrote:
....
>This invokes undefined behavior if the caller attempts to use the
return value of a call to fork().
Fixing this is left as an exercise for the reader. I'm sure you'll be
up to the task.

Nov 28 '07 #29
Fr************@ googlemail.com wrote:
gaze...@xmissio n.xmission.com (Kenny McCormack) wrote:
>CBFalconer <cbfalco...@mai neline.netwrote :
>>Marco Manfredini wrote:

Then please give an implementation of fork() in ISO-C.
>>There is no such thing. fork() is not defined in ISO-C. If it
was defined it would not be implementable within the language
(assuming the usual meaning).

No, no, no. You've got it all wrong. As far as anyone in this
NG is concerned, the following is a perfectly good implementaion
of "fork()" and is absolutely to be considered as good or better
than any other implementation of "fork()":

int fork(void) { puts("Try a spoon instead!"); }

This invokes undefined behavior if the caller attempts to use the
return value of a call to fork().
Since puts always returns a non-zero int, it is easily corrected.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 28 '07 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8911
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
23
63724
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two vars, along with their names. I could easily do this: print "myPlace = %s, myTime = %s" % (myPlace, myTime)
12
4916
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same question twice! Here it is: I am having problems reading the value of a text Node. I think it has to do with the fact that the text is in a <span> tag. I have a table and in each <td> I have text + a checkbox. I want to retreive the text next to the...
21
2446
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the C++ Standard: "" "...sequence of operators and operands that specifies a computation...". That means to me that an expression can be "executed". I am purposely
17
479
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I get the value of a form control? ----------------------------------------------------------------------- In HTML documents, named forms may be referred to as named properties of the « document.forms » collection, and named form controls may be referred to as named properties of the form's elements collection: var frm = document.forms;
2
2025
by: Gary Dale | last post by:
I have a form with a pull-down list with six options, each of which has a value set. The value is the e-mail account name (without the domain) of a group while the displayed value is the full name of the group that will receive the e-mail. I pass this.form to a function to validate the other data before handing it off to a script to actually do the mailing. The mailer script looks at all of the form's fields and sends their names and...
2
2323
by: sparks | last post by:
I am trying to set the default value on a forms field to the filter that I am passing to the form. The filter works fine but the field comes up blank. So I put in a default value and it works fine. But I need to change the default value for the field each time I pass it a filter. so on the switchboard I build for this mess I have 8 buttons and each button does this
8
9675
by: grpramodkumar | last post by:
HI, function change(value,sub) { subcat = document.getElementById(sub); subcat.options.value = value; }
45
18930
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
43
3246
by: kenneth | last post by:
Dear all, I have encountered this weird problem. I have a class definition with an __init__ argument 'd' which defaults to {}. This argument is put in the 'self.d' attribute at initialization I create two independent instances of this class; the code is as follows.
0
9852
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...
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10902
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9420
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...
0
5863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4481
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
4062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
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.