Linux System Programming] [IO standard buffer

00. Contents

01. Buffer Overview

Standard I / O buffer provides three types:

1, fully buffered :

The actual I / O operations to fill the standard I / O buffers before. Conventional documents (such as plain text files) are generally fully buffered.

2, line buffer :

When a line break occurs in the input and output, the standard I / O library to perform I / O operations. This allows us time to output a character, but only the actual I / O operations after writing a line. When the standard input and standard output corresponding to the terminal device (such as a screen) are usually buffered row.

3, without a buffer :

Every tune user program libraries do have to write a kernel call write-back (such as system calls) through the system. Standard error output is usually unbuffered, so that the user program generates an error message may be output to the device as soon as possible.

02. Fully Buffered

Test code:

int main(int argc, char *argv[])
{
	FILE *fp = NULL;
	// 读写方式打开,文件不存在则创建
	fp = fopen("test.txt", "w+");
	if(NULL == fp)
	{
		printf("open error\n");
		return 1;
	}
	char *str = "C++程序员\n";
	fwrite(str, 1, strlen(str), fp);	// 往文件写内容
	while(1);	// 程序阻塞在这里
 
	return 0;
}

Run the program found that, test.txt and no content. Because conventional file is usually fully buffered, only after the buffer is full, the contents will be written to a file. Next, we change what the example above.

Test code:

#include <stdio.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
	FILE *fp = NULL;
	// 读写方式打开,文件不存在则创建
	fp = fopen("test.txt", "w+");
	if(NULL == fp)
	{
		printf("open error\n");
		return 1;
	}
	char *str = "test\n";
	int i = 0;
	while(i <= 512){	// 缓冲区大小不确定,i的大小只是一个调试值
		fwrite(str, 1, strlen(str), fp);	// 往文件写内容
		i++;
	}
	while(1);	// 程序阻塞在这里
 
	return 0;
}

The above example is circulating to write the contents of the file, so that it is possible to fill the buffer, and found, the file is content. To be successful in fact written to the file content, in addition to the buffer is filled, there are other ways.

2.1 people close the file, even if the buffer is not full, the contents will be written into the file

#include <stdio.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
	FILE *fp = NULL;
	// 读写方式打开,文件不存在则创建
	fp = fopen("test.txt", "w+");
	if(NULL == fp)
	{
		printf("open error\n");
		return 1;
	}
	char *str = "test ok\n";
	fwrite(str, 1, strlen(str), fp);	// 往文件写内容
	fclose(fp);		// 人为关闭文件,就算缓冲区没有填满,内容也会写进文件
	
	while(1);	// 程序阻塞在这里
 
	return 0;
}

2.2 program ends normally, even if the buffer is not full, does not close the file, the contents will be written into the file.

#include <stdio.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
	FILE *fp = NULL;
	// 读写方式打开,文件不存在则创建
	fp = fopen("test.txt", "w+");
	if(NULL == fp)
	{
		printf("open error\n");
		return 1;
	}
	char *str = "test ok\n";
	fwrite(str, 1, strlen(str), fp);	// 往文件写内容
	
	return 0;
	// 程序正常结束,就算缓冲区没有填满,没有关闭文件,内容也会写进文件。
}

03. Line buffer

#include <stdio.h>
 
int main(int argc, char *argv[])
{
	printf("hello test");
	while(1);
	
	return 0;
}

Run this program, you will find hello test did not print to the screen. Since the standard input and standard output corresponding to the line terminal devices are usually buffered, when newline input and output, the standard I / O library to perform I / O operations. as follows:

#include <stdio.h>
 
int main(int argc, char *argv[])
{
	printf("hello test\n");
	while(1);
	
	return 0;
}

In addition to a line break, there are other ways to perform I / O operations.

3.1 buffer is full

int main(int argc, char *argv[])
{
	while(1)
    {	
        // 循环打印,总有缓冲区填满的可能
		printf("hello sunplusedu");
	}
	while(1);
	
	return 0;
}

3.2 man-refresh buffer

#include <stdio.h>
 
int main(int argc, char *argv[])
{
	printf("hello test");
	fflush(stdout);	// 人为刷新
 
	while(1);
	
	return 0;
}

3.3 program ends normally

#include <stdio.h>
 
int main(int argc, char *argv[])
{
	printf("hello sunplusedu");
	
	return 0;
	// 程序正常结束
}

04. Without the buffer

#include <unistd.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
	char *str = "hello test";	
	// 有没有\n,缓冲区有没有填满,都没关系
	write(1, str, strlen(str));	// 往标准输出写内容
	while(1);
	
	return 0;
}

Linux system call IO functions generally do not have a buffer.

05. Appendix

发布了639 篇原创文章 · 获赞 2326 · 访问量 75万+

Guess you like

Origin blog.csdn.net/dengjin20104042056/article/details/102989729