connecting application with device driver
This is an interview question.
I had written device driver for a char device so I know that code structure looks like this
struct file_operations something {
.owner=my_device_open;
.read=my_device_read;
.close=my_device_close;
.write=my_device_write;
}
When the device driver is active then in
/dev/mydevice
开发者_Go百科
you can actually read and write into it.
But what I was not clear is how an application will read or write to this device.
I know insmod
will insert the module to kernel,and register_chrdev();
will register the driver in kernel but how will application program communicate with this driver.
Let me know what will be correct answer for it.
Well Martin Beckett summed it up. It is not really more complex, although you could say the same with slightly more detail. Here is my try at it:
The program performs an open("/dev/mydevice", flags)
syscall, then the
kernel reads /dev/mydevice
from disk. It is just an inode, with no
associated data blocks, but it holds two important pieces of
information: the major number and the minor number. From these numbers,
the kernel finds the struct file_operations
that you provided through
register_chrdev()
, and it calls it's .open
field. It returns to the
program a file descriptor that it associated with this particular
struct file_operations
. Next, when the kernel receives a syscall like
write(fd, buf, count)
, it will call the .write
field and so on.
In unix it simply opens the device node as a file and sends/receives data and commands from it.
The beauty of Unix is that from an app's point of view there is nothing special about devices - they are just files (except for ioctls to set some modes). There is work to do in the kernel to accomodate this but that's the kernel modules problem.
Or were you asking something more complex?
精彩评论