In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
13.1 Summary
The previous 12 lessons mainly analyze some common C language grammars of GNU C extensions in the Linux kernel. These extended grammars of GNU C are mainly used to improve C language standards and compilation optimization. Through the development of C standard, we find that C standard will be absorbed in time for some features extended by compilers, or good features and syntax in other programming languages (such as C++). As a new C language standard.
Among these extended syntax of GNU C, attribute and macro definition are two main features. In embedded underlying systems, especially in Linux kernel and U-boot, attribute attributes extended by GNU C are widely used to assist the implementation of some underlying mechanisms or to achieve some compiler optimizations. In terms of macro definition, through the characteristics of statement expressions and variable parameter macros, we can define a high-quality macro with complex functions, security and reliability.
Some of the features mentioned in this tutorial are often encountered in the actual work or when reading the Linux kernel driver source code. After mastering the use of these extended features, you will know how to analyze them when you encounter similar "exotic C language" programs later. In addition, GNU C has some other extended features, because they do not use much in the kernel, or just do some compilation optimization, even if we do not know it will not affect our understanding of the code, limited to space, so we will not talk about it for the time being, such as the following features.
Attribute declaration: const attribute declaration: constructor, destructor attribute declaration: noreturn attribute declaration: used, unused local tag nesting function.
If you encounter similar extensions in the future, you can go to the following websites to have a look.
GNU C Grammar extension GCC Compiler Manual 13.2 C language exercises Test
Here are some C language exercises that you can do. See if you can really master it after learning this tutorial. If you have any questions, you can discuss with me through the reader circle or join the QQ group (475504428).
1. The following programs, compiled and run in different compilation environments, such as C-Free, VC++6.0, and TurboC, are the same results, and why?
# includeint main (void) {printf ("size:% d\ n", sizeof (int); return 0;}
two。 Define a macro to find the minimum of two numbers.
3. Compile the following program into an executable file a.out that can be run on the ARM platform, and disassemble it to see the address of the variable global_val.
Int global_val = 10 int uninit_val;int main (void) {int local_val = 20; return 0;}
In an engineering project, there are two source files as follows, analyze the running results of the following program.
/ / func.c
Int a = 10
Int b
Int c attribute ((weak)) = 30
/ / main.c
Int a
Int b = 20
Int c = 40
Int main (void)
{
Printf ("a:% d\ n", a)
Printf ("b:% d\ n", b)
Printf ("c:% d\ n", c)
Return 0
}
5. Define a variable parameter function to achieve grade printing control: ERROR, DEBUG, INFO. Use these three macros to represent the level of printing, such as defining ERROR, only printing error information; defining DEBUG, printing errors and debugging information; defining INFO, all printing information is printed out.
6. Define a variable parameter macro to achieve hierarchical printing control: ERROR, DEBUG, INFO. Use these three macros to represent the level of printing, such as defining ERROR macros, only printing error information; defining DEBUG, printing errors and debugging information; defining INFO, all print information is printed.
7. Here are some macro definitions in the Linux kernel (Linux4.4.0). Please analyze the functionality they implement.
# define pr_emerg (fmt,...)\ printk (KERN_EMERG pr_fmt (fmt), # # _ _ VA_ARGS__) # define pr_alert (fmt,...)\ printk (KERN_ALERT pr_fmt (fmt), # # _ _ VA_ARGS__) # define pr_crit (fmt,...)\ printk (KERN_CRIT pr_fmt (fmt), # # _ VA_ARGS__) # define pr_err (fmt )\ printk (KERN_ERR pr_fmt (fmt), # # _ _ VA_ARGS__) # define pr_warning (fmt,...)\ printk (KERN_WARNING pr_fmt (fmt), # # _ _ VA_ARGS__) # define pr_warn pr_warning#define pr_notice (fmt,...)\ printk (KERN_NOTICE pr_fmt (fmt), # # _ _ VA_ARGS__) # define pr_info (fmt ...)\ printk (KERN_INFO pr_fmt (fmt), # # _ _ VA_ARGS__)
8. During the Linux kernel startup process, there is often a line of information at the end of starting log.
Freeing unused kernel memory: 468K
Use the section attribute declaration in this course to analyze the kernel initialization and memory release process behind this log.
9. In embedded Linux driver development, there is no main () entry function in the driver module. Please use the knowledge learned in this course to analyze how the driver runs and initializes.
10. Driver analysis: in the linux4.4 source code linux-4.4/arch/arm/mach-footbridge/ebsa285.c, analyze the meaning of the following code and the role of the container_of macro.
MACHINE_START (EBSA285, "EBSA285") / * Maintainer: Russell King * / .atag _ offset = 0x100, .video _ start = 0x000a0000, .video _ end = 0x000bffff, .map _ io = footbridge_map_io, .init _ early = footbridge_sched_clock, .init _ irq = footbridge_init_irq, .init _ time = footbridge_timer_init, .restart = footbridge_restart MACHINE_ENDstatic void ebsa285_led_set (struct\ led_classdev * cdev, enum led_brightness b) {struct ebsa285_led * led = container_of (cdev, struct ebsa285_led, cdev) If (b = = LED_OFF) hw_led_state | = led- > mask; else hw_led_state & = ~ led- > mask; writeb (hw_led_state, xbus);} static enum led_brightness\ ebsa285_led_get (struct led_classdev * cdev) {struct ebsa285_led * led = container_of (cdev, struct ebsa285_led, cdev); return hw_led_state & led- > mask? LED_OFF: LED_FULL;} 13.3concluding remarks
Through the study of this course, coupled with the practice of the 10 exercises in this section, I believe that your C language skills must have been deepened. With this knowledge base, it basically removes the reading barrier of the Linux kernel. I believe that in the future work, study will be increasingly sophisticated, continue to make breakthroughs!
Finally, I wish you all a smooth work and happy study!
In addition, if you want to systematically learn a certain piece of knowledge in the Linux kernel, or what knowledge is not mastered very well, you want to advanced study, but limited to work, study busy, time and energy is limited, can not systematically learn, you can also contact me to communicate
My QQ:3284757626) my blog: www.zhaixue.cc I will continue to write relevant knowledge and tutorials to serve you.
This tutorial is adapted from the C language embedded Linux Advanced programming Video tutorial No. 05, the electronic version of the book can join the QQ group: 475504428 download, more embedded video tutorials, you can follow:
Official account of Wechat: Otaku tribe (armlinuxfun)
51CTO College-Mr. 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.
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.