设计一个程序,在用户空间的用户应用程序中产生20个随机数,通过内核空间的设备驱动程序按五行四列的排列输出,并显示能被5整除的数。
分析:要实现这个功能需要做以下工作:
1.编写嵌入式设备驱动程序:实现按五行四列的排列输出,并显示能被5整除的数。
1设主设备号为100,即#define data_MAJOR 100
2在函数data_read()中将用户空间通过参数buf传递来的数据按5行4列的排列输出。在使用printk函数时,输出的格式用“%d”。
3在函数data_write()中找出用户空间通过参数buf传递来的能被5整除的数据输出到屏幕上。在使用printk函数时,输出的格式用“%d”。
4使用Vi编辑器编辑一个设备驱动程序data_drv.c。
/*************************************
*设备驱动程序data_drv.c
*由用户应用程序传递20个数值,在本设备驱动程序中进行排列输出,
*并打印出能被5整除的数
*************************************/
#include<linux/config.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/devfs_fs_kernel.h>
#include<linux/module.h>
#define data_MAJOR 100
ssize_t data_read(struct file * file,char *buf,size_tcount, loff_t* f_ops)
{//将用户空间通过参数buf传递来的数据按5行4列的排列输出
printk("data_read[--kernel--]\n");
for(count=0;count<20;count++){
printk("%d ",buf[count]);
if((count+1)%4==0)
printk("\n");
}
returncount;
}
ssize_t data_write(struct file * file, const char*buf, size_t count, loff_t * f_ops)
{//找出用户空间通过参数buf传递来的能被5整除的数据
printk("data_write[--kernel--]\n");
for(count=0;count<20;count++){
if(buf[count]%5==0)
printk("%d ",buf[count]);
<