setvbuf on STDOUT safe for other processes?
Asked Answered
E

1

6

I am using HP-UX. I want to disable buffering on stdout to ensure that every line of code is printed in case of core dump with below command:

setvbuf(stdout, NULL, _IONBF, 0); // turn off buffering for stdout

In this case, does it also affect other processes printing to stdout which is being redirected to some log file ? I want to know if this change is only local to process being executed or not. Also, can i disable the buffering within the process and later on set it _IO_FBF again within the code ? (fflush before each call )

PS: I know this will disable buffering and have worse I/O performance, but i want to do this only for debugging purposes.

Ethiopia answered 4/1, 2012 at 15:15 Comment(0)
W
1

The setvbuf call only affects stdio routines in the current process and any children fork'd but not exec'd.

How stdio responds when setvbuf is called multiple times on the same stream is not specified in the C standard, so do not issue multiple calls in code you want to be portable across C implementations.

Wadi answered 5/1, 2012 at 2:8 Comment(2)
I was trying this on linux. Actually when i printf something, do a fflush() and set buf size to zero then it works. I can see that all output is there on stdout upto the point before crash.Now problem is that if i reset buffer size to something larger, it is not reflected and i can confirm that stdout still goes on in unbuffered mode. To work around this, i was planning to close(stdout) and reopen it again. However, the filename for console seems to be platform dependent ( /dev/pts/0 inlinux, /dev/stdout on hp-ux ). Any straightforward solution to find out source for stdout and reopen it ?Ethiopia
Simpler to use dup to hang on to what stdout is opened on. I.e. do this dance: saved = dup(1); fclose(stdout); dup(saved); close(saved); stdout = fdopen(1, "w"); Now you have a freshly opened stdout as far as stdio is concerned, pointing to the same file/pipe/device as before, but ready to accept a new setvbuf call.Wadi

© 2022 - 2024 — McMap. All rights reserved.