Differences between Linux and library function calls

Source: Internet
Author: User

 

 

There are two methods for file operations in Linux: System Call and library function call ). See LinuxProgramDesign (the original English version is beginning Linux programming, prepared by Neil Matthew and Richard stones) Chapter 3: Working with files. System calling actually refers to the underlying call. In the Linux program design, it refers to the underlying call. It is designed for hardware. Library Function calls are intended for application development, which is equivalent to the application API. There are many reasons for using this method. First, the implementation of double buffering technology. Second, portability. Third, some performance defects of the underlying call itself. Fourth: Let the API have a level and specialized work orientation.

 

1. System Call

Functions provided by the system call, such as open, close, read, write, and ioctl, must contain 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), and its operation object is file descriptor or file handle FD (file descriptor ), to write a file, you must first open a file with the write permission called by the open system to obtain the fd of the opened file, for example, FD = open (\ "/dev/video \", o_rdwr ). FD is an integer value. Each time a new file is opened, the obtained FD adds 1 to the current maximum FD. By default, three file descriptors are allocated in Linux: 0-standard input, 1-standard output, and 2-standard error.

System calls are usually used to access the underlying file (low-level file access). For example, you can directly access the device file in the driver.

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

System calls occur in the kernel space. Therefore, if you use system calls in common applications in the user space to perform file operations, there will be overhead for switching from the user space to the kernel space. In fact, even if the user space uses library functions to operate on files, because files are always stored in the storage media, read/write operations are all performed on hardware (memory, will inevitably cause system calls. That is to say, the operations of library functions on files are actually implemented through system calls. For example, the C library function fwrite () is implemented through the write () System Call.

In this case, the use of library functions also has a system call overhead. Why not directly use the system call?This is because reading and writing files is usually a large amount of data (this large amount is relative to the data operation unit implemented by the underlying driver's system call, using library functions can greatly reduce the number of system calls.This result is due to the buffer technology. In user space and kernel space, buffer is used for file operations. For example, if you use fwrite to write files, the content is first written to the user space buffer, when the user space buffer is full or the write operation ends, the user buffer content is written to the kernel buffer. In the same way, when the kernel buffer is full or the write ends, the kernel buffer content is written to the corresponding hardware media of the file.

 

2. library function call

File operation functions provided by standard C library functions, such as fopen, fread, fwrite, fclose, fflush, and fseek, must contain the header file stdio. h. Taking fwrite as an example, its function prototype is size_t fwrite (const void * buffer, size_t size, size_t item_num, file * PF), and its operation object is file pointer file * PF, to write a file, you must use the fopen function with the write permission to open a file and obtain the file structure pointer PF, for example, pF = fopen (\"~ /Proj/filename \ ", \" w \"). In fact, because library functions ultimately implement file operations through system calls, each file structure pointer obtained by opening a file has a file descriptor FD corresponding to the kernel space. There are also predefined file pointers: stdin-standard input, stdout-standard output, and stderr-standard error.

Library Function calls are usually used to access general files in applications.

Library Function calls are system-independent, so they are portable.

Because the library function is called Based on the C library, it is impossible to use it for device operations in the driver of the kernel space.

 

  ※Function library call vs system call
function library call System Call
in all ansi c compiler versions, C library functions are the same system calls vary with operating systems
it calls a program (or function) in the function library it calls the system kernel service
contact the user program is an entry point of the Operating System
run in user address space run in the kernel address space
its running time belongs to" user time " its running time belongs to" system "time
it is a process call with a low call overhead switch between user space and kernel context, high overhead
there are about 300 functions in the C function library libc about 90 system calls in UNIX
Typical C function library call: System fprintf malloc Typical System Call: chdir fork write BRK;
 

 

Address: http://www.spridu.cn/article/Linux_unix/2011/02/17/linux-systemcall-distinction-libraryfunction_9178.shtml

 

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.