|
最近写了一个简单的字符设备驱动, 发现一个很奇怪的问题。
其中的write方法如下:
//全局数据变量
char kbuf[1024];
static int kcount;
ssize_t testdev_write(struct file* file, char* buf, size_t count, loff_t* ppos)
{
ssize_t retval = 0;
printk(KERN_NOTICE"test_write is called1, *ppos = %d, kcount = %d\n", *ppos, kcount); /*kcount输出有问题*/
if (*ppos >= 1024)
{
printk(KERN_ALERT"*ppos exceed the length of buf.\n");
retval = -EFAULT;
goto error;
}
if (*ppos + count >= 1024)
{
printk(KERN_ALERT"count changed from %d to %d.\n", count, 1024 - *ppos - 1);
count = 1024 - 1 - *ppos;
}
if (copy_from_user(kbuf + kcount, buf, count))
{
retval = -EFAULT;
goto error;
}
printk(KERN_NOTICE"string, kbuf: %s\n", kbuf);
printk(KERN_NOTICE"string,kcount = %d, kbuf+kcount: %s\n", kcount, kbuf+kcount); /*kcount输出正确*/
*ppos += count;
kcount = *ppos;
printk("kcount = %d\n", kcount);
return count;
error:
return retval;
}
用户空间的程序代码片段如下:
int fd = open("/dev/testdev/1", O_RDWR);
char* pbuf = "Hello World!\n";
char buf[20];
int num;
extern int errno;
num = write(fd, pbuf, strlen(pbuf));
printf("num = %d\n", num);
if (num != 13)
{
printf("failed to write pbuf1, errno = %d\n", errno);
return -1;
}
num = write(fd, "mike\n", 5);
if (num != 5)
{
printf("failed to write pbuf2, errno = %d\n", errno);
return -1;
}
在用户空间的程序中,连续调用了两次write,两次write都执行成功了。但驱动中的testdev_write执行有点奇怪。第一次调用testdev_write时很正常,第二次调用testdev_write时,下面这句代码输出的有问题:
printk(KERN_NOTICE"test_write is called1, *ppos = %d, kcount = %d\n", *ppos, kcount);
其中*ppos输出为13,正确,但kcount输出还是为0,就不正确了,kcount应该也是13才对。
但是随后这句代码输出又是正确的:
printk(KERN_NOTICE"string,kcount = %d, kbuf+kcount: %s\n", kcount, kbuf+kcount);
这次kcount输出的就是13了,两次输出之间,我并没有修改kcount的值,为什么第一次输出不正确,而第二次输出就正确呢?而且内存也能正确从用户空间拷贝到内核空间。 |
|