天天看點

linux 主裝置号為1 以及/dev/null的介紹

介紹個特殊的字元裝置,主裝置号為1的裝置

  1 char Memory devices

  1 = /dev/mem Physical memory access

  2 = /dev/kmem Kernel virtual memory access

  3 = /dev/null Null device

  4 = /dev/port I/O port access

  5 = /dev/zero Null byte source

  6 = /dev/core OBSOLETE - replaced by /proc/kcore

  7 = /dev/full Returns ENOSPC on write

  8 = /dev/random Nondeterministic random number gen.

  9 = /dev/urandom Faster, less secure random number gen.

 10 = /dev/aio Asynchronous I/O notification interface

 11 = /dev/kmsg Writes to this come out as printk's, reads

export the buffered printk records.

 12 = /dev/oldmem OBSOLETE - replaced by /proc/vmcore

主裝置号為1 的裝置嚴格上都不是真正的字元裝置,都是和mem有關系的 MEM_MAJOR 1

例如:/dev/null 核心中有些資訊不适合在螢幕上顯示,又不适合重定向到磁盤中,這時候

/dev/null起到直接把資料消失的作用。

實作代碼原理如下:

最終執行是在file_operations中的,是以從檔案操作開始:主裝置号為1的如下:

static const struct file_operations memory_fops = {

.open = memory_open,

.llseek = noop_llseek,

};

static int memory_open(struct inode *inode, struct file *filp)

{

int minor;

const struct memdev *dev;

minor = iminor(inode);  //擷取次裝置号

if (minor >= ARRAY_SIZE(devlist)) //主裝置号為1的devlist裝置清單 ,判斷是否在範圍内

return -ENXIO;

dev = &devlist[minor];    //從清單中擷取裝置指針

if (!dev->fops)           //判斷檔案操作是否有效  

return -ENXIO;

filp->f_op = dev->fops;      //指派檔案操作指針

filp->f_mode |= dev->fmode;   

if (dev->fops->open)        //如果非空,執行自己的open操作 /dev/null為空

return dev->fops->open(inode, filp);

return 0;

}

static const struct file_operations null_fops = {

.llseek = null_lseek,

.read = read_null,

.write = write_null,

.read_iter = read_iter_null,

.write_iter = write_iter_null,

.splice_write = splice_write_null,

};

上面filp->f_op = dev->fops;     後就可進行讀寫操作了。

讀寫操作如下:

static ssize_t read_null(struct file *file, char __user *buf,

 size_t count, loff_t *ppos)

{

return 0;

}

static ssize_t write_null(struct file *file, const char __user *buf,

  size_t count, loff_t *ppos)

{

return count;

}

沒錯,什麼也沒做直接傳回了。進而掩蓋事實。做了啊,實際什麼也沒做。

起到一些消息不得不管理,統一空處理的作用。