In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Linux user mode communicates with kernel mode. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Linux user state and kernel state Due to the limitations of CPU permissions, communication is not as simple as imagined using inter-process communication. Today, this article will take a look at what communication methods Linux user state and kernel state have.
When we write code, we usually access kernel space through system call functions in user space, which is the most common way to communicate between user mode and kernel mode. (See xx for Linux user mode and kernel mode)
In addition, there are four ways:
procfs(/proc)
sysctl(/proc/sys)
sysfs(/sys)
netlink socket
procfs(/proc)
Procfs is an abbreviation for process file system. It is essentially a pseudo-file system. Why is it a pseudo-file system? Because it does not take up external storage space, it only takes up a small amount of memory and is usually mounted in the/proc directory.
One of the files we see in that directory is actually a kernel variable. The kernel uses this directory to present its internal information in the form of files, which is equivalent to the/proc directory building a bridge between the user mode and the kernel mode. The files under the user mode read and write/proc are the configuration parameters related to the kernel.
For example,/proc/cpuinfo,/proc/meminfo, and/proc/net provide CPU, memory, and network parameters, respectively. In addition, there are many parameters, as follows:
root@ubuntu:~# ls /proc/1 1143 1345 1447 2 2292 29 331 393 44 63 70 76 acpi diskstats irq locks sched_debug sysvipc zoneinfo10 1145 1357 148 20 23 290 332 396 442 64 7019 77 asound dma kallsyms mdstat schedstat thread-self1042 1149 1361 149 2084 2425 291 34 398 45 65 7029 8 buddyinfo driver kcore meminfo scsi timer_list1044 1150 1363 15 2087 25 3 3455 413 46 66 7079 83 bus execdomains keys misc self timer_stats1046 1151 1371 16 2090 256 30 35 418 47 6600 7080 884 cgroups fb key-users modules slabinfo tty1048 1153 1372 17 21 26 302 36 419 5 67 71 9 cmdline filesystems kmsg mounts softirqs uptime11 1190 1390 18 22 27 31 37 420 518 6749 72 96 consoles fs kpagecgroup mtrr stat version1126 12 143 182 2214 28 32 373 421 524 68 73 97 cpuinfo interrupts kpagecount net swaps version_signature1137 1252 1434 184 2215 280 327 38 422 525 69 74 98 crypto iomem kpageflags pagetypeinfo sys vmallocinfo1141 13 144 190 2262 281 33 39 425 5940 7 75 985 devices ioports loadavg partitions sysrq-trigger vmstat
As you can see, there are a lot of files represented by numbers. These are actually the process files running in the current system. The numbers represent the process number (PID). Each file contains all the configuration information of the process, including the process state, file descriptor, memory mapping, etc. We can look at the following:
root@ubuntu:~# ls /proc/1/attr/ cmdline environ io mem ns/ pagemap schedstat stat timersautogroup comm exe limits mountinfo numa_maps personality sessionid statm uid_mapauxv coredump_filter fd/ loginuid mounts oom_adj projid_map setgroups status wchancgroup cpuset fdinfo/ map_files/ mountstats oom_score root/ smaps syscall clear_refs cwd/ gid_map maps net/ oom_score_adj sched stack task/
To sum up, the kernel exposes its own system configuration information through files one by one. Some of these files are read-only, some are writable, and some are dynamically changing, such as process files. When an application reads a certain/proc/file, the kernel will register this file, and then call a group of kernel functions to process it. The corresponding kernel parameters are copied to the user mode space, so that users can read this file and obtain the kernel information. An approximate illustration is as follows:
sysctl
Sysctl is a Linux command we are familiar with, man sysctl can see its function and usage. It is mainly used to modify kernel runtime parameters, in other words, it can dynamically modify kernel parameters during kernel operation.
It essentially uses file read and write operations to complete the communication between user state and kernel state. It uses a subdirectory of/proc/sys. The difference between procfs and procfs is:
Procfs mostly outputs read-only data, whereas sysctl outputs mostly writable information.
For example, it is common to obtain whether the kernel network layer allows IP packets to be forwarded through cat /proc/sys/net/ipv4/ip_forward, and set the kernel network layer to allow IP packets to be forwarded through echo 1 > /proc/sys/net/ipv4/ip_forward or sysctl -w net.ipv4.ip_forward=1.
Linux also provides the file/etc/sysctl.conf for bulk modification.
sysfs
sysfs is a virtual file system introduced in Linux 2.6, and its practice is to complete the communication between user mode and kernel through file/sys. Different from procfs, sysfs separates some parts about devices and drivers originally in procfs and presents them to users in the form of "device tree."
sysfs not only reads device and driver information from kernel space, but also configures devices and drivers.
Let's see what's under/sys:
# ls /sysblock bus class dev devices firmware fs hypervisor kernel module power
You can see that these files are basically related to computer equipment and drivers. More on the interpretation of these documents you can understand, here is not too much to expand.
netlink
Netlink is one of the most common ways Linux user mode communicates with kernel mode. Linux kernel version 2.6.14 is supported. It is essentially a socket, and the standard API used by regular sockets also applies to it. For example, to create a netlink socket, you can call the following socket function:
#include #include #include netlink_socket = socket(AF_NETLINK, socket_type, netlink_family);
Netlink is flexible enough to allow it to be used in messaging systems between the kernel and a variety of user processes, such as routing subsystems, firewalls (Netfilters), ipsec security policies, etc.
Extension:
The net-tools tool uses procfs(/proc) and ioctl system calls to access and change kernel network configuration parameters, while iroute 2 communicates with the kernel through the netlink socket interface, the former having been phased out and the latter becoming standard.
Thank you for reading! About "Linux user state and kernel state communication ways what" this article is shared here, I hope the above content can have some help for everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.