1

自定义读写操作定义为

ssize_t (*read) (struct file *,char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *,const char __user *, size_t, loff_t *);

对设备进行读取或写入时会发生什么?

我在 LDD 书中找不到对此的简单解释。

例如,当我有一个设备并且我写了一个类似的东西时会发生什么

echo "Hello" > /dev/newdevice

我正在编写一个简单的字符设备。还

cat  /dev/newdevice

我知道这取决于我的自定义读/写,我需要的是从内存中简单读取并写入内存

4

1 回答 1

1

@ user567879,由于设备节点被视为特殊字符或块或网络文件,因此每个文件都有一个文件结构“filp”,该结构又包含指向文件操作表的指针,其中每个系统调用都映射到设备中的适当函数司机。

for ex: .open = my_open
        .write = my_write 
        .read = my_read etc.

当您发出 echo "Hello" > /dev/newdevice 时会发生什么

1) Device node i.e. "/dev/newdevice" is opened using open system call which in turn 
   calls your mapped open function i.e. "**my_open**"

2) If open is successful, write system call issued with appropriate file descriptor 
   (fd), which in turn calls "**my_write**" function present in device driver and thus 
   according to the functionality it writes/transmits user data to the actual 
   hardware. 

3) Same rule applies for "cat  /dev/newdevice" i.e. open the device node --> read 
   system call --> mapped read function in your device driver i.e. "**my_read**" --> 
   reads the data from actual hardware and sends the data read from the hardware to
   user space (application which issued read system call)

我希望我已经回答了你的问题:-)

于 2013-10-10T12:06:25.083 回答