10-18-2012
Well, stdout in C is a FILE* and by default buffered. A FILE* is a fd plus a buffer and controls to say how full it is, inside a process. The > refers to fd 1, which is standard output. fd's are not buffered in any app-visible way, but databases and other paranoid things do fsync(), which is like an open-close, and flushes data some distance closer to the disk or network. Close will wait for the data to be written. Exit of a process will flush all FILE*. So, inside bash ">file.txt" is open( "file.txt", O_CREAT | O_APPEND) and then after forking the new process, close(1) the inherited standard out and dup2( new_fd, 1 ) so the new fd is now standard out, with an fd that survives exec(), then exec( new_cmd ). You can watch this on strace. The new_cmd can examine fd 1 with a variety of commands to see if it is a terminal and such, will automatically buffer it with a FILE* called stdout. The app can use stdout or ignore it and use fd 1 on a rawer level of i/o. Because of the buffer, if you switch, you fflush( stdout ) first. Strace cannot see FILE* actions on most machines and then only if you trace library calls, but it sees all fd activity.
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Ok, I'm sure this is a total newbie question, but I think I'm in the right place, no?
I'm trying to call a perl module from a cgi script - Mail::Sendmail - and my web host installed the module in a directory that doesn't seem to be accessible, at least not the way I'm trying. But I thought you... (1 Reply)
Discussion started by: ftb
1 Replies
2. UNIX for Dummies Questions & Answers
I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies
3. UNIX for Dummies Questions & Answers
What is a file descriptor in Unix??
How to find a file descriptor of a file in Unix??
Does it have anything to do with the Inode numbers?? (3 Replies)
Discussion started by: rahulrathod
3 Replies
4. Programming
Hi,
look at the following code:
The client after estabilishing a connection with the server does the following:
if ((peter = fopen(argv, "r")) == NULL){
printf("errore\n");
exit(0);
}
... (11 Replies)
Discussion started by: teo
11 Replies
5. Shell Programming and Scripting
I am trying to right a function which uses a file descriptor to write to a log file. The problem is that the on the print statement the file descriptor is called bad. Now when I first open the file and print to it in the f_open function by passing the descriptor to f_print_log all works well,... (6 Replies)
Discussion started by: robotball
6 Replies
6. Shell Programming and Scripting
Hello All,
Im opening a file desciptor in perl and sending data using print CMD "$xyz".
is there a limit to the length of the string that I can give to this CMD at a time. (3 Replies)
Discussion started by: rimser9
3 Replies
7. UNIX for Dummies Questions & Answers
Hi
What the below path contains?
/proc/<pid>/fd (1 Reply)
Discussion started by: siba.s.nayak
1 Replies
8. Shell Programming and Scripting
Hello,
How can i use file descriptor in a script to read 2 files at the same time and extract line 200 from file 1 and line 500 from file 2.
Thanks. (6 Replies)
Discussion started by: LiorAmitai
6 Replies
9. Shell Programming and Scripting
I am trying to write a script which will only show me the file descriptor count for a process/pid. My script will return me the count only not the whole output. For example, I would like my script to return the output 23 this case, not the whole output.
Can anybody please help me how do I get... (11 Replies)
Discussion started by: mohullah
11 Replies
10. Shell Programming and Scripting
Hi,
I am trying to read a file line-by-line in a while loop, and perform some tasks which involves non-interactive SSH to a remote server. The code looks something like this --
#!/usr/bin/ksh
export myFile=/path/to/my/file.load
while read line
do
do something
## Adding the SSH... (2 Replies)
Discussion started by: Subu1987
2 Replies
LEARN ABOUT BSD
setlinebuf
SETBUF(3S) SETBUF(3S)
NAME
setbuf, setbuffer, setlinebuf - assign buffering to a stream
SYNOPSIS
#include <stdio.h>
setbuf(stream, buf)
FILE *stream;
char *buf;
setbuffer(stream, buf, size)
FILE *stream;
char *buf;
int size;
setlinebuf(stream)
FILE *stream;
DESCRIPTION
The three types of buffering available are unbuffered, block buffered, and line buffered. When an output stream is unbuffered, information
appears on the destination file or terminal as soon as written; when it is block buffered many characters are saved up and written as a
block; when it is line buffered characters are saved up until a newline is encountered or input is read from stdin. Fflush (see
fclose(3S)) may be used to force the block out early. Normally all files are block buffered. A buffer is obtained from malloc(3) upon the
first getc or putc(3S) on the file. If the standard stream stdout refers to a terminal it is line buffered. The standard stream stderr is
always unbuffered.
Setbuf is used after a stream has been opened but before it is read or written. The character array buf is used instead of an automati-
cally allocated buffer. If buf is the constant pointer NULL, input/output will be completely unbuffered. A manifest constant BUFSIZ tells
how big an array is needed:
char buf[BUFSIZ];
Setbuffer, an alternate form of setbuf, is used after a stream has been opened but before it is read or written. The character array buf
whose size is determined by the size argument is used instead of an automatically allocated buffer. If buf is the constant pointer NULL,
input/output will be completely unbuffered.
Setlinebuf is used to change stdout or stderr from block buffered or unbuffered to line buffered. Unlike setbuf and setbuffer it can be
used at any time that the file descriptor is active.
A file can be changed from unbuffered or line buffered to block buffered by using freopen (see fopen(3S)). A file can be changed from
block buffered or line buffered to unbuffered by using freopen followed by setbuf with a buffer argument of NULL.
SEE ALSO
fopen(3S), getc(3S), putc(3S), malloc(3), fclose(3S), puts(3S), printf(3S), fread(3S)
BUGS
The standard error stream should be line buffered by default.
The setbuffer and setlinebuf functions are not portable to non-4.2BSD versions of UNIX. On 4.2BSD and 4.3BSD systems, setbuf always uses a
suboptimal buffer size and should be avoided. Setbuffer is not usually needed as the default file I/O buffer sizes are optimal.
4th Berkeley Distribution May 12, 1986 SETBUF(3S)