The difference between Linux system calls and standard library calls detailed parsing _unix Linux

Source: Internet
Author: User
Tags function prototype

1. The relationship between system call and library function
The system call enters the kernel state from the user state through a soft interrupt int 0x80. Some functions in the function library call the system call.
A function in a function library can call a system call without invoking it, or it can invoke multiple system calls. Programmers can invoke system calls through the function library.

Advanced programming can also directly use INT 0x80 to enter system calls without having to pass the function library as an intermediary. If you are programming at the core, you can also enter the system call via int 0x80, and you cannot use the function library at this time. Because functions in the function library are not accessible by the kernel.

2, from the user call library functions to the system call the execution of the process.
1) Suppose the user invokes ssize_t write (int fields, cont void *buff, size_t nbytes), library function.
2 The library function performs an int 0x80 interrupt. Because interrupts allow the process to enter the kernel state from the user state, the parameters are transmitted through registers.
3 The interrupt routines for 0x80 interrupts are called system call handler.

Its work is:
I. Store most registers into the kernel stack. This is written in assembly code.
Ii. perform a real system call function ――system calls service routine. This is C code.
III. Return via Ret_from_sys_call () to the library function of the user state. This is the assembly code.

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.

2, library function calls
standard C library functions provide file manipulation functions such as fopen, Fread, Fwrite, Fclose, Fflush, fseek, etc. 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), whose action object is file pointer file *pf, to write a file, You must first open a file with the Fopen function with writable permissions to obtain 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 system calls, the file structure pointers that are obtained for each open file have a kernel-space document descriptor FD corresponding to it. 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 general files in your application. Library function calls are system independent, so portability is good. Because library function calls are based on the C library, it is also not possible to manipulate devices in kernel-space drivers

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.