了解了file_operations的参数loff_t *石油换食品计划 [英] Understanding loff_t *offp for file_operations

查看:116
本文介绍了了解了file_operations的参数loff_t *石油换食品计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了简单的读取和写入字符缓冲区的设备驱动程序。然而,我的问题是关于file_operations结构两个函数读取和写入。我不忠实地明白什么参数loff_t *石油换食品计划确实是。我知道,无论是读取和写入的石油换食品计划*是文件偏移表示当前读/写文件的位置,但是我甚至不知道这意味着什么写或从设备文件读取/。操作

I'm designing a device driver that simply reads and writes to a character buffer. My question is however regarding the two functions in the file_operations structure read and write. I don't truely understand what loff_t *offp really is. I know that for both the read and write operations that *offp is the file offset meaning the current reading/writing position of the file, however I'm not even sure what it means to write or read to/from a device file.

从我收集的,这是怎么了书面方式,并从我读设备是我创造出重新presents我的设备我称之为my_char_struct其中波纹管所示的结构。

From what I gathered, and this is how I am writting and reading from my device is that I create a structure which represents my device which I call my_char_struct which is shown bellow.

struct my_char_structure{
    struct cdev my_cdev;
    struct semaphore sem;
    char *data;
    ssize_t data_size;
    unsigned int access_key;
    unsigned long size;
};

这是初始化并指出当我的司机将insmod这样一个静态结构。

This is a static structure that is initialized and pointed to when my driver is insmod as such.

static dev_t dev_num;
static struct my_char_structure Dev;

int start_mod(void){
    //Because we are dealing with a fictitious device, I want
    //the driver to create my two devices with arbitrarly 
    //assigned major numbers.
    struct my_char_structure *my_dev = &Dev;
    int err;

    alloc_chrdev_region(&dev_num, FIRST_MINOR, COUNT, DEVICE_NAME);

    sema_init(&(my_dev->sem),1);

    cdev_init(&(my_dev->my_cdev), &fops);
    my_dev->my_cdev.owner = THIS_MODULE;
    my_dev->my_cdev.ops = &fops;// fops is my file operations struct

    err = cdev_add(&my_dev->my_cdev, dev_num, COUNT);
    if(err<0)
        printk(KERN_ALERT "There was an error %d.",err);
    printk(KERN_ALERT " insmod to major number %d",MAJOR(dev_num));

    return 0;   
}

module_init(start_mod);

在我的设备是开放的,我只是做一个指针,打开文件指向宏module_init(start_mod)这样期间我已经设置了静态结构...

When my device is open, I just make a pointer for the file open to point to that static structure that I've set up during module_init(start_mod) as such ...

int dev_open(struct inode *in_node, struct file *filp){
    static struct my_char_structure *my_dev;
    my_dev = container_of(in_node->i_cdev, struct my_char_structure, my_cdev);
    printk(KERN_ALERT "The device number is %d",iminor(in_node));
    if(!my_dev)
        printk(KERN_ALERT "something didn't work. my_dev not initialized.");
    filp->private_data = my_dev;
    return 0;
}

什么我读取和写入方法做的是修改初始结构开发,我已经指出,我打开的文件。无论我从结构copy_to_user是用户认为被写入设备和任何我调用copy_from_user用户认为他们书面方式。但是,除了改变我的初始结构开发,文件位置的想法或偏移,除非它指的是一个指针buffered内存内核对于一些任意的结构或类型中没有意义。那是唯一的跨pretation我有文件偏移量......这是正确的?难道这就是这里的参数loff_t *石油换食品计划是指?

What my read and write methods do is modify that initial structure Dev, that I've pointed to with my open files. Whatever I copy_to_user from my structure is what the user considers been written to the device and whatever I copy_from_user the user thinks they're writting. But beyond changing my initial structure Dev, the idea of file position or offset doesn't make sense unless it refers to a pointer to buffered memory within the kernel for some arbitrary structure or type. Thats the only interpretation that I have for the file offset ... is this correct? Is that what the loff_t *offp here refers to?

write(struct file *filp, const char __user *buff, size_t count, loff_t *offp)
read(struct file *filp, char __user *buff, size_t count, loff_t *offp)

