C Linux内核读取自定义函数
尝试创建一个read函数,它不仅可以将数字相加。但也会计算出添加了多少个数字 需要在此类脚本中使用:C Linux内核读取自定义函数,c,linux,kernel,kernel-module,C,Linux,Kernel,Kernel Module,尝试创建一个read函数,它不仅可以将数字相加。但也会计算出添加了多少个数字 需要在此类脚本中使用: for i in 1 2 4 8 16 32 64; do echo $i > /dev/summer0 sleep 60 done 但也只是使用一个测试用例 以下是内核模块代码: /* the memory of the device */ int total; /* Global variables declared as staic, so are global wi
for i in 1 2 4 8 16 32 64; do
echo $i > /dev/summer0
sleep 60
done
但也只是使用一个测试用例
以下是内核模块代码:
/* the memory of the device
*/
int total;
/* Global variables declared as staic, so are global within the file.*/
static char *msg_Ptr;
static char msg[BUF_LEN];
static int n;
/* called after the device is opened
*/
int device_open(struct inode *inode, struct file *file)
{
printk("\nsummer device is open\n");
total = 0;
printk(msg);
msg_Ptr = msg;
return 0;
}
/* Read function */
static ssize_t device_read(struct file *filep, char *buffer, size_t length, loff_t *offset)
{
/* Number of bytes actually written to the buffer */
int bytes_read = 0;
/* If we're at the end of the message, return 0 signifying end of file */
if (*msg_Ptr == 0) return 0;
/* Actually put the data into the buffer */
while (length && *msg_Ptr)
{
/* The buffer is in the user data segment, not the kernel segment;
* assignment won't work. We have to use put_use which copies data from
* the kernel data segment to the user data segment. */
__put_user(*(msg_Ptr++), buffer++);
length++;
n++;
}
/* Most read functions return the number of bytes put into the buffer */
return n;
}
/* handling of ioctl events
*/
long device_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
{
int number;
switch(ioctl_num)
{
case SUMMER_SET:
__get_user(number, (int*)ioctl_param);
total += number;
break;
case SUMMER_GET:
__put_user(total, (int*)ioctl_param);
break;
}
return 0;
}
/* table of event handlers of the device
*/
struct file_operations fops =
{
read : device_read,
//write : device_write,
open : device_open,
release: device_close,
unlocked_ioctl : device_ioctl
};
至于测试用例
int n;
int main(void)
{
int sum;
char buffer[BUF_LEN];
/* open character device */
int fd = open("/dev/summer0", O_RDWR); // open device
if(fd < 0)
{
printf("no device found\n");
return 0;
}
int nc = read(fd,buffer,4,n);
for(;;)
{
int num;
printf("Number (exit with 0) = ");
while(scanf("%d", &num) != 1)
;
printf("-- %d --\n", num);
if(num <= 0) break;
/* use ioctl to pass a value to the character device */
ioctl(fd, SUMMER_SET, &num);
n++;
}
/* use ioctl to get a value from the character device */
ioctl(fd, SUMMER_GET, &sum);
printf("#char = %d\n", nc);
printf("Result = %d\n", sum);
printf("Buffer: %s\n" , buffer);
close(fd); // close device
return 0;
}
intn;
内部主(空)
{
整数和;
字符缓冲区[BUF_LEN];
/*开放字符设备*/
int fd=open(“/dev/summer0”,O_RDWR);//打开设备
如果(fd<0)
{
printf(“未找到设备”);
返回0;
}
int nc=读取(fd,缓冲器,4,n);
对于(;;)
{
int-num;
printf(“数字(以0退出)=”);
while(scanf(“%d”,&num)!=1)
;
printf(“--%d--\n”,num);
如果(num)你尝试做什么不清楚…你能描述一下你做了什么(例如:1-modprobe,2-shell代码,3-c程序…)结果是什么?你想做什么?简言之,你定义了msg
和msg\u Ptr
,但你从未将任何内容写入相关的内存你在device\u open
中拥有的total=0
,因此每次打开设备时你都会重置该值。该程序的全局思想是,它不仅对输入的数字,并给我一个总数的计数。您需要澄清——可能是对您自己,当然是这样——您希望它如何运行。每次打开设备时,C代码重置total
,并通过ioctl
代码进行通信。脚本示例通过编写进行通信到设备(似乎没有处理程序),并且似乎假设总/计数是在几次这样的写入过程中累积的。您尝试做什么并不清楚…您能描述一下您所做的(即:1-modprobe,2-shell代码,3-c程序…)结果是什么?你想做什么?简言之,你定义了msg
和msg\u Ptr
,但你从未将任何内容写入相关的内存你在device\u open
中拥有的total=0
,因此每次打开设备时你都会重置该值。该程序的全局思想是,它不仅对输入的数字,并给我一个总数的计数。您需要澄清——可能是对您自己,当然是这样——您希望它如何运行。每次打开设备时,C代码重置total
,并通过ioctl
代码进行通信。脚本示例通过编写进行通信向设备(似乎没有处理程序)发送数据,并且似乎假定总/计数是在多次这样的写入过程中累积的。