why i value doesn't change ?

R

Richard Tobin

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.

All kinds of things break standard C. Using a debugger lets you
change variables. Using threads makes all kinds of things unsafe.
Using memory-mapping functions can completely mess everything up. You
can't expect everything about standard C to continue to be true when
you use libraries not written in pure standard-defined C. And a good
thing too.

-- Richard
 
S

Stephen Sprunk

Marco Manfredini said:
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.programmer.
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
 
S

Stephen Sprunk

#include<unistd.h>

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.
 
K

Kenny McCormack

Kenny wrote:
) In article <[email protected]>,
)>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.
 
M

Marco Manfredini

Stephen said:
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)
{
}
 
C

CBFalconer

Marco said:
Kelsey said:
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).
 
K

Kenny McCormack

Marco said:
Kelsey said:
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!"); }
 
F

Francine.Neary

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().
 
K

Kenny McCormack

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.
 
C

CBFalconer

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.
 
H

Harald van Dijk

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.
 
P

pete

Marco said:
A conforming implementation allows
the user to use code that is declared
in header files and defined elsewhere. Quiz: Judging only the behavior
described by the OP, can this mysterious "fork()" function be
implemented in ISO-C? If not, what does that mean? Undefined behavior?

It means fork is on topic in another newsgroup.
The problem is that most programmers
find "comp.lang.c" too easy to spell,
while finding a newsgroup where forks are on topic
is too difficult.
 
P

pete

Willem said:
Kenny wrote:
) In article <[email protected]>,
)>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.

It has nothing to do with it.
Lots of standard C library functions
can't be implemented portably in C,
but they're all on topic.

You really shouldn't reply to "Kenny".
It's just a Markov text generator.
 
C

Charlie Gordon

CBFalconer said:
Since puts always returns a non-zero int, it is easily corrected.

Non sequitur.

puts can return EOF or a non negative value, where do you get that it cannot
be zero?
This implementation of fork can be easily fixed in many ways: an easy one
consists in inserting the return keyword in front of puts.
 
C

CBFalconer

Charlie said:
"CBFalconer" <[email protected]> a écrit:
.... snip ...


Non sequitur.

puts can return EOF or a non negative value, where do you get that
it cannot be zero? This implementation of fork can be easily
fixed in many ways: an easy one consists in inserting the return
keyword in front of puts.

I think I misread 'non-negative' as 'non-zero' in the following:

7.19.7.10 The puts function

Synopsis
[#1]
#include <stdio.h>
int puts(const char *s);

Description

[#2] The puts function writes the string pointed to by s to
the stream pointed to by stdout, and appends a new-line
character to the output. The terminating null character is
not written.

Returns

[#3] The puts function returns EOF if a write error occurs;
otherwise it returns a nonnegative value.

My mistake. Sorry. Well caught.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top