Data exchange between linux kernel and user layer and linux Kernel

Source: Internet
Author: User

Data exchange between linux kernel and user layer and linux Kernel

1. There are basically System Call interfaces file_operation interfaces: read write ioctl?

Module param of module driver (module parameters of module driver ).

2. kernel startup parameters :?
By identifying the parameters passed by bootloader?
_ Setup ("para_name =", parse_func)

3. asynchronous notification?
Special netlink?
The advantage of netlink is that it can transmit a group of messages to the user layer, while asynchronous signal notifications can only transmit one flag for notifications, rather than a group of messages (data ), do I need to call the system interface at the application layer after the notification?
It is very troublesome to get this message from the underlying layer. But can netlink directly upload a group of messages?

4. Use debugfs

5. Use sysfs

6. Use procfs

7. relayfs :?
Relayfs is a file system for fast data forwarding (relay ).

8. Is communication using shared memory or shared files basically not limited by the number of descriptors?
Idea: the underlying layer creates a private file and transmits fd to the upper layer. The upper layer uses a select or epoll to listen to this descriptor. The underlying layer writes the message to this file every time there is a message, then use a flag to notify the upper layer ,?
After the upper layer detects the file, it reads the file according to the mutually defined structure. In this way, a communication can be completed.

Netlink and 8 are asynchronous notifications. Generally, they are suitable for sending data from the underlying layer to the upstream layer at irregular intervals.

Summary of kernel and user space information interaction Methods :?
Summarize some common kernel interaction methods for user State information .?
1. When does the kernel state need to interact with the user State ??
During system-level development, such as device driver development and kernel function modules, information is often required between the kernel and user State. Linux provides multiple methods to achieve interaction between kernel state and user State based on different situations. Including kernel startup parameters, module parameters, sysfs, sysctl, system calls, netlink, procfs, seq_file, debugfs, and relayfs. In addition, there are other such parameters, such as brk () system calls, signal, memory ing mechanism, and so on.

2. Brief Introduction to various methods?
(1) kernel startup parameters: linux provides a function to transmit startup parameters to the kernel through bootloader. The general method is to discuss a function for parsing parameters, and then call the macro _ setup () provided by the kernel to register it to the kernel, __setup () defined in linux/init. in h :?
# Define _ setup (str, fn )\?
_ Setup_param (str, fn, fn, 0 )?
Str is the name of the parameter to be parsed. fn is the function for analyzing the parameter value. It is responsible for converting the value of the parameter to the value of the corresponding kernel variable and setting the kernel variable .?
Note: a program that runs before the operating system kernel runs BootLoader. You can initialize hardware devices and create a memory space ing map to bring the system's hardware and software environment to a proper state so that the correct environment can be prepared for the final call to the operating system kernel .?
(2) module parameters and sysfs: the kernel subsystem or device driver can be directly compiled into the kernel, or compiled into a module. If it is compiled into the kernel, you can use the kernel startup parameters to pass the parameters to them through the kernel startup parameters. If the parameters are compiled into modules, you can use the command line to pass the parameters when inserting modules, or at runtime, use sysfs to set or read module data .?
(3) Sysctl: This is an effective way for users to set and obtain runtime Kernel configuration parameters, you can change the Kernel configuration parameters at any time when the system is running, or you can obtain the Kernel configuration parameters at any time. Generally, these Kernel configuration parameters also appear in the/proc/sys directory. user applications can directly use the files in this directory to perform read and write operations on Kernel configuration parameters .?
(4) System Call: This is the interface provided by the kernel to the application. Most of the underlying hardware operations of applications are implemented through system calls .?

(5) Netlink: A special socket exclusive to linux. Netlink is a very good way to transmit two-way data between the kernel and your applications. Users can use the powerful functions provided by netlink by using standard socket APIs, the kernel state requires a dedicated kernel API to use netlink .?
(6)/proc:/proc is an early mode of user-state data interaction with kernel-state data. You can obtain a lot of kernel information from this file system, it is easy for users to read and modify. However, in fact, most kernel parameters provided by the kernel are read-only .?
(7) Seq_file: Generally, the kernel creates a file in the procfs file system to provide output information to the user space. The user space can view the file information through any text reading application, however, procfs has a defect. If the output content is greater than one memory page, it needs to be read multiple times, so it is difficult to process it. In addition, if the output is too large, the speed is slow, sometimes there may be unexpected situations, so developers have implemented the seq_file function, making it easier for the kernel to output large file information .?
(8) Debugfs: In the test version of the kernel, some debugging information is often output to the user space. printk is very easy to use, but when you want to change some behavior of the kernel, printk is very weak, therefore, debugfs is a virtual file system. You can create one or more files in debugfs to provide information to user space applications .?
(9) Relayfs: This is a file system for fast data forwarding (relay, it mainly provides a fast and effective forwarding mechanism for tools and applications that need to forward a large amount of data from the kernel space to the user space .?
In the preceding methods, both the kernel startup parameter and seq_file pass one-way information, and only information can be transmitted to the kernel. Others can be used for Bidirectional Information interaction, but the simplest and most efficient one is netlink .?

3. Other information interaction Methods :?
(1) brk (): when there is a clear buffer location that provides user space, the kernel and user space interaction data mainly use the get_user () and put_user () routines, however, if the user space buffer is not clear, get_user () and put_user () cannot be used again. In this case, you need to borrow the brk system call and the current process space, brk is used to set the heap space size for processes. Each process has an independent heap space. The dynamic memory allocation functions such as malloc actually obtain the memory in the heap space of the process. We will use brk to expand a new temporary buffer in the heap space of the current process, and use put_user to export the kernel data to this determined user space .?
(2) signal: the purpose of the signal in the kernel is mainly to notify the user program of major errors and forcibly kill the current process. At this time, the kernel sends a SIGKILL signal to notify the process to terminate, the kernel sends signals using the send_sign (pid, sig) routine. You can see that the process ID (pid) must be known before sending the signals ), therefore, to asynchronously notify a user process to execute a task by sending a signal from the kernel, you must know the process Number of the user process in advance. It is troublesome to find the process number of a specific process during the kernel running, and it may be necessary to traverse the entire process control block chain list. Therefore, the method of notifying specific user processes by signal is very bad, which is generally not used in the kernel. The use of signals in the kernel only occurs when the current process is notified (the pid can be conveniently obtained from the current variable) to perform some general operations, such as terminating the operation. Therefore, this method is of little use to kernel developers .?
(3) memory ing mechanism: linux provides a memory ing mechanism to allow user processes to directly access the memory. It is to map the memory space of a specific part in the kernel to the memory space of a user-level program, so that the kernel shares a memory space with the user space. This method is generally used in applications with strong real-time requirements or a large amount of interactive data .?
There are still many ways to interact the kernel with user space data. Information Interaction initiated by user-level programs, whether using standard calling or through the driver interface, generally, system calls are required. There are not many situations where information interaction is actively initiated by the kernel. During program design, the kernel is a passive information provider for user-mode applications, therefore, we should follow a reasonable principle to choose which information interaction method to use.

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.