Rabiraj
Rabiraj

Reputation: 11

character device driver's offset in read operation


static ssize_t read(struct file *file, char  *buff, size_t len, loff_t *offset)
{
    printk(KERN_INFO "write\n");
    return    simple_read_from_buffer(buff, len, offset, ker_buff, SIZE);
}

static ssize_t write(struct file *file, const char  *buff, size_t len, loff_t *offset)
{
    printk(KERN_INFO "read\n");
    printk(KERN_INFO "your offset is:%lu\n",(long)offset);

    return      simple_write_to_buffer(ker_buff, SIZE, offset, buff, len);
}

when I perform echo "hi" > /dev/device_name I am getting output:

read
your offset is 0 as output.`

Why am I getting 0?

In man page its mentioned as user's offset position.

Can any one explain me what is the use of offset?

Upvotes: 0

Views: 1012

Answers (1)

The "offset position" is the position in the file where the user-space code is reading or writing. When you do echo "hi" > /dev/device_name, you are writing to the beginning of the file, which is position 0. You could test writing to a different position from the command line by using, for example, echo "hi" | dd of=/dev/device_name seek=2000

Note: you need to update this yourself. If the user-space code writes 4000 bytes, you should increment *offset by 4000. Otherwise, if they write another 4000 bytes after that, *offset will still be 0 and they'll overwrite the first 4000 bytes instead of the next 4000.

Upvotes: 1

Related Questions