Linux system Programming--linux system call

Source: Internet
Author: User
Tags types of functions

System Invocation Overview

A system call , as the name implies, is a set of "special" interfaces that the operating system provides to user program calls. This "special" interface can be used by the user program to obtain the services provided by the operating system kernel, such as a file system-related call request system to open files, close files or read and write files, can be clock-related system calls to obtain system time or set timers.


Logically, a system call can be seen as an interface between a kernel and a user-space program-it is like an intermediary that communicates the request of the user process to the kernel, and then sends the processing results back to the user space after the kernel has finished processing the request.


The fundamental reason why system services need to be made available to user space through system calls is to "protect" the system because we know that the Linux runtime is divided into kernel space and user space , each running at different levels. Logically isolated from each other. Therefore, user processes do not normally allow access to kernel data, nor can they use kernel functions, which can only manipulate user data in user space and invoke user-space functions . For example, we are familiar with the "Hello World" program (execution) is a standard user-space process, it uses the print function printf is a user-space function, the printed character "Hello word" string also belongs to the user spatial data.

However, in many cases, the user process needs to obtain system services (call System program), then must take advantage of the system provided to the user's "special Interface"-system call, its particularity is mainly to specify the user process into the specific location of the kernel; in other words, the user access to the kernel path is predetermined, You can only enter the kernel from the specified location, without allowing you to jump into the kernel arbitrarily . With such a centralized access path that is trapped in the kernel to ensure that the kernel is safe and correct. We can visually describe this mechanism : as a tourist, you can buy a ticket to enter the safari park, but you must honestly sit on the sightseeing car and follow the prescribed route for a sightseeing tour. Of course, don't get off, because it's too dangerous, not to let you lose your life, is to scare the wild animals

implementation of system calls

System calls are part of the operating system kernel and must be provided to the process in some way for them to be called. The CPU can run at different privilege levels, and the corresponding operating system has different runlevel, user state and kernel state. Processes running in the kernel state can access a variety of resources without restriction, while the various actions of user processes in the user state are limited, such as the inability to access memory arbitrarily, the inability to open and close interrupts, and the privilege level of switching operations. Obviously, system calls that belong to the kernel must be running in the kernel state, but how do I switch to the kernel State ?


The answer is a software outage . The difference between software interrupts and what we often say about interrupts (hardware interrupts) is that it is triggered by software instructions and not by the peripheral, that is, an exception developed by the programmer (the exception is a normal exception). The operating system typically switches from the user state to the kernel state via software interrupts.

Interrupts have two important properties, the interrupt number and the interrupt handler. The interrupt number is used to identify different interrupts, and different interrupts have different interrupt handlers. An interrupt vector table is maintained in the operating system kernel (Interrupt vector tables), which stores the addresses of all interrupt handlers, and the interrupt number is the offset of the corresponding interrupt in the interrupt vector table. For more detailed instructions, see the principle of system call implementation.


the difference between a system call and a library function

There are two ways to manipulate files under Linux: System call and library function call (library functions).


Library functions consist of two types of functions :

1) No call to system calls

You do not need to switch to kernel space to complete functions, and feedback the results to the application, such as strcpy, bzero and other string manipulation functions.

2) Call system call required

You need to switch to kernel space, which can be implemented by encapsulating system calls, such as printf, fread, and so on.



System calls take time, and frequent use of system calls in programs can reduce the efficiency of a program's operation. When running kernel code, the CPU is operating in the kernel state, it needs to save the user-state stack and the memory environment before the system call occurs, then goes into the kernel state work. After the system call is over, switch back to the user state again. This kind of environment switching consumes a lot of time.


When the library function accesses the file, it sets different types of buffers as needed, thus reducing the number of calls to the IO system directly and improving the access efficiency. For more information on buffer, see "Talking about standard I/O buffers".



This process is similar to the courier to a certain area (kernel space) Courier delivery, Courier has two ways to send:

1) to a courier immediately sent to the destination, one to send a piece, which leads to go back and forth more frequent ( system call )

2) wait for the courier to save the post (buffer), only once sent to the destination ( library function call )


Transferred from: http://blog.csdn.net/tennysonsky/article/details/45101303

Linux system Programming--linux system call

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.