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

C language in 01:Linux Kernel of self-cultivation of embedded C language

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

1.1 Strange syntax in Linux kernel drivers

When you look at some GNU open source software, or read the Linux kernel and driver source code, you will find that there are a large number of C programs that look "weird" in the Linux kernel source code. Say it is C language, it seems to be different from the way it is written in the textbook; it is not C language, but these programs are really in a C file. At this point, you must suspect that what you are seeing is a "fake C language"!

For example, the following macro definition:

# define mult_frac (x, numer, denom) (\ {\ typeof (x) quot = (x) / (denom);\ typeof (x) rem = (x)% (denom);\ (quot * (numer)) + ((rem * (numer)) / (denom)) \) # define ftrace_vprintk (fmt Vargs)\ do {\ if (_ _ builtin_constant_p (fmt)) {\ static const char * trace_printk_fmt _ _ used\ _ attribute__ ((section ("_ _ trace_printk_fmt") =\ _ builtin_constant_p (fmt)? Fmt: NULL;\\ _ ftrace_vbprintk (_ THIS_IP_, trace_printk_fmt, vargs);\} else\ _ ftrace_vprintk (_ THIS_IP_, fmt, vargs);\} while (0)

Character-driven padding:

Static const struct file_operations lowpan_control_fops = {.open = lowpan_control_open, .read = seq_read, .write = lowpan_control_write, .llwritten = seq_lseek, .release = single_release,}

The macro definition that implements the printing function in the kernel:

# define pr_info (fmt,...) _ _ pr (_ _ pr_info, fmt, # # _ VA_ARGS__) # define pr_debug (fmt,...) _ _ pr (_ _ pr_debug, fmt, # # _ _ VA_ARGS__)

You read it correctly, these are actually C language, but not the standard C language syntax, but some C language syntax extended by the GNU C compiler used by our Linux kernel. These grammars are generally not mentioned in C language textbooks or materials, so you will feel familiar and strange, and it looks "weird". We often encounter these "strange" uses when we are doing Linux driver development or reading the Linux kernel source code, and if we do not understand the specific meaning of these special syntax, it may cause some obstacles to the understanding of the code.

This tutorial is to lead you to understand some C language special syntax extensions commonly used in Linux kernel or GNU open source software, to eliminate the syntax reading barrier and confusion brought by these extension features when reading Linux kernel or GNU open source software.

1.2 C language standards and compilers

Before entering the formal course, let's popularize the concept of C standard. When learning the C language, you may have more or less seen the word "ANSI C" in teaching materials or materials. Maybe I didn't care too much at that time, but in fact, "ANSI C" means the C language standard.

What is the C language standard? The real world we live in is made up of various standards, and it is these standards that make our society operate in an orderly manner. For example, when we cross the road, the traffic rule we follow is a standard: stop at red light, go at green light, wait a minute when yellow light is on. When both pedestrians and drivers follow this default standard, our transportation system will run smoothly. The USB interface in computers is also a standard. When everyone's USB products follow the communication standard of USB protocol, our mobile phones, U disks, USB cameras and USB network cards can plug and unplug each other on all kinds of computer devices. 2G, 3G and 4G are also standards. When baseband chips made by different manufacturers follow this communication standard, only phones of different brands and operating systems we use can call each other, send Wechat to each other and like each other.

Similarly, the C language has its own standards. We know that C language programs need to be compiled through a compiler to generate binary instructions before they can be run on our computers. In the early days of the release of the C language, when the major compiler manufacturers developed their own compilers, they developed and maintained their own compilers for a long time. This creates a situation in which a program written by a programmer is compiled on one compiler and fails in another. According to their own habits, no one is satisfied with each other, just like in the Spring and Autumn and warring States period: different currencies, different weights and measures, and different characters are all Chinese, and it is very troublesome to communicate because the standards are not unified. it's not a way to go on like this.

