mknod 创建内核设备文件【原创】

author:张继飞

写在前面,逆水行舟不进则退。

整合程序时,发现一个error,很是兴奋,原来open  /dev/下的设备时,No such file or directory,看来需要自己创建一个了,忽然忘了 mknod如何使用了。。。杯具。。翻翻以前的文章吧。

子曾经曰过:遇到问题先man一下。

# mknod --h
Usage: mknod [OPTION]... NAME TYPE [MAJOR MINOR]
Create the special file NAME of the given TYPE.

  -Z, --context=CONTEXT   set security context (quoted string)
Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file permission bits to MODE, not a=rw - umask
      --help     display this help and exit
      --version  output version information and exit

Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they
must be omitted when TYPE is p.  If MAJOR or MINOR begins with 0x or 0X,
it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;
otherwise, as decimal.  TYPE may be:

  b      create a block (buffered) special file
  c, u   create a character (unbuffered) special file
  p      create a FIFO

Report bugs to <bug-coreutils@gnu.org>.

如上,OPTION 为可选项,一般是 -m 给节点设备设置permission,(可以用创建之后用chmod 设置)

说下权限,老生常谈,

×××(所有者\组用户\其他用户)

×=4 读权限

×=2 写权限

×=1 执行权限

600 ××× (只有所有者有读和写的权限)

644 ××× (所有者有读和写的权限,组用户其他用户只有读的权限)

777 ××× (每个人都有读和写以及执行的权限)

TYPE(必选项)  表示该设备的类型为字符型还是块设备,一般来说使用ioctl 都用字符设备 也就是 c。

[MAJOR MINOR](可选项) ,主设备号与次设备号。主设备号表示某一类设备,而次设备号则是该类设备的顺序从0往下排的号。即这一类中的0,1,2,3。。。设备。

我要创建一个主设备号为 120 ,次设备号为 0 的 字符设备gpio:mknod -m 644 gpio c 120 0

至此,完成了所需设备的创建,可以正常使用open,close ioctl 等文件操作结构来连接application 与 kernel。

 

写在后面:使用该设备的前提,首先要在内核注册该设备,有驱动才可以,否则,open时会提示No such device or address 或者 No such device 。

 

回顾文章:跟文件系统定制

                 ioctl函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值