First Linux driver: Count the numbers of words

Source: Internet
Author: User
Tags readable

To write the Linux driver steps:

1. Build Linux drive skeleton (load and unload Linux drivers)

#include <linux/module.h>

#include <linux/init.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)

{

PRINTK ("word_count_init_success\n");

return 0;

}

Exiting Linux Drivers

static void Word_count_exit (void)

{

PRINTK ("word_count_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);

2, specify the information related to the drive

Module_author ("lining"); Module author

Module_description ("Statistics of Word count."); Module description

Module_alias ("Word Count module."); Module aliases

Module_license ("GPL"); Open Source Agreement

3. Registration and cancellation of equipment files

Modify the Word_count.c file

#define DEVICE_NAME "WordCount"//define Device file name

static struct File_operations Dev_fops = {. Owner = this_module}; Describes the callback function pointer corresponding to the event triggered by the device file

static struct Miscdevice Misc =//information describing the device file

{

. minor = Misc_dynamic_minor,//secondary device number, dynamically generated secondary device number (Miscellaneous device master number is 10)

. Name = Device_name,//device file name

. fops = &dev_fops//file_operations struct variable pointer

};

static int word_count_init (void)

{

int ret;

ret = Misc_register (&MISC); Create a device file

return ret;

}

STAITC void Word_exit (void)

{

Misc_deregister (&MISC); Log off (remove) device files

}

4. Specifying a callback function

static unsigned char mem[1000]; Saving data written to a device file

static int word_count = 0; Number of words

#define TRUE-1

#define FALSE 0

Determines whether the specified character is a space (including whitespace, tab, carriage return, line feed)

Static Char Is_spacewhite (char c)

{

if (c = = "| | c = = 9 | | c = = | | c = = 10)

return TRUE;

Else

return FALSE;

}

Count the number of words

static int Get_word_count (const char *BUF)

{

int n = 1;

int i = 0;

char c = ';

Char flag = 0; Handle multiple space-delimited cases, 0: normal, 1: a space has been encountered

if (*buf = = ' + ')

return 0;

The first character is a space, counting from zero

if (Is_spacewhite (*buf) = = TRUE)

N--;

Scan every character in a string

for (; (c = * (buf + i))! = ' + '; i++)

{

Case with only one space separating words

if (flag = = = 1 && is_spacewhite (c) = = FALSE)

Frag = 0;

Cases where words are separated by multiple spaces, ignoring extra spaces

else if (flag = = = 1 && is_spacewhite (c) = = TRUE)

Continue

The number of words plus 1 when the current character is a space

if (Is_spacewhite (c) = = TRUE)

{

n++;

flag = 1;

}

}

If the string ends with one or more spaces, no count (the number of words minus 1)

if (Is_spacewhite (* (buf + i-1) = = TRUE)

n--;

return n;

}

Call this function when reading data from a device file, file: pointing to device files BUF: Saving readable data count: Number of bytes readable PPOs: offset of Read data

Static ssize_t word_count_read (struct file *file, char _user *buf, size_t count, loff_t *ppos)

{

unsigned char temp[4];

Breaks down the number of words (int type) into 4 bytes stored in a buf

Temp[0] = Word_count >> 24;

TEMP[1] = Word_count >> 16;

TEMP[2] = Word_count >> 8;

TEMP[3] = Word_count;

Copy_to_user (buf, (void*) temp, 4);

PRINTK ("Read:word count:%d", (int) count);

return count;

}

Call this function when writing data to a device file

Static ssize_t word_count_write (struct file *file, const char _user *buf, size_t count, loff_t *ppos)

{

ssize_t writen = count;

Copy_from_user (Mem, buf, Count);

Mem[count] = ' + ';

Word_count = Get_word_count (MEM);

PRINTK ("Write:word count:%d", (int) word_count);

return writen;

}

static struct File_operations Dev_fops =

{

. Owner = This_module,

. Read = Word_count_read,

. write = Word_count_write

};

5. Compiler driver

6. Install and uninstall Linux drivers

Install Linux driver: # insmod Word_count.ko

Uninstall Linux driver: # rmmod Word_count

First Linux driver: Count the numbers of words

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.