Later, ANSI (AMERICAN NATIONAL STANDARDS INSTITUTE: American National Standards Institute, referred to as ANSI) came out and joined with ISO (International Standards Organization) to convene various compiler manufacturers and technical groups to have tea and meet together to start the standardization of C language. During this period, various leaders are also full of contradictions and disputes, but they live up to their efforts. After difficult negotiations, they finally reached an agreement in 1989 and issued the C language standard, and then made some improvements the following year. So, just as Qin Shihuang unified the six Kingdoms, characters and weights and measures, the C language standard finally came out! Because it was released in 1989, it is generally called the C89 or C90 standard, or ANSI C.

1.3 C standard content

What does the C standard mainly say?

C standard English documents, hundreds of pages, said a lot of things, but to sum up, mainly some grammatical conventions of C language programming, such as:

Define various keywords, data types, define various operation rules, priority of various operators and associative data type conversion variables scope function prototype function nested layer function parameter limit standard library function

After the release of the C standard, everyone abides by this standard: when programmers develop programs, they write according to this standard; when compiler manufacturers develop compilers, they also parse and translate programs according to this standard. Different compiler manufacturers support a unified standard, so that programs written by everyone, using different compilers, can be compiled and run correctly, which greatly improves the efficiency of program development and promotes the development of the IT industry.

1.4 the development process of C standard

C standard is not immutable, just like mobile communication, it is constantly changing from 2G, 3G, 4G to 5G. The C standard has also gone through the following four stages:

Kendr CANSI CC99C11

Kindr C

Kraft R C is also commonly referred to as traditional C. Before the C standard was unified, Dennis Ritchie, the author of the C language, and Brian Kernighan co-wrote a book, the C programming language. Early programmer programming, this book can be said to be an absolute authority. This book is thin and concise, and mainly introduces the basic use of C language. Later, the second edition of C programming language came out and made some changes: for example, adding unsigned int, long int, struct and other data types; changing the operator = + / =-to + = /-= to avoid some ambiguity and Bug caused by the operator. This book can be seen as a prototype of the ANSI standard. However, the early C language is still very simple, such as no standard library functions defined, no preprocessing commands and so on.

ANSI C

ANSI C is a standard issued by ANSI (American National Standards Institute) on the basis of Kendr C, which unifies the different standards of major compiler manufacturers, and extends the syntax and features of C language. This standard is also known as C89/C90, and it is also the C language standard supported by various compilers by default. The following features are mainly added to ANSI C:

Add signed, volatile, const keywords, add void* data type, add preprocessor commands to add wide characters, wide strings define the C standard library.

C99 standard

The C99 standard is a new standard issued by ANSI in 1999 on the basis of the C89 standard. The standard extends the ANSI C standard, such as adding some keywords to support new data types:

Boolean: _ Bool complex: _ Complex imaginary: _ Imaginary inline: inline pointer modifier: restrict supports long long, long double data types support variable-length arrays allow assignment to structure-specific members support hexadecimal floating-point numbers, float _ Complex and other data types.

In addition, the C99 standard also draws lessons from some advantages of other languages and makes a series of improvements to syntax and functions, which greatly facilitate programmers to develop programs, such as:

Variable declarations can be placed anywhere in a code block. ANSI C stipulates that the declaration of variables should be written at the beginning of the function statement, otherwise a compilation error will be reported. Now there is no need to write this, where you need to use variables, where you can declare them directly; the source program supports a maximum of 4095 bytes per line. This seems to be enough, and no program is so complex that an one-line program has 4KB characters; supports / / single-line comments. ANSI C is not as convenient to use / * * / as C++ 's / / annotation, so the new C99 standard has borrowed from it and began to support this annotation method; some header files have been added to the standard library, such as stdbool.h, complex.h, stdarg.h, fenv.h and so on. True and false are often returned in C language. In fact, these are also the bool types defined in C++. Then why do we often write this, but the programmer compiles the program without an error? this is because in the early days, everyone used the VC++6.0 series, the C++ compiler. Another possibility is that some IDE encapsulate the data of this data type.

C11 new standard

The C11 standard is the latest C language standard released in 2011. It modifies some Bug of the C language standard and adds some new features:

Add _ Noreturn to declare that the function has no return value; add _ Generic: support generic programming; modify some Bug of standard library functions: for example, the gets () function is replaced by the gets_s () function; add file lock function; support multithreading;...

