【发布时间】:2012-10-02 18:45:31
【问题描述】:
这听起来像是一个奇怪的问题,但是当我去打开一个文件时:
int fd;
fd = open("/dev/somedevice", O_RDWR);
我究竟得到了什么?我可以看到手册页说:The open() function shall return a file descriptor for the named file that is the lowest file descriptor not currently open for that process
但就是这样吗?它只是一个int 还是在幕后附加了数据?我问的原因是我发现了一些代码(Linux/C),我们正在从用户空间打开文件:
//User space code:
int fdC;
if ((fdC = open(DEVICE, O_RDWR)) < 0) {
printf("Error opening device %s (%s)\n", DEVICE, strerror(errno));
goto error_exit;
}
while (!fQuit) {
if ((nRet = read(fdC, &rx_message, 1)) > 0) {
然后在内核端,此模块(提供 fd)映射的文件操作读取到 n_read() 函数:
struct file_operations can_fops = {
owner: THIS_MODULE,
lseek: NULL,
read: n_read,
然后n_read()中使用了文件描述符,但正在访问它以获取数据:
int n_read(struct file *file, char *buffer, size_t count, loff_t *loff)
{
data_t * dev;
dev = (data_t*)file->private_data;
所以...我认为这里发生的事情是:
A) 从open() 返回的文件描述符包含的数据不仅仅是描述性整数值
或者
B) 用户空间中对“读取”的调用之间的映射并不像我想象的那么简单,而且这个等式中缺少一些代码。
有什么可以帮助指导我的意见吗?
【问题讨论】:
-
fQuit在哪里声明?是的,文件描述符只是整数。有关它们的任何信息都必须通过内核文件描述符表中的系统调用来获取。 -
@user1700513 - 你可以假设
fQuite是 0。
标签: c linux linux-kernel kernel-module file-descriptor