static ssize_t my_read(struct file *f, char __user *buf, size_t
len, loff_t *off)
{
static int first=1;
printk(KERN_INFO "Driver: read()\n");
printk(KERN_INFO "Value of loff_t %ld",*off);
printk(KERN_INFO "Inside value of stringlength is %d",strlen(buffer));
if (copy_to_user(buf,buffer, 1) != 0)
return -EFAULT;
else
{
if(first<=strlen(buffer))
{
first=first+1;
printk(KERN_INFO "INside value of first is %d",first);
printk(KERN_INFO "Inside value of stringlength is %d",strlen(buffer));
return 1;
}
else
return 0;
}
}
static ssize_t my_write(struct file *f, const char __user *buf,
size_t len, loff_t *off)
{
printk(KERN_INFO "Driver: write()\n");
if (copy_from_user(buffer, buf+len-1, 1) != 0)
return -EFAULT;
else
return len;
}
但是,我第一次执行 echo -n "HEllo" andcat /dev/mynull
时,它只打印o
并出于某种原因释放设备。后续不cat /dev/mynull
返回任何内容。的值为strlen(buffer)
1。
为什么会这样?预期的结果是 Hello 应该被打印并且strlen()
应该返回5
。