Linux驱动程序:统计单词个数
本例为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驱动程序:统计单词个数的更多相关文章
- 第六章 第一个Linux驱动程序:统计单词个数
现在进入了实战阶段,使用统计单词个数的实例让我们了解开发和测试Linux驱动程序的完整过程.第一个Linux驱动程序是统计单词个数. 这个Linux驱动程序没有访问硬件,而是利用设备文件作为介质与应用 ...
- 第六章第一个linux个程序:统计单词个数
第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数. 第 1 步:建立 Linu x 驱 ...
- NOIP200107统计单词个数
NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...
- NOIP2001 统计单词个数
题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...
- Codevs_1040_[NOIP2001]_统计单词个数_(划分型动态规划)
描述 http://codevs.cn/problem/1040/ 与Codevs_1017_乘积最大很像,都是划分型dp. 给出一个字符串和几个单词,要求将字符串划分成k段,在每一段中求共有多少单词 ...
- luogu P1026 统计单词个数
题目链接 luogu P1026 统计单词个数 题解 贪心的预处理母本串从i到j的最大单词数 然后dp[i][j] 表示从前i个切了k次最优解 转移显然 代码 #include<cstdio&g ...
- Codevs 1040 统计单词个数
1040 统计单词个数 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个长度不超过200的 ...
- codevs1040统计单词个数(区间+划分型dp)
1040 统计单词个数 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个长度不超 ...
- P1026 统计单词个数——substr
P1026 统计单词个数 string 基本操作: substr(x,y) x是起始位置,y是长度: 返回的是这一段字符串: 先预处理sum[i][j],表示以i开头,最多的单词数: 从后往前寻找,保 ...
- [luogu]P1026 统计单词个数[DP][字符串]
[luogu]P1026 统计单词个数 题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1&l ...
随机推荐
- Java 与 Json的互相转换
这几天一直在做Java解析Json数据的一个项目,因为初识json,所以很多东西都是有着懵懂的认识.这里写下我解析时遇到的问题和收获. 我解析json时用到的是json-lib包.下载地址:http: ...
- MYSQL 1093 之You can't specify target table for update in FROM clause解决办法
You can't specify target table for update in FROM clause含义:不能在同一表中查询的数据作为同一表的更新数据. 出现以上错误,是因为想将表自身的字 ...
- go1.8之安装配置具体步骤
操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 安装go 这里直接安装二进制,其它方式请自行搜索. 1.下载并安装go 命令如下: ? 1 2 3 wget https://st ...
- IT狂人第一至四季/全集The IT Crowd迅雷下载
本季第一至四季 The IT Crowd (2006)看点:<IT狂人>史上最囧,最雷,最脑残,最出乎意料,最不按常理出牌的IT “精英们”登上银屏了.让超擅长收发邮件.单击和双击鼠标的I ...
- ISP图像处理&&相机系统
如何理解 ISO.快门.光圈.曝光这几个概念? 摄影基础篇——彻底弄清光圈.快门与ISO 理解这三个参数各自都是如何控制进入的光线量: 快门速度一般的表示方法是1/100s.1/30s.2s: 小的“ ...
- Add Binary Leetcode java
题目: Given two binary strings, return their sum (also a binary string). For example, a = "11&quo ...
- BUG的严重级别分类 BUG状态标准
英文参考 BUG的严重级别分类 Severity This field describes the impact of a bug. Blocker Blocks development and/or ...
- canvas移动端常用技巧图片loading
核心知识点:drawImage 作用:将图片加载在canvas html: <canvas id="myCanvas" width="200" heigh ...
- js面向对象之继承-原型继承
//animal 父类 超类 var Animal = function(name) { this.name = name; this.sayhello = function() { alert(&q ...
- 交互软件Axure—高保真原型
在上一篇文章中跟大家分享了Axure7.0 的简介.基本操作和原型图的制作,主要是应用元件库里的原件进行界面元素的搭建,直至完成原型图,在最后给大家展示了高保真原型图效果.而在本次分享中,主要带领大家 ...