本例为Android升读探索(卷1):HAL与驱动开发 一书中附带的演示样例程序。现粘贴出来,以便查阅。

终端操作,可能用到的命令:

insmond word_count.ko
lsmod | grep word_count 查看驱动是否成功安装
rmmod word_count
dmesg | grep word_cout | tail -n 2 查看有linux驱动输出的日志信息
cat /var/log/syslong | grep word_count | tail -n 2
modinfo word_count.ko 查看驱动的信息

驱动源码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h> // 定义设备文件名称
#define DEVICE_NAME "wordcount"
static unsigned char mem[10000]; // 保存向设备文件写入的数据
static int word_count = 0;
#define TRUE 255
#define FALSE 0 // 推断指定字符是否为空格(包含空格符、制表符、回车符和换行符)
static unsigned char is_spacewhite(char c)
{
if (c == 32 || c == 9 || c == 13 || c == 10)
return TRUE;
else
return FALSE;
} static int get_word_count(const char *buf)
{
int n = 1;
int i = 0;
char c = ' '; char flag = 0; // 处理多个空格分隔的情况,0:正常情况,1:已遇到一个空格
if (*buf == '\0')
return 0;
// 第1个字符是空格,从0開始计数
if (is_spacewhite(*buf) == TRUE)
n--; // 扫描字符串中的每个字符
for (; (c = *(buf + i)) != '\0'; i++)
{
// 仅仅由一个空格分隔单词的情况
if (flag == 1 && is_spacewhite(c) == FALSE)
{ flag = 0;
}
// 由多个空格分隔单词的情况,忽略多余的空格
else if (flag == 1 && is_spacewhite(c) == TRUE)
{ continue;
}
// 当前字符为空格是单词数加1
if (is_spacewhite(c) == TRUE)
{
n++;
flag = 1;
}
}
// 假设字符串以一个或多个空格结尾,不计数(单词数减1)
if (is_spacewhite(*(buf + i - 1)) == TRUE)
n--;
return n;
} static ssize_t word_count_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
unsigned char temp[4]; temp[0] = word_count >> 24;
temp[1] = word_count >> 16;
temp[2] = word_count >> 8;
temp[3] = word_count;
if (copy_to_user(buf, (void*) temp, 4))
{
return -EINVAL;
}
printk("read:word count:%d", (int) count); return count;
} static ssize_t word_count_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
ssize_t written = count; if (copy_from_user(mem, buf, count))
{
return -EINVAL;
}
mem[count] = '\0';
word_count = get_word_count(mem);
printk("write:word count:%d\n", (int) word_count); return written;
} // 描写叙述与设备文件触发的事件相应的回调函数指针
static struct file_operations dev_fops =
{ .owner = THIS_MODULE, .read = word_count_read, .write = word_count_write }; // 描写叙述设备文件的信息
static struct miscdevice misc =
{ .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops }; // 初始化Linux驱动
static int __init word_count_init(void)
{
int ret; // 建立设备文件
ret = misc_register(&misc); // 输出日志信息
printk("word_count_init_success\n"); return ret;
} // 卸载Linux驱动
static void __exit word_count_exit(void)
{
// 删除设备文件
misc_deregister(&misc); // 输出日志信息
printk("word_count_init_exit_success\n");
} // 注冊初始化Linux驱动的函数
module_init( word_count_init);
// 注冊卸载Linux驱动的函数
module_exit( word_count_exit); MODULE_AUTHOR("lining");
MODULE_DESCRIPTION("statistics of word count.");
MODULE_ALIAS("word count module.");
MODULE_LICENSE("GPL");

測试代码

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int testdev;
unsigned char buf[4]; testdev = open("/dev/wordcount", O_RDWR);
if (testdev == -1)
{
printf("Cann't open file \n");
return 0;
}
if (argc > 1)
{ write(testdev, argv[1], strlen(argv[1]));
printf("string:%s\n", argv[1]);
} read(testdev, buf, 4); int n = 0;
// 将4个字节还原成int类型的值
n = ((int) buf[0]) << 24 | ((int) buf[1]) << 16 | ((int) buf[2]) << 8
| ((int) buf[3]);
printf("word byte display:%d,%d,%d,%d\n", buf[0], buf[1], buf[2], buf[3]);
printf("word count:%d\n", n);
close(testdev);
return 0;
}

