码迷,mamicode.com
首页 > 其他好文 > 详细

System and Device Programming------questions solved about device

时间:2016-05-08 01:24:24      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:device   special files   

(1) br------ 2 root root 0,21 Feb 12 15:40 /dev/dsk15
crw-rw-- 2 root root 7,21 Mar 7 09:29 /dev/rdsk15
because the two special files propably refer to same partition,so
they refer to to same device,they are just two files of same
device,
which stands for different access method,block and character,righ
If right,does it mean there are some devices can be accessed by
both
block and character methods,but when to use block,when to use
character?

SAME device accessed through buffer cache (c) or raw (r)

In program Blockraw.c,two different file descriptors with same
offset for lseek,
what should i infer from it?(I don‘t test the program because I
don‘t find the
files using "ls")

Will print "Buffers are identicaln");

(2)In Buffer cache,to minimize the number of disk accesses,it has
delayed write method,
it is to avoid the read of the block on disk,that is,after
transferring the content to disk,
there may be modifying on the block in buffer cache again,so it‘s

better to delay, then transferring the changes together later,right?


OK, keep as much as possible the buffer in memory

(3)getblk is for allocating the buffer from the buffer pool to a
block,in breada procedure,
after getblk,why the data in the buffer can be invalid? Is it due
to getblk doesn‘t put block
into buffer,just assign the buffer to it?

The buffer is invalid because the data are not been completely
transferred.


On 01.05.2016 18:53, Pietro Laface wrote:

The offset is the same for the two special files
The access method does not change the content of the blocks that are
read,
using the character device read the transfer goes directly to the user
space, whereas
using the block device there is a double copy (to the buffer cache,
and from this buffer ti the user buffer).


(1)Today I did lab7, when I checked the result of the special files using:
cat chardev_SDP_lab,I found it was the characters I input from keyboard.

Could I say character or block device(which are special files) store data
read from input to the device;or the data is in fact inside kernel memory,
but accessed through special files?

A character driver reads from a device, through the special file, in its kernel buffer, which is then copied to the user buffer. 


(2)Since some devices like hard disk can have both block and character device
for two access methods,how to decide which method to access?

 

 

The management of block device driver is more complex, and it  has not been addressed.

A block device is used only for devices such as disks, where the minimum amount of transfer

is a block.

Raw access is typically performed by system programs such as mkfs of fsck, which of course

need to create or restore a file system

mkfs - build a Linux file system

(mkfs is used to build a Linux file system on a device, usually a hard disk partition. filesys is either the device name (e.g. /dev/hda1/dev/sdb2), or a regular file that shall contain the file system. blocks is the number of blocks to be used for the file system.

fsck - check and repair a Linux file system

fsck is used to check and optionally repair one or more Linux file systems. filesys can be a device name (e.g./dev/hdc1/dev/sdb2), a mount point (e.g. //usr,/home), or an ext2 label or UUID specifier (e.g. UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd or LABEL=root). Normally, the fsck program will try to handle filesystems on different physical disk drives in parallel to reduce the total amount of time needed to check all of the filesystems.



(3)for 7.2,I modified code as following:
ssize_t
device_read(struct file *filp, char *buff, size_t count, loff_t *offp) {
unsigned long ret;
  int ss=strlen(char_dev_buf);
  printk(KERN_INFO "the char_dev_buf is : %d\n", strlen(char_dev_buf));
        ret = copy_to_user(buff, char_dev_buf, ss);
        memset(char_dev_buf,0,strlen(char_dev_buf));
        return ss;
}

I just copy precise length of data in kernel buffer to user buffer,it works,
but I hesitate to clear or not,if not,it can have unclear output which mixes
input from different times;if yes,I think it won‘t work in real case because
it looks like just read once,then delete.Could you please give me some suggestions?

It‘s OK because you just want to pass to the user the input line up to the ‘\n‘. 


(4)I create a block device after character device works for module chardev_SDP_lab.ko,

 laface@VMdebian6:/var/log$ ls -l /dev/chardev_SDP_lab*
crw-r--r-- 1 root root 251, 0 May  6 18:15 /dev/chardev_SDP_lab
brw-r--r-- 1 root root 251, 0 May  6 21:42 /dev/chardev_SDP_lab_block

but when I tried to write into it,failed:

root@VMdebian6:/home/laface/Desktop/lab7/chardev_SDP_lab# echo something > /dev/chardev_SDP_lab_block
bash: /dev/chardev_SDP_lab_block: No such device or address

Does this mean,I can create block and character device freely,but for certain device,it can only work
with character device or block device?

See the second answer 


Conclusion:

So for drivers to access device,need to use special files,

In Unix-like operating systems, a device file or special file is an interface for a device driver that appears in a file system as if it were an ordinary file. There are also special files in MS-DOSOS/2, and Microsoft Windows. They allow software to interact with a device driver using standard input/output system calls, which simplifies many tasks and unifies user-space I/O mechanisms.

Device files often provide simple interfaces to peripheral devices, such as printers and serial ports. But they can also be used to access specific resources on those devices, such as disk partitions. Finally, device files are useful for accessing system resources that have no connection with any actual device such asdata sinks and random number generators.

  • A Character (‘c‘) Device is one with which the Driver communicates by sending and receiving single characters (bytes, octets).

  • A Block (‘b‘) Device is one with which the Driver communicates by sending entire blocks of data.

  • Examples for Character Devices: serial ports, parallel ports, sounds cards.

  • Examples for Block Devices: hard disks, USB cameras, Disk-On-Key.

  • so in the slides,i infer that both character and block devices refer to same device(cz same partition),just being accessed through buffer or not.And special file is just an interface of a device driver appeared in a file system.


System and Device Programming------questions solved about device

标签:device   special files   

原文地址:http://11259454.blog.51cto.com/11249454/1771046

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!