Linux module programming experience

Source: Internet
Author: User
Linux module programming experience-general Linux technology-Linux programming and kernel information. See the following for details. 1. Makefile format
In the 2.4 kernel, you can directly set the kernel source file path (gcc-I option setting) to compile the module code. However, in the 2.6 kernel, make linux + make modules + make modules_install first, then there will be a kernel code tree under/lib/modules/kernel release number/, and then the following Makefile can be compiled (the file name must be Makefile rather than makefile)

Obj-m: = driver. o # Name of the driver to be output
KDIR =/lib/modules/$ (shell uname-r)/build # build is actually a symbolic connection pointing to the location of the kernel code tree
PWD = $ (shell pwd) # current path
Modules:
$ (MAKE)-C $ (KDIR) M = $ (PWD) modules # I don't understand what this line means. I will take a look at it later.
Clean:
$ (RM) *. o *. ko *. symvers *. mod. c # delete all temporary files and output files

2 MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT

In the 2.4 kernel, module. h already has these two macros, and 2.6 is no longer available (FC3, but other cores are not visible). You need to define them as follows:

# Dfine MOD_INC_USE_COUNT try_module_get (THIS_MODULE)
# Define MOD_DEC_USE_COUNT module_put (THIS_MODULE)

3. Replace printk () in the kernel code ()
In some kernels, printk () cannot output information on the console (klogd, syslogd, or printk () is running with a low priority). You can use the following method to directly output the information to the console:

Current represents a pointer of the current process (struct task_struct *). It contains the struct tty_struct * tty pointer under 2.4 and calls tty-> write () to output information to the console, however, in the 2.6 kernel, current does not contain the tty pointer, but contains a struct signal * signal pointer, which contains the tty pointer. The Code is as follows:

Void print_string_2_tty (char * str)
{
# If LINUX_IS_V2.4
Struct tty_struct * ts = current-> tty;
# Else
Struct tty_struct * ts = current-> signal-> tty;
# Endif
If (tty & tty-> driver)
{
Tty-> driver-> write (ts, 0, str, strlen (str ));
}
}

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.