In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
On the question of whether linux device drivers use global variables
Today, someone asked me whether linux device drivers use global variables, and why try not to use global variables?
In fact, this question is said from two aspects:
1) the use of global variables destroys the reentrancy of the function, and locks have to be used to protect global variables in order to achieve function reentry. This efficiency has become inefficient.
Function reentry, mainly the use of local variables, that is, variables on the thread's stack, each using their own, there will be no conflict. The other is to use locks to protect the integrity of the data.
2) using global variables violates the writing principle of linux drivers. That is, a driver should support driving multiple devices to work at the same time. For example, a motor driver may operate multiple motors at the same time, rotating in multiple axes, and one motor controls one axis, such as x-axis, y-axis, and z-axis. If global variables are used in the driver to store device information, it is impossible to support saving multiple device information at the same time. If you use a linked list, a linked list node holds a device information, although it is feasible, but every time the kernel operates on your driver's api, you have to traverse the list and compare object pointers to find the information about the device you operate with each time the kernel passes in the struct device (such as struct i2c_client, which contains struct device). Obviously inefficient.
So in fact, the right way is:
In probe, a private structure is assigned to hold the state information for this device. Then call the dev_set_drvdata series functions to bind your assigned structure to the struct device passed in during probe. The struct device is created by the bus driver when a new device is plugged in.
When the later system calls the api function of your driver, you pass in the struct device, and you retrieve your assigned private structure (containing the status information of the specific device, such as i2C address, etc.) through the dev_get_drvdata series function, so as to know which specific device you are operating.
For details, please refer to my "linux device driver Model" video.
Https://edu.51cto.com/course/17159.html
And my video "how to write a device driver" or "go deep into the linux kernel"
Https://edu.51cto.com/course/17132.html
Https://edu.51cto.com/course/17155.html
In addition, for my related training video, please see:
Welcome to all the courses I have released: https://edu.51cto.com/lecturer/8896847.html
Links to my new and more discounted packaged courses are as follows:
Https://edu.51cto.com/sd/0a9d4
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.