Why use the same interface to talk to a printer as to talk to a disk drive? The short answer is that while the devices are very much different, they can be thought of as having most of the same characteristics as files. The entire system is then kept smaller and simpler by only using one interface with a few extensions. This is fine, except that it hides important differences between devices. For example, it is possible to read any byte on a disk at any time, but it is only possible to read the next byte from a terminal.
There are other differences, but this is the most fundamental one: Some devices like disks are random-access , and others like terminals are sequential-access. Of course, it is possible to pretend that a random-access device is a sequential-access device, but it doesn't work the other way around.
A practical effect of the difference is that filesystems can only be mounted on block devices, not on character ones. For example, most tapes are character devices. It is possible to copy the contents of a raw, quiescent unmounted and not being modified filesystem to a tape, but you will not be able to mount the tape, even though it contains the same information as the disk. Most textbooks and tutorials start by explaining character devices, the sequential-access ones, because a minimal character device driver is easier to write than a minimal block device driver.
My reason for starting this column with block devices, the random-access devices, is that the KHG explains simple character devices better than it does block devices, and I think that there is a greater need for information on block devices right now.
Furthermore, real character device drivers can be quite complex, just as complex as block device drivers, and fewer people know how to write block device drivers.
I am not going to give a complete example of a device driver here. I am going to explain the important parts, and let you discover the rest by examining the Linux source code.
I've included a few hints in this article, as well. Whereas character device drivers provide procedures for directly reading and writing data from and to the device they drive, block devices do not.
Instead, they provide a single request procedure which is used for both reading and writing. The requests have already been sorted by the time the request function reads the queue. When it is called, if it is not interrupt-driven, it processes requests for blocks to be read from the device, until it has exhausted all pending requests. Normally, there will be only one request in the queue, but the request procedure should check until it is empty.
Note that other requests may be added to the queue by other processes while the current request is being processed. The first thing you notice about this function may be that it never explicitly returns.
It does not run off the end and return, and there is no return statement. It checks the request queue and, if there are no requests in the queue, it returns. This is the current request, the one at the head of the request queue that is being processed. The request structure includes all the information needed to process the request, including the device, the command read or write; we'll assume read here , which sector is being read, the number of sectors to read, a pointer to memory to store the data in, and a pointer to the next request.
There is more than that, but that's all we are concerned with. The sector variable contains the block number. The length of a sector is specified when the device is initialized more later , and the sectors are numbered consecutively, starting at 0. If the physical device is addressed by some means other than sectors, it is the responsibility of the request procedure to translate.
In some cases, a command may read or write more than one sector. If it has been satisfied, it is called with an argument of 1 and, if it has been aborted, it is called with an argument of 0. A character device driver can also be used where it is necessary to copy data directly to or from a user process. Line printers, interactive terminals, and graphics displays are examples of devices that require character device drivers.
I only posted this a half-hour ago and already you guys were all over it. Now I know. Thank you. Steven Yeah, I think hictio lives here. What's the difference between a character device and a block device? Posted: Tue Sep 10, pm. What can I say? Lucky me, right? Registered: Mar 14, Posts: Posted: Wed Sep 11, am.
0コメント