Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.unix-wizards Subject: Re: Unix standard output buffer problem Message-ID: <309@hadron.UUCP> Date: Sun, 16-Mar-86 13:03:51 EST Article-I.D.: hadron.309 Posted: Sun Mar 16 13:03:51 1986 Date-Received: Tue, 18-Mar-86 07:26:31 EST References: <1421@brl-smoke.ARPA> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 33 In article <1421@brl-smoke.ARPA> PUPPY%clemson.csnet@csnet-relay.arpa writes: > I set the standard output flag byte to indicated no buffering: > stdout -> _flag |= _IONBF; /* Declared in stdio.h */ > The code then does a fork() call. ... > The child process does an exec() type call. The execed pgm > reports that the stdout -> _flag has mysteriously been reinitialized > to indicate buffering. The problem is, that local states aren't carried over in an exec(). When a program is exec'ed, the entire memory of the process being run is wiped clean, and then the image of the new program is overlaid on top of it. So, you see, unfortunately, no status information can be passed down except via command-line arguments or other, non- standard techniques (like using IPC's: pipes, sockets, shared memory, mailboxes, et alii). Stdout is always unbuffered if and only if it is connected to a tty; otherwise it defaults to buffered. You may set it to unbuffered for the duration of your program with setbuf(stdout, (char *) NULL); but this does not last past exec()'s. You may alternatively (and should, if leaving stdout buffered) do an fflush(stdout) every time that you think you want output to appear. This, unfortunately, doesn't help you if what you want to do is run a binary program thorugh a pipe. There were, once upon a time, various kernel mods to associate a pipe with a tty, so that stat's and ioctl's got passed through. I'm afraid I don't have any references here and now. BTW: for future reference, the stdio routines are given to you so that you don't have to mess with FILE internals. Don't do it: it'll turn around and bite you some day when you least expect it ... -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}