gpt4 book ai didi

c - Linux内核读取函数

转载 作者:太空宇宙 更新时间:2023-11-04 11:11:38 24 4
gpt4 key购买 nike

我正在尝试为我的内核程序创建一个读取函数。首先用户必须输入一些数字(输入数字 0 停止输入)。数字输入已经为我完成,我的工作是制作读取功能。读取功能必须跟踪输入数字的数量,并且它有一个缓冲区,其中包含自模块处于事件状态以来输入的数字。我坚持从 summer.c 中获取缓冲区和读取的字符数到 test-summer.c 并打印它们。

Summer.c代码:

   /* example of a character device using ioctl 
*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include "summer.h"

#define BUF_LEN 80 /* Max length of the message from the device */

/* 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];

/* called after the device is opened*/
int device_open(struct inode *inode, struct file *file)
{
printk("\nsummer device is open\n");
total = 0;

// sprintf(msg);
printk(msg);
msg_Ptr = msg;

return 0;
}

/* called after the device is closed
*/
int device_close(struct inode *inode, struct file *file)
{
printk("\nsummer device is closed\n");
return 0;
}

/* 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;
}

/* 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--;
bytes_read++;
}
/* Most read functions return the number of bytes put into the buffer */
return bytes_read;
}


/* 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,
compat_ioctl: device_ioctl
};

/* called after the kernelmodule is opened
*/
int init_module(void)
{
/* register the device
** after registration the device is known to linux by its major number
** example: mknod /dev/summer0 c 240 0
** this creates the device /dev/summer0
** which is a character device (c) with major number 240 and minor number 0
*/
int retval = register_chrdev(MAJOR_NUM, DEVICE_NAME, &fops);
if(retval < 0)
{
printk("character device not registered\n");
return retval;
}
printk("summer kernel module loaded\n");
return 0;
}

/* called after the module is closed
*/
void cleanup_module(void)
{
/* unregister the device
*/
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
printk("summer kernel module unloaded\n");
}

测试-summer.c代码

/* example of use of a character device through ioctl
*/

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "../summer.h"

int main(void)
{
int sum;
char* buffer;
/* open character device */
int fd = open("/dev/summer0", O_RDWR); // open device
if(fd < 0)
{
printf("no device found\n");
return;
}
/* read from device */
int nc = read(fd,buffer,4,0);

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);
}

/* use ioctl to get a value from the character device */
ioctl(fd, SUMMER_GET, &sum);
printf("Result = %d\n", sum);

/* print num of chars + the buffer as a string with the amount of numbers read since the kernel is active. */
printf("#char = %d\n", nc);
printf("Buffer: %s\n" , buffer);

close(fd); // close device
return 0;
}

最佳答案

这是一个老问题,但不想让它无人问津。

在 test-summers.c 文件中,您没有为缓冲区分配内存并将其传递给读取函数。这可能是您没有获得正确的返回值以及缓冲区没有填充您想要的数据的原因。

关于c - Linux内核读取函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22840279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com