How the kernel space communicates with the user space

Source: Internet
Author: User

How the kernel space communicates with the user space

here are a summary of 7 ways to The main thing is to do a programming implementation in a way that was not familiar before, so as to deepen the impression .

1. using the API: This is the most common way to use

A. Get_user (x,ptr): called in the kernel, gets the value of the specified address of the user space and saves it in the kernel variable x.

B. Put_user (x,ptr): is called in the kernel to save the value of the variable x of the kernel space to the specified address at the user space.

C. Copy_from_user ()/copy_to_user (): Mainly used in device-driven read and write functions, triggered by system calls.

2. Using the proc file system: similar to the Sysfs file system, it can also be used as a means of interacting with the kernel space and user space.

The/proc file system is a virtual file system that can be used as a Linux kernel space and user space. Unlike normal files, the contents of the virtual files here are created dynamically.

The way to use the/proc file system is simple. Call Create_proc_entry, return a proc_dir_entry pointer, and then fill in the structure that the pointer points to, and the test case below me just fills in the Read_proc property.

Here is a simple test case, read the virtual out of the file can get the kernel space passed over the "Proc!" Test by qiankun! String

3. using the Sysfs file system +kobject: in fact, this was the implementation of the previous program, but that day too nervous to forget, t_t. Each kobject registered in the kernel corresponds to a directory in the SYSFS system. You can obtain the appropriate information by reading the files in the Sys directory under the root directory. In addition to the Sysfs file system and the proc file system, some other virtual file systems can achieve the same effect.

4.netlink:netlink Socket provides a set of BSD-like APIs for the IPC of the user state and kernel state. NetLink has several advantages over other user-and kernel-state IPC Mechanisms: 1. Complete data exchange using a custom protocol, no need to add a file, etc. 2. Multi-point transfer can be supported. 3. Support the kernel to initiate the session first. 4. Asynchronous communication, support caching mechanism.

For user space, the use of NetLink is relatively simple, because and the use of sockets very similar, the following is the use of kernel space for NetLink, mainly about the most important create function, the function prototype is as follows:

extern struct sock *netlink_kernel_create (struct net *net,

Int unit,unsigned int groups,
                                   void (*input) (struct Sk_buff *skb),
                                           struct Mutex *cb_mutex,
                                            struct module *module);

The first parameter is typically passed in &init_net .

The second parameter refers to the type of NetLink, the system defines 16, and we'd better define it if we use it. This needs to be consistent with the third parameter of the creation socket used by the user space before communication can be completed.

The fourth parameter refers to a callback function that is called when a message is received. The parameters of the callback function are structs of struct sk_buff type. By analyzing its structure members can get the data passed over

The sixth parameter is typically passed in as a this_module. Refers to the current module.

Here is a simple test for netlink, which outputs the string "NetLink test by Qiankun" to the kernel by NetLink, and the kernel then returns the string. The NetLink type uses 22.

5. document : It should be said that this is a rather clumsy practice, but it can be used in this way. When in the kernel space, directly manipulate the file, the information you want to pass to the file, and then the user space can read the file can get the desired data. The following is a simple test program, in the kernel state, the program will write a string to the "/home/melody/str_from_kernel" file, and then we read this file in the user state, we can get the data sent from the kernel state.

6. Using the MMAP system call: you can map the address of the kernel space to the user space. Used to do embedded in the past several times. On the one hand, the MMAP function pointer in the struct FILE_OPERATIONS structure can be modified in driver to re-implement the mapping operation of a file. On the other hand, you can also directly open the/dev/mem file to map a page in physical memory to an address in process space.

In fact, in addition to rewriting the mmap function in the struct file_operations, we can also rewrite other methods such as IOCTL to achieve the way to drive kernel space and user space communication.

7. Signal: sends a signal from the kernel space to the process. This is often encountered, the user program has a major error, the kernel sends a signal to kill the corresponding process.

The code is as follows:

From for notes (Wiz)

List of attachments

    How the kernel space communicates with the user 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: 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.