[comp.lang.c] It's unexplainable...

speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) (01/31/91)

Would anyone please explain to me why my output is not what I am
expecting...  Here is a script of my source and the output...

Thanks,
Lance
-----------------------------------------------------

Script started on Wed Jan 30 15:15:36 1991
/dev/ttyp1: Not owner
bronze::/u3/speelmo/c/test:26>> cat foo.c
#include <stdio.h>

void main(void){
  printf("The date is: ");
  system("date");
  printf("\n");
}
bronze::/u3/speelmo/c/test:27>> cc foo.c
bronze::/u3/speelmo/c/test:28>> a.out
Wed Jan 30 15:16:17 EST 1991
The date is: 
bronze::/u3/speelmo/c/test:29>> 
script done on Wed Jan 30 15:16:21 1991
-- 
==============================================================================
| Lance Speelmon                      |  University Computing Services       | 
| speelmo@bronze.ucs.indiana.edu      |  Network Operations Center           |
==============================================================================

prg@mgweed.UUCP (Gunsul) (01/31/91)

In article <1991Jan30.203605.14481@bronze.ucs.indiana.edu>, speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) writes:
| Would anyone please explain to me why my output is not what I am
| expecting...  Here is a script of my source and the output...
| 
| Thanks,
| Lance
| -----------------------------------------------------
| 
| #include <stdio.h>
| 
| void main(void){
|   printf("The date is: ");
|   system("date");
|   printf("\n");
| }
| ==============================================================================
| | Lance Speelmon                      |  University Computing Services       |
| | speelmo@bronze.ucs.indiana.edu      |  Network Operations Center           |
| ==============================================================================

Lance, just before your printf statement, insert this line:

	setbuf(stdout, 0);

I think you'll be pleased with the results..  You might want to RTFM on
setbuf after you see the results.  Many people have been had by this one!

Phil

peterj@swanee.ee.uwa.oz.au (Peter Jones) (01/31/91)

In <1991Jan30.203605.14481@bronze.ucs.indiana.edu>
    speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) writes:

>Would anyone please explain to me why my output is not what I am expecting...  
>% cat foo.c
>#include <stdio.h>

>void main(void){
>  printf("The date is: ");
>  system("date");
>  printf("\n");
>}
>% a.out
>Wed Jan 30 15:16:17 EST 1991
>The date is: 

There are two processes running here, one is the "foo" program and the
other is the "date" program.  It will depend on the scheduling what actually
happens - I guess.  It would need "foo" to either flush its output buffer
or to do an input or to write to "stderr" (& not be buffered).

profesor@wpi.WPI.EDU (Matthew E Cross) (01/31/91)

In article <peterj.665303644@zeus> peterj@swanee.ee.uwa.oz.au (Peter Jones) writes:
>In <1991Jan30.203605.14481@bronze.ucs.indiana.edu>
>    speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) writes:
>
>>Would anyone please explain to me why my output is not what I am expecting...  
>>% cat foo.c
>>#include <stdio.h>
>
>>void main(void){
>>  printf("The date is: ");
>>  system("date");
>>  printf("\n");
>>}
>>% a.out
>>Wed Jan 30 15:16:17 EST 1991
>>The date is: 
>
>There are two processes running here, one is the "foo" program and the
>other is the "date" program.  It will depend on the scheduling what actually
>happens - I guess.  It would need "foo" to either flush its output buffer
>or to do an input or to write to "stderr" (& not be buffered).

No, no, no. Well, OK, there are two processes, but nothing depends on
the scheduling.  "foo" will run, start the subprocess "date", which
runs and finishes, then "foo" will continue.

What causes this is the line buffering of the stdio functions.
Whenever you print anything, it goes into a buffer, until you send a
'\n' character, when the routines receive a '\n' character, they flush
the output buffer.  (this is also why 'getchar()' won't to anything
until you hit a return, it waits to flush the buffer).  Also, the
"date" program prints a newline before it prints the date.

To avoid the line buffering, you would have to use 'setbuf()' or
something like it.

-- 
+-------------------------------------+---------------------------------------+
| "The letter U has a lot of uses ... |       profesor@wpi.wpi.edu            |
|  I like to play it like a guitar!"  +---------------------------------------+
|          -Sesame Street             |       Make love, not War..            |

gwyn@smoke.brl.mil (Doug Gwyn) (02/01/91)

In article <8412@mgweed.UUCP> prg@mgweed.UUCP (Gunsul) writes:
>In article <1991Jan30.203605.14481@bronze.ucs.indiana.edu>, speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) writes:
>| #include <stdio.h>
>| void main(void){
>|   printf("The date is: ");
>|   system("date");
>|   printf("\n");
>| }
>Lance, just before your printf statement, insert this line:
>	setbuf(stdout, 0);
>I think you'll be pleased with the results..  You might want to RTFM on
>setbuf after you see the results.  Many people have been had by this one!

This is not a very good solution, since it forces stdout to be unbuffered.
(By the way, the only reason you can write 0 instead of an explicitly
correct null pointer type is that there is presumably a prototype in scope.
On non-ANSI C implementations, you should use (char*)0 or (char*)NULL for
the second argument.)  A better solution is to include fflush(stdout); just
before invoking system().

Note also that main() is misdefined.  A function returning int is expected
by the run-time system.

yang@nff.ncl.omron.co.jp (YANG Liqun) (02/01/91)

In profesor@wpi.WPI.EDU (Matthew E Cross)'s arctile he writes:
>In article <peterj.665303644@zeus> peterj@swanee.ee.uwa.oz.au (Peter Jones) writes:
>>>In <1991Jan30.203605.14481@bronze.ucs.indiana.edu>
>> speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) writes:
>> ...
>>>Would anyone please explain to me why my output is not what I am expecting...

>> There are two processes running here, one is the "foo" program and the
>>other is the "date" program.  It will depend on the scheduling what actually
>>happens - I guess.  It would need "foo" to either flush its output buffer
>>or to do an input or to write to "stderr" (& not be buffered).

>No,no, no. Well, OK, ...

>Also, the "date" program prints a newline before it prints the date.

Yes, "date" does print a newline. But if it did not print a newline what would
be ? Suppose system calls a command does not print a newline. I think the
result will be the same.
The key point here is that a process exits it will automatically close all
the files opened and "close" will flush the buffer.
Yang

--
;  Li-qun Yang			OMRON Computer Technology R&D lab
;  yang@nff.ncl.omron.co.jp	tel: 075-951-5111  fax: 075-956-7403

srini@ultra.com (S. Srinivasan) (02/02/91)

In <1991Jan30.203605.14481@bronze.ucs.indiana.edu> speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) writes:

>Would anyone please explain to me why my output is not what I am
>expecting...  Here is a script of my source and the output...

>void main(void){
>  printf("The date is: ");
>  system("date");
>  printf("\n");
>}
>Wed Jan 30 15:16:17 EST 1991
>The date is: 

"printf" uses stdio which uses buffering in that it does not 
actually send output to the stdout until :
	1. It hits the buffer limit, OR
	2. It hits a newline (not too sure of this one), OR
	3. the calling process exits.

"system" call forks a subshell to print out the date, which again
may use stdio buffering, but the child process exits and the buffers are
thus flushed.  The parent "main" process which was blocked waiting for the
child then continues until it exits.  Only at this point are its buffers 
flushed.  Thus...

To fix it, you might try looking at setbuf(2).

S. Srinivasan
srini@ultra.com