From the revision of the C11 standard, we are gradually aware of the future development trend of the C language: C language is now drawing lessons from the advantages of the current programming language and constantly adding it to its own standard. For example, modern programming languages such as multithreading, string and generic programming are supported by the latest standards of the C language. But if it goes on like this, we can only look forward to whether the C language can still maintain its elegant feature of "simplicity is beauty". But at least for now we don't have to worry about this, because most compilers don't support the newly released C11 standard, so we don't need it for the time being.

1.5 Compiler support for C standards

Standards are one thing, and whether various compilers support it or not is another. We should be clear about this. Just like mobile phones, mobile phones released in different periods have different support for communication standards. Early phones may only support 2G communications, but later they support 3G. Now most of the new phones released support 4G and are compatible with 2G/3G.

Now the 5G standard is under development and is about to be released. It is said that it will be released in 2019 and commercially available in 2020. However, there are no mobile phones that support 5G communications, just as there is no compiler that supports the C11 standard.

Different compilers even have different support for C standards. Some compilers only support ANSI C, which is currently the default C standard. Some compilers can support C99 or some features of the C99 standard. At present, the best support for the C99 standard is the GNU C compiler, which is said to support 99% of the new features of the C99 standard.

1.6 Compiler's extension of C Standard

Different compilers, for the needs of development environment, hardware platform and performance optimization, will not only support the C standard, but also do some extensions themselves.

Using C language to develop programs on 51 single-chip microcomputer, we often use Keil for C51 integrated development environment. You will find that Keil for C51 or other C compilers in IDE will make a lot of extensions to the C language standard. For example, add various keywords:

Data:RAM low 128B space, single-cycle direct addressing; code: represents the program storage area; bit: bit variable, commonly used to define the P0~P3 pin of the single-chip microcomputer; sbit: special function bit variable; sfr: special function register; reentrant: reentrant function declaration.

If you use these keywords in your program, your program can only be compiled and run using the 51 compiler, and you will not be able to use other compilers, such as VC++6.0.

By the same token, the GCC compiler has made many extensions to the C standard:

Zero-length array statement expression built-in function attribute special attribute declaration label element case range.

For example, zero-length arrays are supported. These new features are not supported by the C standard at present, nor do other compilers. If you define a zero-length array in the program:

Int a [0]

You have to use the GCC compiler to compile correctly, but you may not be able to compile with the VC++ 6.0compiler, because Microsoft's C++ compiler does not support this feature.

1.7 main contents of this tutorial

The syntax of GCC's own extension will be widely used in GNU open source software and Linux kernel, which will bring some obstacles and troubles to our understanding of open source software and Linux kernel code. This tutorial mainly introduces some common syntax and use of GNU C extensions to the C standard. The ultimate goal is to understand the application of these special grammars in Linux kernel drivers and GNU open source software, and to remove the troubles and obstacles that these special grammars bring to our understanding of kernel code.

1.8 the learning environment required for this tutorial

In this tutorial, you will use commands such as arm-linux-gnueabi-gcc to compile and disassemble the program. So before you take this tutorial, make sure you have the following Linux environment or source code on your computer:

Linux learning environment: Ubuntu, Fedora, etc.; arm-linux-gnueabi-gcc cross-compilation tool; Linux kernel source code: Linux 4.4.xU-boot-2016.09 source code

Remarks

If you do not have a Linux learning environment at hand, you can also install C-Free learning in the Windows environment. The C language sample program in the tutorial can also be compiled under the C-Free environment. Of course, here, it is recommended that you use a virtual machine to install a Linux learning environment, a good environment is more conducive to our learning, if you have any doubts in the installation process, you can join the QQ group (475504428), we Wechat official account: otaku tribe (armlinuxfun), to participate in the technical discussion.

This tutorial is adapted from the C language embedded Linux advanced programming video tutorial No. 04, the electronic version of the book can join the QQ group: 475504428 download, more embedded video tutorials, you can follow 51CTO College-teacher Wang Litao: http://edu.51cto.com/sd/d344f

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

Servers

Wechat

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

12
Report