根据我的accademic项目,我目前的任务是使用内核模块生成10个随机数,我的用户空间程序(c程序)应该能够显示这些数字。我一直在学习内核空间和用户空间程序。我偶然发现了角色装置的创造。我使用此命令创建了一个设备。
mknod /dev/my_device c 222 0
据我所知,这个设备是用户空间和内核空间程序之间的媒介。所以我创建了一个内核模块,用于注册和注销我的角色设备。保存为my_dev.c
#include<linux/module.h>
#include<linux/init.h>
#include"my_dev.h"
MODULE_AUTHOR("Krishna");
MODULE_DESCRIPTION("A simple char device");
static int r_init(void);
static void r_cleanup(void);
module_init(r_init);
module_exit(r_cleanup);
static int r_init(void)
{
printk("<1>hi\n");
if(register_chrdev(222,"my_device",&my_fops)){
printk("<1>failed to register");
}
return 0;
}
static void r_cleanup(void)
{
printk("<1>bye\n");
unregister_chrdev(222,"my_device");
return ;
}
我的编译此模块的Make文件是
obj-m += my_dev.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
这个内核模块是使用insmod命令编译并加载到内存中的。
这里有一个程序,它可以向保存为my_dev.h的用户bufer写入和读取一些文本。
/*
* my device header file
*/
#ifndef _MY_DEVICE_H
#define _MY_DEVICE_H
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <asm/current.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
char my_data[80]="heloooo"; /* our device */
int my_open(struct inode *inode,struct file *filep);
int my_release(struct inode *inode,struct file *filep);
ssize_t my_read(struct file *filep,char *buff,size_t count,loff_t *offp );
ssize_t my_write(struct file *filep,const char *buff,size_t count,loff_t *offp );
struct file_operations my_fops={
open: my_open,
read: my_read,
write: my_write,
release:my_release,
};
int my_open(struct inode *inode,struct file *filep)
{
/*MOD_INC_USE_COUNT;*/ /* increments usage count of module */
return 0;
}
int my_release(struct inode *inode,struct file *filep)
{
/*MOD_DEC_USE_COUNT;*/ /* decrements usage count of module */
return 0;
}
ssize_t my_read(struct file *filep,char *buff,size_t count,loff_t *offp )
{
/* function to copy kernel space buffer to user space*/
if ( copy_to_user(buff,my_data,strlen(my_data)) != 0 )
printk( "Kernel -> userspace copy failed!\n" );
return strlen(my_data);
}
ssize_t my_write(struct file *filep,const char *buff,size_t count,loff_t *offp )
{
/* function to copy user space buffer to kernel space*/
if ( copy_from_user(my_data,buff,count) != 0 )
printk( "Userspace -> kernel copy failed!\n" );
return 0;
}
#endif
这是我的用户空间程序
acs.c
它在运行时通过从上述程序的内核缓冲区读取文本来打印“heloooo”。
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int fd=0,ret=0;
char buff[80]="";
fd=open("/dev/my_device",O_RDONLY);
printf("fd :%d\n",fd);
ret=read(fd,buff,10);
buff[ret]='\0';
printf("buff: %s ;length: %d bytes\n",buff,ret);
close(fd);
}
现在我的问题是,我需要编写一个用户空间程序,该程序在运行时打印10个随机数。但这些数字应该使用内核模块生成。所以基本上以上三个代码都能正常工作并打印出“helooo”。我需要做的是获得随机数作为输出,而不是“helooo”。
这里有一个存储模块,它使用线性同余生成器算法生成一些随机数。
LCG.c
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
int M = 8; //Modulus, M>0
int a = 9; //Multiplier, 0 <= a < M.
int c = 3; //Increment, 0 <= c < M.
int X = 1; //seed value, 0 <= X(0) < M
int i; //iterator, i < M
for(i=0; i<8; i++)
{
X = (a * X + c) % M;
printk(KERN_INFO "%d\n",X);
}
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Task Done ! :D.\n");
}
我有所有的代码。但我不知道如何将这个随机数生成器代码放入我的charecter设备调用代码中。当我运行acs.c程序时,我需要获得内存模块的输出
LCG.c公司
通过使用字符设备。请帮我找到解决方案。