In terms of device drivers, linux2.6 has significant changes compared with linux2.4, which mainly shows that many new features (such as memory pool), sysfs file system, and kernel module have been added to the kernel API. O changes. ko, Driver Module compilation method, module usage count, module loading and uninstalling Function Definition, etc.
The Linux kernel consists of five subsystems: process scheduling, memory management, virtual file system, network interface, and inter-process communication. For example:
In device-driven programming, when the requested resources cannot meet the requirements, the driver generally schedules other threads to execute and changes the process corresponding to the driver to sleep until the requested resources are released, will be awakened and the anniversary is ready.
In device-driven programming, If you need several concurrent tasks, you can start the kernel thread. The function for starting the kernel thread is: int kernel_thread (INT (* fN) (void *), void * Arg, unsigned long flags );
Linux memory management completes the conversion from virtual memory to physical memory for each process. Generally, each process in Linux has 4 GB of memory, 0-3 GB belongs to the user space, and 3-4 GB belongs to the kernel space, kernel space has different processing methods for general memory, I/O device memory, and high-end memory
The Virtual File System hides the details of various hardware and provides unified interfaces for all devices. Moreover, it is independent of specific file systems and is an abstraction of various file systems. It uses super block to store information about the file system and inode to store the physical information of files, use the directory item dentry to store the logical information of the file
In Linux, the control transfer from user space to kernel space can only be completed through system calls and hardware interruptions.
C Programming in Linux
1. Zero-length Array
An array of zero length is allowed in gnu c. They are very useful as the last element of the struct. In ISO c89, You need to allocate a length of 1 to the directory, which means a waste of space or make the malloc parameters complicated. In ISO c99, you can use flexible array elements, but there are minor differences in syntax and doctrine.
· Flexible array elements are written as contents [] without 0.
· Flexible array elements have incomplete types, so the sizeof operation may not be applied. Sizeof is assigned a value of 0 as an odd feature of the original implementation of a zero-length array.
Flexible array elements can be used as the last element of the struct when other elements are not empty. GCC now allows a zero-length array anywhere. Define a struct that contains only one zero-length array. In any case, you may encounter problems. We recommend that you use a zero-length array only when the flexible array element is allowed. For example:
# Include <stdio. h>
# Include <string. h>
Struct arr
{
Int;
Char buff [0];
};
Int main ()
{
Char B [0];
Struct arr * PTR;
PTR = (struct arr *) malloc (sizeof (struct ARR) + 4 );
If (PTR = NULL)
{
Printf ("malloc failed/N ");
Return-1;
}
Memset (PTR, 0x30, sizeof (struct ARR) + 4 );
Printf ("0x % x 0x % x 0x % x 0x % x/N", PTR-> buff [0], PTR-> buff [1], PTR-> buff [2], PTR-> buff [3]);
Printf ("% d/N", sizeof (struct ARR), sizeof (INT), sizeof (B [0]);
Return 0;
}
Malloc allocates the size of the data structure struct arr plus four bytes. In fact, sizoef (struct ARR) = 4 = sizoef (INT) does not include the size of the zero-length array, therefore, after memset, the buff content is 0x30, and sizeof (B [0]) = 1.
2. Case range
Gnu c supports case x... The number of [x, y] in the range meets the case condition, for example:
Switch (CH)
{
Case '0 '... '9': C-= '0 ';
Break;
Case 'A '... 'F': C-= 'a'-10;
Break;
}
3. Statement expression
Gnu c regards a compound statement that contains parentheses as an expression, called a statement expression, which can appear anywhere the expression can appear. We can use loop variables and local variables that can only be used in composite statements in statement expressions. For example:
# Include <stdio. h>
# Define min_t (type, x, y )/
({Type _ x = (x); Type _ y = (y); _ x <_ y? _ X: Y ;})
Int main ()
{
Int IA, IB, mini;
Float FA, FB, minf;
IA = 1;
IB = 2;
Fa = 1.1;
Fb = 2.2;
Mini = min_t (INT, ++ IA, ++ Ib );
Minf = min_t (float, ++ FA, ++ FB );
Printf ("Mini = % d/nminf = % F/N", Mini, minf );
Return 0;
}
4. typeof keywords
The typeof (x) statement can obtain the X type. Therefore, we can use typeof to redefine min_t.
# Define min_t (x, y )({/
Const typeof (x) _ x = (x );/
Const typeof (y) _ y = (y );/
(Void) (<__ x ==<__ y );/
_ X <_ y? _ X: _ y ;})
5. Macro of variable parameters
Standard C only supports variable parameter functions. For example, the prototype of printf is
Int printf (const char * Format {, argument }...);
In gnc c, macros can also accept variable parameters, such:
# Define pr_debug (FMT, Arg ...) Printfk (FMT, # Arg)
"#" Indicates that processing Arg does not represent any parameters. In this case, the preceding comma becomes redundant. After "#" is used, the gnu c Preprocessor discards the preceding comma.
6. Label Elements
The method for specifying an array is to add "[Index] =" before the initialization value. You can also use "[first... Last] = "to specify a range. For example:
Unsigned char data [Max] = {[0... MAX-1] = 'A '};
7. Current function name
Gnu c predefines two signs to save the name of the current function, __function _ to save the name of the function in the source code, __pretty_function _ to save the name with language characteristics, in the C function, the two names are the same:
Void example ()
{
Printf ("this is function: % s", _ function __);
Printf ("this is function: % s", _ pretty_function __);
}
Output: Example example
8. Special attribute Declaration
Gnu c allows special properties of functions, variables, and types for manual code optimization and custom code check methods. To specify a declared attribute, you only need to add _ attribute _ (attribute) after the declaration )). Attribute is an attribute description. If multiple attributes exist, they are separated by commas. Gnu c supports more than 10 attributes, including noreturn, format, section, aligned, and packed.
The noreturn attribute acts on the function, indicating that the function never returns. This will allow the compiler to optimize the code and eliminate unnecessary warning information. For example:
# Define attrib_noret _ attribute _ (noreturn ))
Asmlinkage noret_type void do_exit (long error_code) attrib_noret
The format attribute page is used for functions, indicating that parameters in the printf, scanf, or strftime style are used. specifying the format attribute allows the compiler to check the parameter type based on the format string, for example:
Asmlinkage int printk (const char * FMT ,...) _ Attribute _ (format (printf, 1, 2 )));
The first parameter in the above Code is a format string. Starting from the second parameter, parameters are checked according to the format string rules of the printf function.
The unused attribute acts on functions and variables, indicating that the function or variable may not be used. This attribute can prevent the compiler from generating warning information.
The aligned attribute is used for variables, struct, or consortium. It specifies the peer mode of a variable, struct, or consortium, in bytes, for example:
Struct example_struct
{
Char;
Int B;
Long C;
}__ Attribute _ (aligned (4 )));
Indicates that the variable of the struct is bounded by 4 bytes.
The packed attribute acts on variables and types. When used for variables or struct members, it indicates the use of the smallest possible bounds. When used for enumeration, struct, or consortium types, it indicates that this type uses the smallest memory. For example
Struct example_struct
{
Char;
Int B;
Long C _ attribute _ (packed ));
};
The purpose of the compiler for struct idioms and variables is to faster access the memory occupied by struct or members and variables, for example, for a 32-bit integer variable, if it is stored in 4 bytes (that is, the lower two addresses are 00), the CPU can read 32 in a bus cycle. Otherwise, the CPU can be combined into a 32-bit integer only after two bus cycles.
9. built-in functions
Gnu c provides a large number of built-in functions, most of which are standard C library functions (gnu c compiler's built-in versions, such as memcpy). They share the same functions as the corresponding standard C library functions. The names of other built-in functions that do not belong to library functions usually start with _ builtin.
Do {} while (0)
In the Linux kernel, do {} while (0); is mainly used for macro definition to avoid branch, logic, and semicolon errors and ensure that macros are used without compilation errors, such
# Include <stdio. h>
# Include <malloc. h>
// # Define safe_free (p) ({free (p); P = NULL ;})
# Define safe_free (p) do {free (p); P = NULL;} while (0)
Int main ()
{
Int * P = malloc (4 );
If (null! = P)
Safe_free (P );
Else
Return-1;
Printf ("success! /N ");
Return 0;
}
Goto
It is widely used in Linux kernel squadron Goto, but generally only for error handling. The Goto for error handling is simple and efficient, you only need to ensure that the order of cancellation and resource release during error handling is the opposite to that of normal registration and release applications.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dingyuanpu/archive/2010/05/09/5572297.aspx