General file I/O usage suggestions

Source: Internet
Author: User

You can call the posix_fadvise function to set the method of some operation files. For example, you can use the following code to clear the cache in a certain region:

Int main (){
Int FD = open ("test", o_rdwr );
Int ret = posix_fadvise (FD, 0, 10, posix_fadv_dontneed );
Printf ("% d \ n", RET );
If (ret =-1 ){
Printf ("posix_fadvise call failed! \ N ");
}
Close (FD );
Return 0;
}

After this function is called, the kernel recycles the specified range from the page buffer, that is, clearing the cache. Some people use this to eliminate the impact of cache when testing I/O efficiency. The value of advice can be the following:

  1. Posix_fadv_normal: no comments;
  2. Posix_fadv_random: The program intends to read and write randomly. The kernel disables the pre-read function and reads the minimum amount of data each time;
  3. Posix_fadv_sequentiphosphatase: the kernel doubles the pre-read size by sequential access;
  4. Posix_fadv_willneed: in the near future, the program will access this segment of content, and the kernel will enable pre-reading to read them;
  5. Posix_fadv_noreuse: If you plan to access the service only once in the future, but the kernel behavior is like 4;
  6. Posix_fadv_dontneed: in the near future, applications do not intend to access pages in the specified range. The kernel deletes the specified range from the page buffer.

In the case of posix_fadv_willneed, Linux has its own unique interface, which is defined as follows and is not specific to the example:

ssize_t readahead(int fd, off64_t offset, size_t count);

In some cases, these suggestions greatly increase efficiency. For example, when playing a video file, the played content can not be placed in the memory, and the memory can be provided to other programs, in this case, you can set posix_fadv_dontneed. Or set posix_fadv_willneed when reading files in sequence, so the next access will not be blocked.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.