Linux kernel (System), understanding of functions, program debugging of macros

Source: Internet
Author: User


1, operating system 1.1, the components of the Linux kernel (System):

The kernel mainly consists of five parts: process scheduling, memory management, virtual file system, network interface and process communication.

(1) Process scheduling

Process scheduling is the CPU's scheduling algorithm for CPU access to multiple processes, making the process macro-parallel execution. Common scheduling algorithms are: first come first service principle, high priority priority, short process first (who executes short time, on), time slice rotation (each process gives point time to run).

All processes of the system must use the process scheduling algorithm, so it is in the center position.

(2) Memory management

The primary role of memory management is to manage memory between processes.

Generally for 32-bit Linux systems, the Linux process has its own independent 4GB of memory space, 0~3GB user space, 3~4GB kernel space.

(3) Virtual file system

The Linux virtual file system (VFS) is an abstraction of the actual file system, unifying the interface for all devices. Display file system: For example, there are FAT16, FAT32, NTFS, yaffs, jffs2,ext,ext2, so many different formats of the system, different formats have their own set of files operation data structure (file_operation), So when writing the application layer code, do you want to do different processing for so many file systems, this time, the virtual file system is generated, the virtual file system Unified management of these real file systems, and then provide a unified interface, so that the application layer can use the unified open Read Write To visit the.

(4) Network interface

Network interfaces enable access to a variety of network standards and support for a variety of network hardware. So the network interface can be divided into network protocols and network drivers.

The network protocol realizes each kind of network transmission protocol, for example the TCP/IP protocol is how to implement specifically,

Network driver: Actually is the network hardware device drive, realizes this part of the hardware drive.

(5) Process communication

Linux interprocess communication (IPC), which contains: pipelines (nameless pipes and named pipes), signals, message queues, shared memory, sockets, semaphores.

1.2. C Library function and API

For an out-of-the-out API, there are APIs provided by the operating system and C libraries. Like the

File operation: Open, write, read, close. and the C library provides the interface: fopen, fwrite, Fread, fclose. Essentially, fopen is two times encapsulated using the open function.

For different systems, the system provides almost the API is different, and for the C library, the different platform as long as the use of C library, C library API is the same, so for portability, C Library of high portability.


2. Understanding of functions

Typically, a return value exists for a child function that is large or small, and the return value of the function is generally not directly using the void type. The reason why a function will execute a return value even if it is not called is:

(1) First, in the C language, generally, the return value is zero, is generally normal exit, and the return value is negative one case, is an exception, and the return value is the integrity of the words, then the general is their own definition.

return 0 ;     return -1;     return 1;

(2) Return statement, then it means that the code of the function here is all over.

(3) The return value of the function and the input parameters can be understood as the raw material and the product after processing (the function body).

3, null understanding (1) NULL definition
#ifdef __cplusplus #define Null    0#else  /* __cplusplus */#define null    ((void *) 0)#endif  / * __cplusplus */#endif/  * NULL */

When the __cplusplus macro is defined, NULL is zero, that is, when the compiler is C + +, NULL is zero, but the C compiler, then NULL is (void *) 0. Here 0 can be understood as: digital zero and address zero, specific environment specific analysis.

(2) The role of NULL

let the wild pointer point to Safety 0 Address

When a generic pointer is defined:

int *p = NULL;

It points p to address zero, because generally 0 addresses are not accessible at random.

(3) NULL and ' + ', ' 0 ', 0
'  / ' 0 ' 0 '  - 0  0.

Attention:

' + ': is the sign of the end of the string.

' 0 ': ASCII that corresponds to 0 of this character, typically used to obtain zero ASCII values.

0: Numbers,

NULL: typically used for pointers.

4, the Macro debugging

The method of macro debugging is very convenient,

Application debugging:

#ifdef DEBUG #define DBG (...) fprintf (stderr, "dbg (%s,%s (),%d):", __file__, __function__, __line__); fprintf (stderr, __va_args__)#else#define DBG (...) #endif

Prints the current file, function, line number.

__va_args__: is a string that converts the actual strings inside the DBG brackets. When the debug definition is canceled, the subsequent debug information doing will not be compiled to print out the information.


Debugging of the kernel:

#define DEBUG (FMT, args ...)     PRINTK (FMT, # #args)#else#define DEBUG (FMT, args ...)     Do {} while (0)#endif

...: three-point variable, that is, the number of parameters is variable. For the understanding of the argument in another post do a detailed introduction.

Linux kernel (System), understanding of functions, program debugging of macros

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.