fork problem

M

mishra

Hi,
i thied the following code..
# include<stdio.h>
int main()
{
int a;
printf("Hello...");
a=fork();
printf("hi\n");
return(0);
}

the o/p is as follows..
rapti:~/prg [\>] > ./a.out
Hello... hi
Hello... hi

but when i gave
# include<stdio.h>
int main()
{
int a;
printf("Hello...\n"); // mark the \n here
a=fork();
printf("hi\n");
return(0);
}

the o/p became ..
rapti:~/prg [\>] > ./a.out
Hello...
hi
hi

can any one tell me how that \n is affecting?
and insted of the printf statement if i put some thing else.. that is
not getting evaluated two times...

please help me out...
regard
Mishra
 
J

Johan Borkhuis

mishra said:
Hi,
i thied the following code..
# include<stdio.h>
int main()
{
int a;
printf("Hello...");
a=fork();
printf("hi\n");
return(0);
}

the o/p is as follows..
rapti:~/prg [\>] > ./a.out
Hello... hi
Hello... hi

but when i gave
# include<stdio.h>
int main()
{
int a;
printf("Hello...\n"); // mark the \n here
a=fork();
printf("hi\n");
return(0);
}

the o/p became ..
rapti:~/prg [\>] > ./a.out
Hello...
hi
hi

can any one tell me how that \n is affecting?
and insted of the printf statement if i put some thing else.. that is
not getting evaluated two times...

Looks like printf is buffering data until it gets a newline. As you fork
the original process the data for printf is also copied to the new
context, and so you see the data appear twice.
If you would change this you could try using unbuffered output.

Kind regards,
Johan

