Differences between Linux system calls and library function calls

Source: Internet
Author: User
Tags function prototype

There are two ways to manipulate files under Linux: System call and library function call (library functions). The system call actually refers to the bottom-level invocation, which is the meaning of the underlying call in the Linux programming. The hardware is oriented. The library function call is for the application development, the equivalent of the application API, in such a way there are many reasons, first: the implementation of double buffering technology. The second is portability. Thirdly, some performance aspects of the underlying invocation itself are flawed. IV: Let the API also have a level and dedicated work-oriented.

1. System call

System calls provide functions such as open, close, read, write, IOCTL, etc., which need to include the header file Unistd.h. Write for example: Its function prototype is size_t write (int fd, const void *buf, size_t Nby TES), whose action object is a file descriptor or file handle FD (descriptor), to write a file, you must first open a file with writable permission to obtain the FD of the open file, such as Fd=open (\ "/dev/video\", O_ RDWR). FD is an integer value, with each new file opened, the FD obtained is the current maximum FD plus 1. The Linux system is assigned a default of 3 file descriptor values: 0-standard input,1-standard output,2-standard error.

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

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

System calls occur in kernel space, so if you use system calls for file operations in a generic application in user space, there is a cost of user-space-to-kernel-space switching. In fact, even if the library function is used in user space to manipulate the file, because the file always exists on the storage medium, both the read and write operations, which are the operations of the hardware (memory), will inevitably cause system calls. In other words, the operation of a library function on a file is actually implemented through a system call. For example, the C library function fwrite () is implemented through the write () system call.

In this case, using library functions also has the overhead of system calls, so why not use system calls directly? This is because the use of library functions can greatly reduce the number of system calls, since the read-write file is usually a large amount of data (which is a large number of data manipulation units implemented relative to the underlying driver's system call). This result is also due to the buffer technology. In the user space and kernel space, the use of the file operation of the buffer, for example, write a file with fwrite, the content is written to the user space buffer, when the user space buffer full or the end of the write operation, the contents of the user buffer is written to the kernel buffer, the same reason, The kernel buffer content is written to the hardware medium that corresponds to the file when the kernel buffer is full or the write is complete.

2. Library function call

The standard C library functions provide file operation functions such as fopen, Fread, Fwrite, Fclose, Fflush, fseek, etc., including header file stdio.h. In fwrite, for example, the function prototype is size_t fwrite (const void * Buffer, size_t size, size_t item_num, File *PF), the object of the action is the document pointer to the filename *PF, to write a file, you must first open a file with a writable permission with the fopen function to obtain the file structure pointer pf of the open files, For example pf=fopen (\ "~/proj/filename\", \ "w\"). In fact, since the operation of the library function on the file is ultimately implemented through a system call, the file structure pointer that is obtained for each file opened has one kernel space corresponding to the document descriptor FD. There is also a corresponding predefined file pointer: Stdin-standard input,stdout-standard output,stderr-standard error.

Library function calls are typically used for access to generic files in an application.

Library function calls are system-independent, so portability is good.

Because library function calls are based on the C library, it is not possible to use the device in the kernel space driver.

※ Function library Call VS system call

  

Differences between Linux system calls and library function 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.