Differences between Linux system calls and library function calls

Source: Internet
Author: User

Http://www.cnblogs.com/yanlingyin/archive/2012/04/23/2466141.html

There are two ways to manipulate files under Linux: System call and library function call (library functions). can refer to "Linux Programming" (the original English version of "Beginning Linux Programming", the author is Neil Matthew and Richard Stones) Chapter III: Working with files. 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. Take write as an example: its function prototype is size_t write (int fd, const void *buf, size_t nbytes), whose operand is a file descriptor or file handle, FD (file descriptor), to write a file, You must first open a file with a writable permission with open system call to obtain the FD for the file you opened, 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., which need to include header file stdio.h. Take fwrite as an example, its function prototype is size_t fwrite (const void *buffer, size_t size, size_t item_num, File *PF), its action object is the file pointer files *pf, to write a file, You must first open a file with a writable permission with the fopen function to get the file structure pointer pf of the open files, such as 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
function Library Calls System calls
In all ANSI C compiler versions, the C library functions are the same System calls are different for each operating system
It calls a program (or function) in the library. It calls the service of the system kernel
Associated with the user program is an entry point for the operating system
Execute in user address space Executing in the kernel address space
Its run time is "User Time" Its run time is "system" time
is a procedure call and the call cost is small Need to switch between user space and kernel context environment, overhead is large
There are about 300 functions in the C function library libc There are about 90 system calls in Unix
Typical C function library calls: System fprintf malloc Typical system call: chdir Fork Write brk;

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.