Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the specifications for Linux kernel source code programming

2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what are the specifications of Linux kernel source code programming". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what are the specifications of Linux kernel source code programming" can help you solve the problem.

1. It is highly recommended that the width of a single row is eighty columns.

Any statement with a row greater than eighty columns should be split into multiple rows, unless parts of more than eighty columns improve readability and do not hide information. However, do not split strings that are visible to the user, such as printk information, into multiple lines, as this will cause the information not to be found when using grep.

two。 About curly braces

Curly braces are used in if,do, while, and for statements in the C language. Kernel code tends to place the left parenthesis at the end of the line, the right parenthesis at the beginning of the line, and the curly braces and the preceding statements, as well as if and the following statements, all leave a space.

3. About spaces

This should be listed separately to explain, because there are too many spaces in the kernel code.

Linux kernel-style spaces are mainly used for keywords, that is, adding a space after the keyword. Notable exceptions are keywords that look like functions, such as sizeof, typeof, alignof, attribute, which are used in Linux with a pair of parentheses, such as sizeof (int).

So you need to add a space after the following keywords:

If, switch, case, for, do, while

However, there is no need to add spaces after sizeof, typeof, alignof, attribute:

S = sizeof (struct file)

When declaring a pointer or a function that returns a pointer, the position of the asterisk should be close to the variable or function name, not the type name, for example:

Char linux_banner; unsigned long long memparse (char ptr, char * retptr); char match_strdup (substring_t * s)

Add a space around the binary and ternary operators, for example:

= +-

< >

* /% | & ^ =!?:

But do not add spaces after the unary operator:

& * + ~! Sizeof typeof alignof attribute defined

4. Variable naming

C is a simple and rough language, so your naming should also be concise. The definition of variables in the linux kernel should be as simple as possible, and without ambiguity, the simpler the better. Underscores can be used, but capital letters are absolutely not recommended. Therefore, do not use hump nomenclature for variable and function definitions in the kernel.

5. Function

Functions should be short and short, and a function should only do one thing. It is not recommended that hundreds of lines of code form a function.

It is best to leave comments in front of the key function.

6. Annotation

The recommended format for multiline comments is as follows:

/ *

To support ISA shared interrupts, we need to have one interrupt

Handler that ensures that the IRQ line has been deasserted

Before returning. Failing to do this will result in the IRQ

Line being stuck active, and, since ISA irqs are edge triggered

No more IRQs will be seen. , /

7. It is recommended to use function self-annotation

The so-called function self-annotation is that you can guess what you want to do from your function name, such as the kernel:

Wait_event (), wait_event_interruptible (), wait_event_interruptible_timeout (), etc.

Note that writing code is not only for yourself now, but also for yourself in the future, but also for others. If you look back at the code you wrote a year ago, it shows that there is something wrong with your code specification.

8. Constant macros and enumerations are named in uppercase 9. Print kernel or driver information

Writing debugging information is a huge challenge, and once you have completed it, it will be of great help to remote debugging

Many subsystems have the Kconfig debug option to open-DDEBUG in the corresponding makefile, or define the macro # define DEBUG in the file. When debugging information can be printed unconditionally, or the # ifdef segment related to debugging has been compiled, then printk (KERN_DEBUG.) It can be used to print debugging information.

10. Inline function (inline)

The Inline keyword causes the compiler to insert the specified function body and replace each place (context) where the function is called, saving extra time for each call to the function. However, the proliferation of the inline keyword makes the kernel larger, which slows the entire system down, because the larger kernel takes up more CPU cache and results in fewer available memory page caches. Imagine that a page cache miss would result in a disk addressing, which takes at least 5 milliseconds. 5 milliseconds is enough for CPU to run a lot of instructions.

This is the end of the introduction to "what are the specifications for Linux kernel source code programming". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report