🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.internals
4 messages
4 total messages Started by Ming-Chuan Huang Thu, 19 Nov 1992 13:30
printf() & fork() ......
#2392
Author: Ming-Chuan Huang
Date: Thu, 19 Nov 1992 13:30
34 lines
1012 bytes
Hello:

#include <stdio.h>

program 1:

void main()
{
    printf("Hello ");
    if (fork() == 0)
        printf("world\n");
}

result:
Hello Hello world

program 2:

void main()
{
    printf("Hello \n");
    if (fork() == 0)
        printf("world\n");
}

result:
Hello 
world


is there anybody who knows the difference between the two programs ?

                                                      mchuang
                                     mchuang@csie.nctu.edu.tw
Re: printf() & fork() ......
#2394
Author: Aaron Orenstein
Date: Sun, 22 Nov 1992 01:41
71 lines
2320 bytes
In article <1992Nov19.133035.172@ccsun7.csie.nctu.edu.tw>, mchuang@csie.nctu.edu.tw (Ming-Chuan Huang) writes:
-> Hello:
-> 
-> #include <stdio.h>
-> 
-> program 1:
-> 
-> void main()
-> {
->     printf("Hello ");
->     if (fork() == 0)
->         printf("world\n");
-> }
-> 
-> result:
-> Hello Hello world
-> 
-> program 2:
-> 
-> void main()
-> {
->     printf("Hello \n");
->     if (fork() == 0)
->         printf("world\n");
-> }
-> 
-> result:
-> Hello 
-> world
-> 
-> 
-> is there anybody who knows the difference between the two programs ?
-> 
->                                                       mchuang
->                                      mchuang@csie.nctu.edu.tw

Your problem is that you are using buffered I/O.
under unix, printf() does not actually output the information until
either a newline is printed, or the output is flushed.  (Output is automatically
flushed when you try to get input).  So what is happening is this:

You do a printf("Hello ") and it fills the buffer with "Hello ".  When you
fork, the child gets a copy of the buffer.  The parent exits, and flushes it's buffer which outputs "Hello " on the screen.  The child, adds "world" to it's
buffer and then, since you gave it a newline, it flushes the buffer.  So the
child prints out "Hello world"

In the second example, the newline at the end of the first printf() is flushing
the buffer so the buffer is empty when the child starts...

Your problem could be solved with the following code:
void main()
{
  printf("Hello ");
  fflush(stdout);
  if(fork()==0) printf("world\n");
}

result:
Hello world



	Anything else?

    Good luck
	- Aaron

-- 
--------------------
"The Code is free, It's the comments that cost a lot..."
Aaron Orenstein	(aorenste@cs.ulowell.edu, 129.63.1.11)
Re: printf() & fork() ......
#2393
Author: Mike Swiston
Date: Sun, 22 Nov 1992 02:07
34 lines
1090 bytes
In <1992Nov19.133035.172@ccsun7.csie.nctu.edu.tw> mchuang@csie.nctu.edu.tw (Ming-Chuan Huang) writes:

% #include <stdio.h>

% program 1:

% void main()
% {
%     printf("Hello ");
%     if (fork() == 0)
%         printf("world\n");
% }

% result:
% Hello Hello world

Under turbo cshell, I get the output:
Hello world
Hello  Exit 1

leading me to guess that the problem is that
your output buffer isn't getting flushed the
way you might expect it to be.  You can fix
that like so:

#include <stdio.h>

void main()
{
    printf("Hello ");
    fflush(stdout);
    if (fork() == 0)
        printf("world\n");
}
Re: printf() & fork() ......
#2395
Author: John Hascall
Date: Sun, 22 Nov 1992 16:29
26 lines
1073 bytes
mchuang@csie.nctu.edu.tw (Ming-Chuan Huang) writes:
~void main() {
~    printf("Hello ");
~    if (fork() == 0) printf("world\n");
~}
~result: Hello Hello world

~void main() {
~    printf("Hello \n");
~    if (fork() == 0) printf("world\n");
~}
}result: Hello
         world
~is there anybody who knows the difference between the two programs ?

Yes.

John
(hint: try comp.unix.questions)

(useful hint: `line buffered')
-- 
John Hascall                   ``An ill-chosen word is the fool's messenger.''
Systems Software Engineer
Project Vincent
Iowa State University Computation Center  +  Ames, IA  50011  +  515/294-9551
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads