Linux system calls, buffered Io, unbuffered IO

Source: Internet
Author: User

1. What is a system call?

The Linux kernel sets up a set of subroutines that are used to implement various system functions, called system calls.

  Linux system calls are provided as part of the C library, and users can invoke them in their own applications through system invoke commands.

#include <linux/unistd.h>/* All system calls need this header */.

2, the difference between the system call and the general library function

The general library function is always running in the user state, and the system call is going through the INT 0x80 statement into the kernel state, the function of the system call is actually done by the corresponding kernel function.

3. Process of System call

Shows the process of a system call

1, in our own program called a library function xyz ();

2, the library function implementation, first do some preparatory work (including the system call number, parameters, etc. into the corresponding register), ready to call int 0x80, into the kernel

3, the kernel first calls the System_call () function, the parameter check, according to the system call number to obtain the corresponding system call service routines (corresponding kernel functions)

4. Execute the kernel function

5. After the kernel function is executed, go to Ret_from_sys_call () routine and return to user space

6, the library function implementation in accordance with the return value, and then determine the return value of the library function

7, the library function returns, the entire system call completes.

4. Buffered io and unbuffered IO

Unbuffered IO: Don't be fooled by names, actually there are buffers in the kernel actually. In the case of write, if the kernel buffer is 100, and each write 10, then write 10 times the kernel function writes the data to disk. The reason for this is that disk IO is a very time-consuming thing to do with Buffering to improve system performance. Similarly, if read is called, the kernel reads the memory data into the kernel buffer one time, and the next time it calls read, check that the kernel buffers have no data required, and if no disk IO is made.

Buffered IO: In the user space there is also a buffer, take fwrite as an example, if the kernel buffer is 100, the user space buffer is 50, each time fwrite10, then fwrite 5 times after the system call write, A total of two write systems call the kernel function to make disk IO. That is, through the buffer of user space, can reduce the number of system calls, improve IO performance.

  

The following is an introduction to the kernel buffers

switching between kernel mode and user mode takes time, In contrast, disk I/O operations consume more time, and in order to increase efficiency, the kernel also uses buffer technology to increase the speed of access to the disk. A disk is a collection of blocks, and the kernel buffers the chunks of data on the disk. The kernel copies the data blocks on the disk into the kernel buffers, and when a process in a user space reads data from disk, the kernel typically does not read the disk directly, but instead copies the data in the kernel buffer into the buffer of the process. When the data block required by the process is not in the kernel buffer, the kernel joins the corresponding data block to the request queue, suspends the process, and then serves other processes. After a period of time (in fact a short time), the kernel reads the corresponding block of data from the disk into the kernel buffer, then copies the data into the process's buffer, and finally wakes up the suspended process.

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.