write + and fwrite own cache

Linux read / write fread / fwrite difference between the two

In this text, double-click

Linux read / write fread / fwrite difference between the two

1, fread is buffered, read without a buffer.

2, fopen c is defined in the standard, open is defined in POSIX.

3, fread to read a configuration may .read reading binary file with no difference in the general linux / unix in.

4, fopen can not specify the file to create rights .open can assign permissions.

5, fopen return pointer, open returns a file descriptor (an integer).

6, any device is a file linux / unix in, you can use open, read.

 

If the file size is 8k.

If you use read / write, and only allocate a buffer 2k, to read the file needs to be done four times a system call to actually read from the disk.

If you use fread / fwrite, the system automatically allocates buffer, then read this file as long as the system call read out from the disk.

That is, a read / write disk to read 4 times, and use fread / fwrite read 1 times as long as the disk, the efficiency ratio read / write 4 times higher.

 

If the program is limited to memory, with a read / write better.

Fwrite and fread are used, it is automatically assigned the cache, the speed will be fast, simpler than do their own. If you want to deal with some specific descriptor, with read and write, such as sockets, pipe or the like

The efficiency of the system call you write depends on the size of buf and the total number you want to write, if buf is too small, the number of times you get into kernel space increase, the efficiency is low.

The fwrite will do it for you cache, reducing the actual occurrence of the system call, so the efficiency is relatively high.

 

If you call only once (possible?), Maybe about the same, strictly speaking, to write a little bit faster (because in fact fwrite finally took write into the file system to do real work)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Next is the original:

    When viewing srs_rtmp open source library, find the file operation, are read and write. Curious why not use fread and fwrite it? I looked at his company's Code (IPC's do) and found that is the case. Ever since going online to find the answer, then I think < If the program is limited to memory, with a read / write better. > This is what I was looking for answers in this share. Code pound a day, strong Chinese people.

Guess you like

Origin blog.csdn.net/qq_16810885/article/details/93043285