The difference between Linux system calls and library function calls

Source: Internet
Author: User
Tags function prototype linux

1. System call

The functions provided by the system call such as open, close, read, write, IOCTL, etc., need to include header file Unistd.h. Take write as an example: its function prototype is size_t write (int fd, const void *buf, size_t nbytes), and its action object is file descriptor or file handle FD (file descriptor), to write a file, You must first open a file with an open system call with writable permission to obtain the FD of the open file, such as Fd=open (\ "/dev/video\", O_RDWR). FD is an integer value, each new open a file, the obtained FD is the current maximum FD plus 1. The Linux system has 3 file descriptor values assigned by default: 0-standard input,1-standard output,2-standard error.

System calls are typically used for underlying file access (low-level file access), such as direct access to device files in the driver.

System calls are operating system-related, so there is generally no portability across the operating system.

System calls occur in kernel space, so if you use system calls for file operations in a generic application in user space, there is the overhead of user space switching to kernel space. In fact, even if you use library functions to manipulate files in user space, because files always exist on the storage medium, the operation of the hardware (memory), regardless of the read-write operation, is bound to cause system calls. That is, the operation of the library function on the file is actually implemented through system calls. For example, the C library function fwrite () is implemented through a write () system call.

In this case, the use of library functions also has the overhead of system calls, why not use the system call directly? This is because the read-write file is usually a large amount of data (this is a large number of data manipulation units that are implemented in relation to the underlying driven system call), and the use of library functions can greatly reduce the number of system calls. This result is also due to buffer technology. In the user space and the kernel space, the file operation uses the buffer, for example writes the file with the fwrite, is writes the content to the user space buffer first, when the user space buffer is full or writes the operation end, only then writes the user buffer content to the kernel buffer, the same reason, The kernel buffer content is written to the corresponding hardware media of the file when the kernel buffer is full or the write ends.

Related Article

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.