Linux System Programming Basics (II) C standard IO library functions and unbuffered IO functions

Source: Internet
Author: User

Let's take a look at how the C standard I/O library functions are implemented using system calls.

fopen (3)

Calling open (2) Opens the specified file, returns a file descriptor (that is, an int type number), assigns the file structure, which contains information such as descriptor, I/O buffer, and current read/write location, and returns the address of this file structure.

FGETC (3)

A descriptor for the file is found by passing in the filename parameter, I/O buffers and current read and write locations, to determine whether the next character can be read from the I/O buffer, or to return the character directly if read, otherwise call read (2), and the file description Fu in to allow the kernel to read the file's data to the I/O buffer , and then returns the next character. Note that for the C standard I/O library, the open file is identified by the file * pointer, and for the kernel, the open file is identified by the document descriptor, the file descriptor is obtained from the open system call, and the file descriptor is required when using the read, write, close system calls.

FPUTC (3)

Determine if there is room for the I/O buffer for the file to hold one character, and if there is room, save it directly in the I/O buffer and return, if the I/O buffer is full, call write (2) so that the kernel writes the contents of the I/O buffer back to the file.

Fclose (3)

If there is still data in the I/O buffer that has not been written back to the file, call write (2) writeback, and then close (2) closes the file, releasing the files structure and I/O buffers.

In the case of write files, the relationship between C standard I/O library functions (printf (3), Putchar (3), fputs (3)) and system call write (2) is shown in the following illustration.

Hierarchical relationship between library functions and system calls

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.