Linux驱动程序:统计单词个数的更多相关文章

  1. 第六章 第一个Linux驱动程序:统计单词个数

    现在进入了实战阶段,使用统计单词个数的实例让我们了解开发和测试Linux驱动程序的完整过程.第一个Linux驱动程序是统计单词个数. 这个Linux驱动程序没有访问硬件,而是利用设备文件作为介质与应用 ...

  2. 第六章第一个linux个程序:统计单词个数

    第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数.  第 1 步:建立 Linu x 驱 ...

  3. NOIP200107统计单词个数

    NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...

  4. NOIP2001 统计单词个数

    题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...

  5. Codevs_1040_[NOIP2001]_统计单词个数_(划分型动态规划)

    描述 http://codevs.cn/problem/1040/ 与Codevs_1017_乘积最大很像,都是划分型dp. 给出一个字符串和几个单词,要求将字符串划分成k段,在每一段中求共有多少单词 ...

  6. luogu P1026 统计单词个数

    题目链接 luogu P1026 统计单词个数 题解 贪心的预处理母本串从i到j的最大单词数 然后dp[i][j] 表示从前i个切了k次最优解 转移显然 代码 #include<cstdio&g ...

  7. Codevs 1040 统计单词个数

    1040 统计单词个数 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个长度不超过200的 ...

  8. codevs1040统计单词个数(区间+划分型dp)

    1040 统计单词个数 2001年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 给出一个长度不超 ...

  9. P1026 统计单词个数——substr

    P1026 统计单词个数 string 基本操作: substr(x,y) x是起始位置,y是长度: 返回的是这一段字符串: 先预处理sum[i][j],表示以i开头,最多的单词数: 从后往前寻找,保 ...

  10. [luogu]P1026 统计单词个数[DP][字符串]

    [luogu]P1026 统计单词个数 题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1&l ...

随机推荐

  1. 不规则形状的Mask动画

    不规则形状的Mask动画 效果 源码 https://github.com/YouXianMing/Animations // // MaskShapeViewController.m // Anim ...

  2. JQuery攻略(五)表单验证

    表单验证,字段空白,输入合法,数据合法....... 此章节有 1.1字段验证 1.2正则表达式验证 1.3复选框的勾选 1.1字段验证 1.字段非空 $(document).ready(functi ...

  3. 通过Spannable对象设置textview的样式

    通过Spannable对象我们可以设置textview的各种样式,其功能十分强大.通过SpannableString和它的setSpan(Object what, int start, int end ...

  4. Error: Program type already present: android.arch.lifecycle.LifecycleRegistry$1

    com.firebaseui:firebase-ui-firestore:3.1.0 depends on android.arch.lifecycle:extensions:1.0.0-beta1. ...

  5. git clone报错:“server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none”

    I can push by clone project using ssh, but it doesn't work when I clone project with https. it shows ...

  6. org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.xugao.bean.MemberLevel.memberpointrate

    由于数据不合法的原因,好几次遇到: org.hibernate.PropertyAccessException: Null value was assigned to a property of pr ...

  7. pchar,pwidechar,pansichar作为返回参数时内存访问错误

    function Test:pachr: var   str: string; begin   str := 'Test Char';   result:=pchar(str); end; 上面的Te ...

  8. javascript中的分支判断与循环

    分支判断与循环 分支结构 单一选择结构(if) 二路选择结构(if/else) 内联三元运算符 ?: 多路选择结构(switch) var condition = true; if (conditio ...

  9. 使用C#反射机制访问类的私有成员【转】

    首先我必须承认访问一个类的私有成员不是什么好做法.大家也都知道私有成员在外部是不能被访问的.而一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员访问,可以套用下面这种非常好的方式 ...

  10. tensorflow_python中文手册

    https://www.tensorflow.org/api_docs/python/tf/nn/static_bidirectional_rnn https://www.w3cschool.cn/t ...