Why don't you understand the Linux kernel driver source code?

Source: Internet
Author: User
Tags knowledge base

Learning Embedded Linux driver development, the most important skill is the ability to write Linux kernel driver, deep understanding of the Linux kernel. The basis for this step is that you have to understand the Linux kernel source code, understand its basic framework and implementation, understand the use of its kernel API, and then according to their own needs to write high-quality kernel drivers.

said easy to do, many new people, and even work 1-2 years of developers just contact the Linux kernel, don't write, look at the kernel code may be a face confused: Obviously is the C language, but just do not understand what the meaning, in addition to the function name, function parameters, function return value and comments, to understand the basic function of the entire function outside , once the details of the analysis, you will find that the difficulty, each line of code seems unfathomable, as if there is a great energy, so you turn books, Baidu, Google, step-by-step forward.

This is a lot of beginners commonly used learning methods, including me, just contact the Linux kernel driver, see a lot of déjà vu, but carefully scrutinized and very unfamiliar kernel code, heart is broken, heart is virtual: obediently, this is what thing, why do not understand, I have IQ problems? I can't read it alone. Even after work I have consulted a lot of work experience of colleagues, want to ask some of the experience and methods of reading the kernel, you will find that many of them in fact, not in-depth study of the kernel, in addition to their own module is more familiar with (register configuration, Data flow), for other modules also have little time and energy to pay attention to, Even the framework of this module is rarely concerned, of course, most of it is the relationship between energy and time, the demand for workload requires you to quickly complete the task through a variety of APIs. Even some colleagues will say, look at the Linux kernel like the fog to see the flowers, hazy a little better, not to look at, the more you see the less understand.

But, to write high-quality programs, the Linux kernel, module understanding must be deep, otherwise, you will stay on the periphery, mend. According to my various learning experience and experience, the Linux kernel is not indestructible, offensive, mastered the correct learning methods and knowledge base, we can also swim in the kernel code, the Linux kernel in the C language of a variety of wonderful applications and powerful skills, the Linux kernel of a variety of complex framework, Subsystems can also be jiangshan and confident.

What are the knowledge reserves and skills you need to learn Linux to drive and analyze Linux kernel sources?

1) C language base + data structure

linux kernel, in other words, is made up of various structures, function pointers, lists, and queues. So before you go into the Linux kernel, your C-language foundation must be strong: what function pointers, pointer functions, array pointers, pointer arrays, and various pointers as function parameters, return values, and so on are all clear, because the Linux kernel uses these extensively. These are the foundation, now make confused, see the kernel is dizzy. For C-pointer advanced learning, you can focus on video tutorials: C language Embedded Linux advanced programming

2) syntax extension for C language

These "Weird C code" that you're looking at is actually a GCC compiler's extended syntax for standard C: statement expressions, local tags, __attribute__ attribute declarations, variable parameter macros, and so on. The syntax for these GCC extensions is widely used in the Linux kernel and in the driver source code, especially when it comes to setting up the underlying startup and compiling links. If you do not grasp the use of these extended C language syntax, in reading the Linux kernel source code, or drive the process, you may encounter a lot of obstacles to our understanding of the code caused a variety of interference.

So before going to read the Linux source code, it is recommended to first learn the GNU C Common extended syntax for standard C: Wang Litao's talent Lesson or video tutorial: c language embedded Linux Advanced programming phase 5th: C Standard and GNU C extension

After studying this, to remove the grammar barriers to read the Linux source code, then you can choose a small module of their own interest: the first to leave this module to the user's API to play, learn to program, and then slowly study the implementation of its inner core. From the bottom to the top, open the two-pulse, and then to analyze the kernel, other complex systems, also comprehend by analogy, relatively easy to get started.

3) Object-oriented thinking in the Linux kernel

With the above foundation, we analyze a small Linux kernel module, there is no problem. When encountering a large complex subsystem, such as USB subsystem, memory management, MTD, file system, structure inside nested multi-layer structure, all kinds of device, bus, driver, various layers, is not a bit around halo? A kind of elephant, lost in the forest feel, can not grasp the "overall". At this time, we can not use the C language of the process-oriented thinking,the design of the Linux kernel actually uses a lot of object-oriented thinking。 Therefore, we should learn to use object-oriented thinking to analyze the Linux kernel, analysis of the reuse of each module, so it is very convenient to build a system in the mind frame and level. Then use the process-oriented thinking to analyze the specific functions of the implementation, specific details, spend more time and energy, I believe you will have a different harvest. About the common data structure and object-oriented thinking of the Linux kernel, you can focus on the tutorial:C language Embedded Linux Advanced programming phase 7th: Data structure and data encapsulation in the Linux kernel

Embedded Communication QQ Group: 475504428

Public Number: Otaku Tribe

Sina Weibo: @ Otaku Tribe


Why don't you understand the Linux kernel driver source code?

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.