Sixth. First Linux driver: Count the number of words

Source: Internet
Author: User

First, the steps to write a Linux driver

1th Step: Build Linux driver skeleton (load and unload Linux drivers)

The skeleton part is mainly the initialization and exit function of Linux driver, the code is as follows:

#include <linux/module.h>
#include <linux/init .h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess .h>
Initializing Linux drivers
static int word_count_init (void)

{

Output log Information

PRINTK ("work_count_init_success\n");

return 0;

}

Exiting Linux Drivers
static void Word_count_exit (void)
{

Output log Information
PRINTK ("word_count_init_exit_success\n");
}

Registering initialize Linux-driven functions
Module_init (Word_count_init);

Registering a function that exits the Linux driver
Module_exit (Word_count_exit);

2nd step: Registering and unregistering device files

# Insmod Word_count.ko//install Linux Driver

# Lsmmod | grep word_count//See if Word_count is installed successfully

# Rmmod Word_count//Uninstall Linux driver

# DMESG | grep Word_count | Tail–n 2//view log information that is output by Linux drivers.

You can also execute the following command to obtain log information from the Linux driver output.

# Cat/var/log/syslog | grep Word_count | Tail-n 2

3rd Step: Specify driver-related information

The module is specified using the Module_author macro.
Module Description: Specified using the Module_description macro.
Module aliases: Specified using the Module_alias macro.
Open Source protocol: Use the Module_license macro to specify.

4th step: Specify the callback function

Regardless of how complex or "cool" a Linux driver is, it makes sense to allow user-space applications to interact with kernel-space drivers. The most common way to interact is to read and write device files. Through File_operations.read and file_operations. The write member variable can specify the callback function pointer to be called by the read-write device file, respectively.

5th step: Writing Business logic

6th step: Writing the Makefile file

7th step: Compiling Linux drivers

8th step: Install and uninstall Linux drivers

Before loading the driver module with the Modprobe command, you need to use the DEPMOD command to detect the dependencies of the Linux driver module.

Once the dependency has been detected using the-depmod command, you can call the Ufodprobe command to load the Linux driver.

# modprobe Word_count

If the Linux driver is compiled into the kernel, the driver will automatically load as soon as Linux uses the kernel. If the Linux
The driver is separate from the module and requires the Linux driver module to be loaded using the Insmod or Modprobe command, using the Rmmod
command to uninstall the Linux driver module.
The first 5 steps in the above 8 steps are about how to write the Linux driver J Word, and the next 3 steps can make the Linux driver work properly.

The use of JNI under Linux

Learn Android which involves the use of JNI, for this cross-language call really did not see, Java is also recently learned, let alone the understanding of JNI,

The use of JNI is also very important and critical for Android. So in the end Java is how to call C + +, through the network to summarize the summary of learning, oneself also summarize this learning process.

What is JNI

JNI is a shorthand for Java native interface that can be translated into Java native interfaces. Java can invoke libraries of C + + through JNI, which is undoubtedly a boon for Java programs with high performance requirements. JNI is the interface that Java interacts with C + +.

The use of JNI also has a price. It is known that Java programs are run on top of the JVM and can be platform agnostic. However, if the Java program calls the native code through JNI (such as C + +, etc.), the Java program loses platform independence. At a minimum, you need to recompile the native code section. Therefore, the application of JNI needs a good balance, not the last resort, please do not choose JNI, you can choose alternatives, such as TCP/IP for interprocess communication and so on. This is why the bottom of Google's Android platform is implemented with JNI, but he does not recommend that developers use JNI to develop applications on Android. Will lose Android's application platform independence.

Sixth. First Linux driver: Count the number of words

Related Article

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.