zoukankan      html  css  js  c++  java
  • 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 == '')
    		return 0;
    	//  第1个字符是空格,从0開始计数
    	if (is_spacewhite(*buf) == TRUE)
    		n--;
    
    	//  扫描字符串中的每个字符
    	for (; (c = *(buf + i)) != ''; 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] = '';
    	word_count = get_word_count(mem);
    	printk("write:word count:%d
    ", (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
    ");
    
    
    	return ret;
    }
    
    // 卸载Linux驱动
    static void __exit word_count_exit(void)
    {
    	//  删除设备文件  
    	misc_deregister(&misc);
    
    	//  输出日志信息
    	printk("word_count_init_exit_success
    ");
    } 
    
    //  注冊初始化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 
    ");
    		return 0;
    	}
    	if (argc > 1)
    	{
    
    		write(testdev, argv[1], strlen(argv[1]));
    		printf("string:%s
    ", 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
    ", buf[0], buf[1], buf[2], buf[3]);
    	printf("word count:%d
    ", n);
    	close(testdev);
    	return 0;
    }
    


  • 相关阅读:
    永续债的会计处理
    python基于粒子群优化的投资组合优化研究
    R语言使用Metropolis- Hasting抽样算法进行逻辑回归
    R语言使用K-Means聚类可视化WiFi访问
    R语言实现拟合神经网络预测和结果可视化
    Python Monte Carlo K-Means聚类实战研究
    R语言: GARCH模型股票交易量的研究道琼斯股票市场指数
    R语言stan泊松回归Poisson regression
    R语言旅行推销员问题TSP
    在R语言和Stan中估计截断泊松分布
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6747488.html
Copyright © 2011-2022 走看看