--
o o o o o o o . . . _____J_o_h_a_n___B_o_r_k_h_u_i_s___
o _____ || http://www.borkhuis.com |
.][__n_n_|DD[ ====_____ | (e-mail address removed)4all.nl |
>(________|__|_[_________]_|________________________________|
_/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
== VxWorks-page: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
 
N

ng5000

Ok,

If remember correctly (I', sure you'll let me know if not!):

'\n' causes the stdout buffer to be flushed, i.e. as soon as '\n' is
parsed the contents will, in this case, be displayed on screen. In the
first example you don't flush the buffer so 'Hello' will still be in
the buffer waiting to be outputted. fork () returns twice, once as the
parent process and once as the child process. The child process's
memory will be a copy of the parent process's memory (i.e. both
processes will have the same data in memory), so a copy of stdout's
buffer (which contains "Hello") will exist for both processes. When
you then go on to send "hi\n" to stdout the buffer is flushed causing
"Hello" to be displayed by both the parent and the child.

In order to better understand how fork () works you should be checking
it's return value (remembering that it returns twice) so that you can
print whether you are in the parent or the child.

Nick
 
A

Antonio

mishra said:
Hi,
i thied the following code..
# include<stdio.h>
int main()
{
int a;
printf("Hello...");
a=fork();
printf("hi\n");
return(0);
}

the o/p is as follows..
rapti:~/prg [\>] > ./a.out
Hello... hi
Hello... hi

but when i gave
# include<stdio.h>
int main()
{
int a;
printf("Hello...\n"); // mark the \n here
a=fork();
printf("hi\n");
return(0);
}

the o/p became ..
rapti:~/prg [\>] > ./a.out
Hello...
hi
hi

can any one tell me how that \n is affecting?
and insted of the printf statement if i put some thing else.. that is
not getting evaluated two times...

please help me out...
regard
Mishra

This is my first post here, but I think I can help there. This strange
behaviour is caused because of the way that stdout behaves. Stdout is
line buffered, wich means that when you write to it, in truth you're
writting to a buffer that doesn't get "flushed" until a new line
character is written to the buffer.

In your first example, you write to stdout, but don't place any new
line character, so the actual write to console is delayed. Then you
make a fork, wich creates a copy of the process, including the buffer
to stdout. When you make the second printf, the buffer is flushed, so
both processes write the same to stdout.

In the second example, since there is a new line character in the first
printf, the buffer has been flushed, and so both processes after the
fork have an empty buffer to stdout.

So to summarize, the first printf is only executed once, as would be
any sentence that you could place before the fork. But it's effects are
replicated by the line buffering of stdout.

Hope that helped.
 
S

SM Ryan

# This is my first post here, but I think I can help there. This strange
# behaviour is caused because of the way that stdout behaves. Stdout is
# line buffered, wich means that when you write to it, in truth you're

In this case, it is almost certainly line bufferred, but that does not
have to be generally true of stdout. To force the bufferring behaviour,
use the setvbuf function.

As a general rule, force stdio buffer flushes of all writable FILEs
before forking. You can also use fflush for this.
 
K

Keith Thompson

mishra said:
i thied the following code..
# include<stdio.h>
int main()
{
int a;
printf("Hello...");
a=fork();
printf("hi\n");
return(0);
}

the o/p is as follows..
rapti:~/prg [\>] > ./a.out
Hello... hi
Hello... hi
[snip]

There is no fork() function in standard C. Try comp.unix.programmer
(where they'll tell you, among other things, that you should #include
the proper system-specific header before using fork().)

If you invoked your compiler properly, it should warn you that fork()
is undeclared. If you're using gcc, try "gcc -ansi -pedantic -W -Wall";
change "-ansi" to "-std=c99" if you want to take advantage gcc's
incomplete C99 support. If you're using a compiler other than gcc,
check its documentation.
 
J

Jack Klein

mishra said:
Hi,
i thied the following code..
# include<stdio.h>
int main()
{
int a;
printf("Hello...");
a=fork();
printf("hi\n");
return(0);
}

the o/p is as follows..
rapti:~/prg [\>] > ./a.out
Hello... hi
Hello... hi

but when i gave
# include<stdio.h>
int main()
{
int a;
printf("Hello...\n"); // mark the \n here
a=fork();
printf("hi\n");
return(0);
}

the o/p became ..
rapti:~/prg [\>] > ./a.out
Hello...
hi
hi

can any one tell me how that \n is affecting?
and insted of the printf statement if i put some thing else.. that is
not getting evaluated two times...

please help me out...
regard
Mishra

This is my first post here, but I think I can help there.

Since it's your first post here, I'll point this out:

The question is off-topic and any attempts to answer it in detail are
off-topic. The topic of this newsgroup is the C language, which is
specifically and completely defined originally by the two editions of
K&R, then, since 1989, by various ANSI/ISO International Standards.

fork() is not part of the C language or its libraries. It is a
non-standard (from the point of the language and this newsgroup)
extension provided by some particular compiler and operating system
combinations.

The UNIX API does not define the C language. The Windows API does not
define the C language.

In cases like this, the only appropriate reply is to redirect the
poster to a group where his question is topical, if one knows of one.
In this case would be a good choice.
 
S

SM Ryan

# The question is off-topic and any attempts to answer it in detail are

It is most definitely on-topic unless you think setbuffer, setvbuf,
printf, stdout, and _everything_ else in <stdio.h> is off-topic.
The question is about how does stdio bufferring work, not how does
fork work. A similar issue would show up if the program aborts and
appears to lose part of the output: until you know some output can
be bufferred and unknown to the operating system, you won't understand
what setvbuf refers to.

# fork() is not part of the C language or its libraries. It is a

As usual, two mistakes: "C language" is not ANSI C. Second, you see
"fork" and your knee jerks without pausing to understand the true
problem, which is how ANSI C file bufferring and interact with program
execution.

# The UNIX API does not define the C language. The Windows API does not
# define the C language.

ANSI <stdio.h> does not define the language.
 
R

Robert Gamble

SM said:
# The question is off-topic and any attempts to answer it in detail are

It is most definitely on-topic unless you think setbuffer, setvbuf,
printf, stdout, and _everything_ else in <stdio.h> is off-topic.
The question is about how does stdio bufferring work, not how does
fork work. A similar issue would show up if the program aborts and
appears to lose part of the output: until you know some output can
be bufferred and unknown to the operating system, you won't understand
what setvbuf refers to.

# fork() is not part of the C language or its libraries. It is a

As usual, two mistakes: "C language" is not ANSI C.

It is in this group, but as usual you still can't seem to comprehend
this.
Second, you see "fork" and your knee jerks without pausing to understand the > true problem, which is how ANSI C file bufferring and interact with program
execution.

The use of fork() plays a large part in the behavior being discussed
and a group like comp.unix.programmer would be a much more appropriate
place for this post. Jack was completely correct in what he said and
he couldn't have been more helpful or polite with his reply.

Robert Gamble
 
A

Antonio

Robert said:
It is in this group, but as usual you still can't seem to comprehend
this.

<flame>
Then this group should be renamed to something along the lines of
comp.lang.c.ansi or comp.lang.ansic
</flame>

In case anyone doubts it, the flame was intended to be a joke...
 
C

CBFalconer

Robert said:
It is in this group, but as usual you still can't seem to
comprehend this.


The use of fork() plays a large part in the behavior being discussed
and a group like comp.unix.programmer would be a much more appropriate
place for this post. Jack was completely correct in what he said and
he couldn't have been more helpful or polite with his reply.

As contrasted to Ryans continuous childish attempts to foul normal
newsreaders by insisting on his non-standard quote mark. It is
best to treat him as a pure troll, and PLONK him. This reduces the
annoyance factor and thence the blood pressure.
 
C

CBFalconer

Antonio said:
<flame>
Then this group should be renamed to something along the lines
of comp.lang.c.ansi or comp.lang.ansic
</flame>

In case anyone doubts it, the flame was intended to be a joke...

Unfortunately there are those among us who would take it
seriously. They are often the same people that flout other useful
standards for no reason other than to annoy. We who restrict the
definition have at least 6 references available:

K&R I
K&R II
C89
C90
C95
C99

all of which can be fairly readily found, and all of which build on
previous work. Most of these have, at one time or another, been
stamped as "THE STANDARD" by national and international
organizations. Before recommending anything else, be sure it is
equally accepted and applicable. For example Posix is an OS
standard, not a language standard. Many systems can use the C
language that can never even approach the Posix standard.
 
S

SM Ryan

# The use of fork() plays a large part in the behavior being discussed

Your knee is jerking too. fork demonstrates the problem in this case,
but the underlying issue is not fork, but has stdio bufferring works.
 
S

SM Ryan

# You neglected to quote Jacks informative paragraph, which follows:
#
# >>> which is specifically and completely defined originally by the
# >>> two editions of K&R, then, since 1989, by various ANSI/ISO

Maybe you should re-read K&R 1978 pp 158-177 (chapter 8).
 
B

Barry Schwarz

# You neglected to quote Jacks informative paragraph, which follows:
#
# >>> which is specifically and completely defined originally by the
# >>> two editions of K&R, then, since 1989, by various ANSI/ISO

Maybe you should re-read K&R 1978 pp 158-177 (chapter 8).

You mean the one that says this chapter applies only to Unix.


<<Remove the del for email>>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top