🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
4 messages
4 total messages Started by Abhay B. Joshi Thu, 18 Oct 1990 21:44
Output redirection
#1534
Author: Abhay B. Joshi
Date: Thu, 18 Oct 1990 21:44
29 lines
1073 bytes
Hello Netters,

Following is a small C program.
-----------begin-------
#include <stdio.h>

main()
{
	fprintf(stdout,"Some People\n") ;
	fprintf(stderr,"Are In\n") ;
	fprintf(stdout,"Very \n") ;
	fprintf(stderr,"Deep\n") ;
	fprintf(stdout,"Trouble\n") ;
}
-----------end---------

Try to compile and execute it in the following ways:
	% a.out		# prints "Some People Are In Very Deep Trouble"
	% a.out >tmp 2>&1
	% cat tmp	# prints "Are In Deep Some People Very Trouble"

Sorry, I have removed the newlines between the words. But the sequence is
of importance.

Is there a way to keep the sequence the same, while using o/p redirection ?
Thanks.

Abhay Joshi
uunet!motcid!joshi
Re: Output redirection
#1536
Author: Paul John Falstad
Date: Fri, 19 Oct 1990 03:37
25 lines
1368 bytes
In article <4859@graphite20.UUCP> joshi@motcid.UUCP (Abhay B. Joshi) writes:
>#include <stdio.h>
>
>main()
>{
	setlinebuf(stdout);  /* this should fix it */
>	fprintf(stdout,"Some People\n") ;
>	fprintf(stderr,"Are In\n") ;
>	fprintf(stdout,"Very \n") ;
>	fprintf(stderr,"Deep\n") ;
>	fprintf(stdout,"Trouble\n") ;
>}

By default, stdout is line buffered when connected to a terminal, and
block buffered otherwise.  stderr is always unbuffered, hence the
discrepancy.

I'm sure you have your reasons for wanting this.  Apparently they're
beyond me.  :-)

--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
And Dinsdale said, "You've been a naughty boy, Clement," and splits me nostrils
open, and saws me leg off, and pulls me liver out.  And I said, "My name's not
Clement."  And then he loses his temper.  And he nails me head to the floor.
Re: Output redirection
#1546
Author: Conor P. Cahill
Date: Fri, 19 Oct 1990 12:14
32 lines
1876 bytes
In article <4859@graphite20.UUCP> joshi@motcid.UUCP (Abhay B. Joshi) writes:
>  [ program with mixed stderr and stdout output deleted]
>
>	% a.out		# prints "Some People Are In Very Deep Trouble"
>	% a.out >tmp 2>&1
>	% cat tmp	# prints "Are In Deep Some People Very Trouble"

The problem is the way that stderr and stdout are buffered.  If going
to a tty, both of them are line buffered on most systems.  This means
that once a \n is placed into the buffer, it is flushed.

In the case of re-direction, the stderr remains line buffered while the
stdout becomes block buffered (it won't be written out until it gets
a full block of data or is flushed directly).

You can solve this problem two ways.  The first way is the turn off
the buffering of data.  This is not the best solution and can have
a detrimental effect on the performance of your system.  To do this
you need to add the following calls to the begining of your 
program:
		setbuf(stdout,(char *)0);
		setbuf(stderr,(char *)0);

The second (and optimal) way to solve the problem is to place a call to
fflush() whereever it is important.  This way you only flush the buffer
when it is necessary.


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 
Re: Output redirection
#1549
Author: Joe Smith
Date: Sat, 20 Oct 1990 00:25
19 lines
1151 bytes
#include <stdio.h>
main()
{
        fprintf(stdout,"Some People\n") ;
	fflush(stdout);   /* must flush to make sure this comes out 1st */
        fprintf(stderr,"Are In\n") ;
	fflush(stderr);
        fprintf(stdout,"Very \n") ;
	fflush(stdout);
        fprintf(stderr,"Deep\n") ;
	fflush(stderr);
        fprintf(stdout,"Trouble\n") ;
	fflush(stdout);
}
-- 
Joe Smith (408)922-6220 | SMTP: jms@tardis.tymnet.com or jms@gemini.tymnet.com
BT Tymnet Tech Services | UUCP: ...!{ames,pyramid}!oliveb!tymix!tardis!jms
PO Box 49019, MS-C41    | BIX: smithjoe | 12 PDP-10s still running! "POPJ P,"
San Jose, CA 95161-9019 | humorous dislaimer: "My Amiga 3000 speaks for me."
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