(给我的理解是正确的)当一些file_operation如读/写叫,我还没有确定*石油换食品计划的个人,是什么参数loff_t *石油换食品计划最初设置为?如果在最后file_operation石油换食品计划= some_arbitrary_address(因为我告诉它的话),就是那个什么石油换食品计划将被设置在这种操作又被称为什么?如果我有运行的其他file_opens操作,会发生什么,将它设置成什么最后file_operation离开它的,还是会保留的FILE_OPEN运行的时间用来和替换*石油换食品计划些什么FILE_OPEN有它在标签?一个字符设备的概念对我来说过于抽象,当它似乎是设备本身甚至不喜欢储存的文件应的信息,而是它可以节省信息的驱动程序。我希望我已经解释了我的翳影,我会澄清什么,我似乎含糊不清的。

(given my understanding is correct) When some file_operation such as read/write is called and I hadn't set *offp personally, what is loff_t *offp initially set to? If in the last file_operation offp = some_arbitrary_address(because I told it so), is that what the offp would be set to when this operation is called again? What happens if I have other file_opens operations running, will it set to what the last file_operation left it as, or will it keep a tab of which file_open operation it used and replace *offp to what the file_open had it at? The concept of a char device is too abstract for me when it seems that the device itself doesn't even store the information like a file should, but rather its the driver that saves the information. I hope I've explained my fogginess and I'll clear up anything that I seem ambiguous about.

感谢任何帮助:)

推荐答案

参数loff_t是一个长偏移量,即一个寻求统一 off_t off64_t 等,让驾驶者可以只使用参数loff_t,而不是担心。

"loff_t" is a "long offset", i.e., a seek position that unifies the crazy diversity of off_t, off64_t, and so on, so that drivers can just use loff_t and not worry about it.

指针本身,在你进入驱动程序的时候,指向用户所提供的偏移(假设它的用户code做司机接入技术上的内核可以提供自己的,但用户情况一个思考)通过 lseek的 llseek lseek64 ,等等,然后通过普通的读写操作。考虑一个普通的磁盘文件的情况:当你第一次打开文件,你(作为一个用户)得到内核提供的数据结构跟踪你文件中的当前位置,因此,如果您一些字节,接下来的从你离开的地方拿起

The pointer itself, at the time you get into the driver, points to the offset provided by the user (assuming it's user code doing the driver access—technically the kernel can provide its own, but the user case is the one to think about) via lseek or llseek or lseek64, etc., and then by ordinary read and write operations. Consider the case of a regular on-disk file: when you first open the file, you (as a user) get the kernel to provide a data structure that keeps track of your current position in the file, so that if you read or write some bytes, the next read or write picks up from where you left off.

此外,如果 DUP 文件描述符,还是由等效(例如) EXEC 运行命令序列而言,寻求位被全部继承进程共享。因此,在shell提示下,命令:

Furthermore, if you dup the file descriptor, or do the equivalent by (e.g.) fork and exec in terms of running a sequence of commands, that seek-position is shared by all the inheriting processes. Hence, at the shell prompt, the command:

(prog1; prog2; prog3) > outputfile

创建一个输出文件,然后 DUP S中描述的三个方案,从而使输出 PROG2 写道:进入该文件从 PROG1 输出,并且输出 PROG3 之后紧跟着另外两个都因为所有三个单独的进程共享相同的基础内核数据结构相同的内部参数loff_t

creates an output file, then dups the descriptor to the three programs, so that output that prog2 writes goes into the file immediately after the output from prog1, and output from prog3 follows the other two—all because all three separate processes share the same underlying kernel data structure with the same internal loff_t.

这同样适用于设备驱动程序文件。当你的读写函数调用,您会收到当前偏移为用户提供的,你可以(也应该)根据需要更新......假设有任何需要(例如,你要为用户提供一个普通文件的外观,包括寻求补偿为你阅读和写作)移动的事实。如果设备的寻道抵消一些逻辑应用程序,你可以用在这里。

The same applies to device driver files. When your read and write functions are called, you receive the "current offset" as provided by the user, and you can (and should) update it as needed ... assuming there is any need (e.g., you want to provide users with the appearance of a regular file, including the fact that seek offsets move as you read and write). If the device has some logical application of the seek offset, you can use that here.

当然,还有很多更多的设备驱动程序,这就是为什么有这个东西(剂适量)整个图书章节。 : - )

Of course, there's a lot more to device drivers, which is why there are entire book-chapters on this stuff (q.v.). :-)

这篇关于了解了file_operations的参数loff_t *石油换食品计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