Reading and Writing

The actual work of reading from and writing to character device files is not an especially interesting task because of the links already established between the virtual file and the device driver code. Calling the read and write operations of the standard library issues the system calls discussed in Chapter 8 to ultimately invoke the relevant operations (primarily read and write) in the file_operations structure. The specific implementation of these methods varies according to device and cannot be generalized.

The above memory devices have it easy because they need not bother with interaction with concrete peripherals but simply invoke other kernel functions to do their work for them.

For example, the /dev/null device uses the read_null and write_null procedures to implement read and write operations on the bit bucket. A quick look at the kernel sources will confirm that the implementation of these functions is really very simple.

drivers/char/mem.c static ssize_t read_null(struct file *

size_t count,

return 0;

static ssize_t write_null(struct file * file, const char _user * buf, size_t count, loff_t *ppos)

return count;

Reading from the null device returns nothing, and this is easy to implement; the result returned is a data stream with a length of 0 bytes. Data written to the device are simply ignored, but a successful write operation is reported for data of any length.

More complicated character devices supply functions that read and write meaningful results. The generic mechanism, however, is unchanged.

Continue reading here: Block Device Operations

Was this article helpful?

0 0