The sixth one Linux Program: Count the number of words
From this chapter to start the actual combat, the example is to count a piece of text or a paragraph of the number of words. It also describes different ways to test Linux drivers, which are used primarily to differentiate between the platforms.
A, linux : linux with normal linux API Span style= "font-family: the song Body;" > > There's no difference, it's just a different way. linux Every drive in the system is mapped to a file, These mapped files are referred to as device files or mapping files and are saved in ./dev directory.
linux driver working mode: linux unix linux The principle and idea of writing a driver under it is quite similar to the other unix system. In linux Design the driver in the environment, simple thinking, easy to operate, The function is also very powerful, but the support function is few, can only rely on the kernel
There are three main types of device files under the Linux operating system: Character devices, block devices, and network interfaces. The main difference between a character device and a block device is that when a read / Write Request is made to a character device, the actual hardware I/O is usually followed, and the block device is not, it uses a piece of system memory as a buffer, When the user process requests the device to meet the requirements of the user, the requested data is returned, and if not, the request function is called to perform the actual I/O operation. Block devices are designed primarily for slow devices, such as disks, to avoid consuming too much CPU time to wait.
WriteLinuxDriver steps:LinuxThe program has his own rules as well as other programs:1.EstablishLinuxdrive skeleton, i.e. load and unloadLinuxDriver ProgramModule_initprocessing driver initialization,Module_exitProcessing driver Exit;2.Registration and logoffLlinuxDevice Files,Misc_registerfunction creation andMisc_deregisterRemoving device files;3.Develop driver-related information:Modinfo;4.make a callback function;5.write business logic;6.WriteMakefiledocuments;7.compilingLinuxdriver;8.Install and UninstallLinuxDrive.
First Linux driver, count the number of words: This driver file does not have access to the hardware, but instead uses the device files to interact with the application as media.
(1) Create a directory to store Linux drivers
Create a drive source code file
Writing Makefile
When Linux drivers rely on other programs, you need to write makefile files like this
2. writing The skeleton of the Linux driver (Initialize and exit drivers)
The PRINTK function , which is used to output log information
Test compiling Linux Driver Source code (not necessarily on Android device)
Installing Linux Drivers
To see if the word_count installation was successful
Uninstalling Linux Drivers
View log information for Linux driver output
3. Open Source Agreement,GPL Agreement,LGPL Agreement,BSD Agreement , Apachelicence2.0 Protocol,MIT Protocol
4. Registration and cancellation of equipment
Extern int Misc_register (struct Miscdevice * misc)
Extern int Misc_deregister (struct Miscdevice * misc)
You also need to modify the word_count_init and word_count_exit functions in the word_count.c file
The device file is described by the main device number and the secondary device number. The main device number is set to ten, which is a simple character device with common characteristics in Linux system, called misc device. When the device file is successfully registered,the Misc_register function returns an integer other than 0 , and the failure returns 0.
Insmod Word_count.ko
Rmmod Word_count
Ls –A/dev
Ls –L/dev
Cat/proc/devices ( get master device and its main device number )
5. Specify callback functions word_count_read and word_count_write
The most common way to interact is to read and write device files.
6. Implement
The number of words is stored using the int type variable. the Mem Array has a length of 10000, and the last character is "%", so the string with the statistic has a maximum length of 999
7. compiling, installing, and uninstalling Linux Drivers
Testing with multiple methodsLinuxDrive:1.will beLinuxsource code compiled intoLinuxafter the drive module; 2.TestLinuxDrive module. 3.adapting on the Development BoardAndroid NDKTestLinuxtest Drive;4.UseJavathe code is tested directly on the Development BoardLinuxDrive; 5compile the driver into theLinuxKernel for testing:1.will beWord--count.cfile intoLinuxkernel source code;2.Kconfigdocuments;3.ModifyMakefiledocuments;4.Configuration. configdocuments;5.compilingLinuxkernel.
developing and testing drivers using Eclipse: developing and testing Linux drivers with Eclipse
1. eclipse linux driver , build c Project , set up c include path , Compile linux driver
2. Test Linux drivers in Eclipse , import test-word_count.c settings Include path , set Target, build project , run test program.
The sixth one Linux program: Count the number of words