Differences between library functions and system calls

Source: Internet
Author: User
Relationship between system calls and database functions

The system call enters the kernel state from the user State through Soft Interrupt int 0x80. Some functions in the function library call the system call.
Functions in the function library can be called either without calling the system or multiple systems. Programmers can call the system through the function library.
Advanced Programming can also directly use int 0x80 for system calling without using the function library as an intermediary. If it is in the core programming, you can also use int 0x80 to enter the system call. At this time, you cannot use the function library. Because the functions in the function library cannot be accessed by the kernel.
2. The process from the user to the system call and execution.
1) assume that the user calls ssize_t write (INT fields, cont void * buff, size_t nbytes); library function.

2) The database function will interrupt the execution of int 0x80. Because the process enters the kernel state from the user State, the parameters are transmitted through registers.
3) The Interrupt Routine corresponding to the 0x80 interrupt is called the system call handler. I. Stores most registers into the kernel stack. This is written in assembly code.
Ii. Execute the real system call function-system call service routine. This is the C code. Iii. Return to the user State Library Function Through ret_from_sys_call. This is assembly code. 1. 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, such as 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 call is based on the C library, it is impossible to use it for device operations in the driver program of the kernel space.

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.