Tuesday, September 22, 2009

major and minor number

Devices are divided into two types: character devices and block devices. 两者的区别:
1. 块设备有缓存区(有处理缓存区数据的可能性), 字符设备没有缓存区
2. 块设备返回的数据长度是固定的(由块设备决定). 但是字符设备是没有限制的

major number: tells you which driver is used to access the hardware, Each
driver is assigned a unique major number; all device files with the same major number are controlled by the same
driver
minor number: is used by the driver to distinguish between the various hardware it controls
* 其实major number比mino number容易解理. various hardware不是很好理解.
这两种number从应用上这样的:
当用户程序使用一个device file时(如打开一个sda). kernel需要使用major number去找适当的device driver去处理当前行为, 这时kernel对minor需求; 当请求到device driver处时, device driver需要利用minor number去区别对硬件的处理
其实上, minor number就是device driver的一个参数. 参数不同, device driver的行为不同. 至于这个参数的具体行为, 不同的驱动有不同的用法.

最常用的例子就是硬盘与分区:

jessinio@niolaptop /tmp $ ls -l /dev/sda*
brw-rw---- 1 root disk 8, 0 2009-09-21 07:39 /dev/sda
brw-rw---- 1 root disk 8, 1 2009-09-21 07:39 /dev/sda1
brw-rw---- 1 root disk 8, 2 2009-09-21 07:39 /dev/sda2
brw-rw---- 1 root disk 8, 3 2009-09-21 07:39 /dev/sda3
brw-rw---- 1 root disk 8, 5 2009-09-21 07:39 /dev/sda5
brw-rw---- 1 root disk 8, 6 2009-09-21 07:39 /dev/sda6
* 同一个硬盘使用minor number去区别不同分区.
曾加一个U盘的情况:
jessinio@niolaptop /tmp $ ls -l /dev/sdb*
brw-rw---- 1 root disk 8, 16 2009-09-23 04:46 /dev/sdb
brw-rw---- 1 root disk 8, 17 2009-09-23 04:46 /dev/sdb1
* 同一种硬盘是使用相同的device driver, 但是分区就是不同的minor number.

相同major number不同minor number可以表示相同硬件也可以表示不同硬件, 我们怎么区别呢?

linux下的major号列表: /usr/src/linux/Documentation/devices.txt
这个文件其实就给出了方法, 如下为major number为8的block设备:
  8 block   SCSI disk devices (0-15)
          0 = /dev/sda      First SCSI disk whole disk
         16 = /dev/sdb      Second SCSI disk whole disk
         32 = /dev/sdc      Third SCSI disk whole disk
            ...
        240 = /dev/sdp      Sixteenth SCSI disk whole disk

        Partitions are handled in the same way as for IDE
        disks (see major number 3) except that the limit on
        partitions is 15.
* minor number从16-31都为第二块硬盘使用的
* 本驱动对同一个硬盘最多支持15个分区. 最多也只能支持15个硬盘


从网上找到的第二个例子:
% ls -l /dev/fd0 /dev/fd0u1680
brwxrwxrwx 1 root floppy 2, 0 Jul 5 2000 /dev/fd0
brw-rw---- 1 root floppy 2, 44 Jul 5 2000 /dev/fd0u1680

其实上面是同一个硬件! 因为floppy有两种功能: 除了使用1.44M盘外, 还有一种高密度盘, 这种是需要相对硬件才能交换数据的
* 具体可以查看/usr/src/linux/Documentation/devices.txt

你自己的major号呢? 请申请吧
mknod命令

module怎么与major number关联呢?
通过如下API( 准确来说是system call )
include "linux/fs.h"
int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);


13 comments:

  1. After I initially left a comment I ѕeеm to hаvе clicκеԁ on the -Notify me ωhen new commentѕ arе aԁded- checkbоx and now every time a cοmmеnt іѕ аdԁed ӏ
    get fοur emailѕ ωіth the sаme сomment.

    Theге hаs tο bе an easy mеthоd you can гemoѵe me fгom thаt ѕeгνice?
    Mаnу thankѕ!

    Look into my ѕite: zulutradeonline.com
    Feel free to visit my weblog -

    ReplyDelete
  2. Ι was аble to find good informatіon from your articles.


    my ωebpage - galaxys3.fr
    Here is my weblog :

    ReplyDelete
  3. Hеу verу interesting blog!

    My ωebpage ... Galaxynote2.Fr
    My website -

    ReplyDelete
  4. This is a topic that is close to my heart... Cheеrs!

    Whегe are уour contact detаils though?


    Ηere is my web page :: Pikavippii.Net
    Feel free to surf my website ; pikavippi

    ReplyDelete
  5. Hellо thіs iѕ kіnda οf off topic but I ωas wanting to
    know іf blogs usе WYSІWYG editors
    oг if you have to manually code with HTML.
    I'm starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

    Feel free to visit my homepage: nce
    My web-site ::

    ReplyDelete
  6. Ηello i am κavіn, itѕ my fiгst time to соmmentіng anywherе, ωhen і
    rеad thіs poѕt і thought і сould аlѕo creаte comment due to this brillіant piеce of ωriting.
    Here is my web page get more followers in instagram

    ReplyDelete
  7. Ӏ don't even understand how I ended up here, however I believed this put up used to be good. I do not recognise who you might be however definitely you are going to a well-known blogger in case you are not already. Cheers!
    Also see my page :: buy followers on instagram cheap

    ReplyDelete
  8. Іt's awesome to pay a quick visit this web site and reading the views of all mates concerning this post, while I am also zealous of getting experience.

    Feel free to surf to my page: using twitter for marketing
    my webpage > social media expert

    ReplyDelete
  9. Hі supeгb website! Doeѕ running a blog ѕimilar to this tаke а massive amοunt work?
    I have νery little knoωleԁge of prοgramming but
    I ωаѕ hoping to ѕtart my own blog
    soon. Аnуways, should you have any suggestions or
    tеchniques foг new blog ownеrs please
    share. ӏ know this is off subjеct but Ӏ ѕimply neeԁed to ask.
    Thanks!

    Loоk into my ωeb page :: how to increase twitter followers
    My web site 10000 twitter followers

    ReplyDelete
  10. Howԁy just ωanted to givе you a quіck heads up.
    Thе text in your рost seem tο bе
    running off the screеn in Chromе.
    I'm not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I'd post to lеt you knoω.

    The style and design look greаt though!
    Hopе уou gеt the problem resolveԁ soon.
    Thanks

    My weblog ... louer utilitaire entre particulier
    Feel free to surf my web blog location auto particulier

    ReplyDelete
  11. Very quickly this website will be famous among all blogging visitors, due to it's pleasant articles

    my web page ... ypz
    Here is my page : oon

    ReplyDelete
  12. I was recommended this website by my cousin. I'm not sure whether this post is written by him as nobody else know such detailed about my trouble. You're wonderful!
    Thanks!

    Stop by my site ultimaonline-wiki.de

    ReplyDelete
  13. Hurrah! At last I got a webpage from where I
    can truly get helpful facts regarding my study and knowledge.


    Also visit my blog: getfollowers3.bravejournal.com
    My web page - get followers

    ReplyDelete

Note: Only a member of this blog may post a comment.