我想知道这两个函数的区别:
int register_chrdev_region(dev_t first, unsigned int count, char *name);
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);
最佳答案
参见 here有关这两个功能的详细信息。
只有事先知道要从哪个主编号开始,注册才真正有用。通过注册,您告诉内核您想要什么设备号(开始的主要/次要编号和计数),它要么给你,要么不给你(取决于可用性)。
通过分配,您告诉内核您需要多少设备号(起始次设备号和计数),它会为您找到一个起始主设备号(如果可用的话)。
部分是为了避免与其他设备驱动程序发生冲突,最好使用分配函数,它会动态为您分配设备编号。
来自上面给出的链接:
Some major device numbers are statically assigned to the most common devices. A list of those devices can be found in
Documentation/devices.txt
within the kernel source tree. The chances of a static number having already been assigned for the use of your new driver are small, however, and new numbers are not being assigned. So, as a driver writer, you have a choice: you can simply pick a number that appears to be unused, or you can allocate major numbers in a dynamic manner.Picking a number may work as long as the only user of your driver is you; once your driver is more widely deployed, a randomly picked major number will lead to conflicts and trouble.
Thus, for new drivers, we strongly suggest that you use dynamic allocation to obtain your major device number, rather than choosing a number randomly from the ones that are currently free. In other words, your drivers should almost certainly be using
alloc_chrdev_region
rather thanregister_chrdev_region
.The disadvantage of dynamic assignment is that you can't create the device nodes in advance, because the major number assigned to your module will vary. For normal use of the driver, this is hardly a problem, because once the number has been assigned, you can read it from
/proc/devices
.
有一个相关但技术上不重复的问题 here .
关于linux - register_chrdev_region 和 alloc_chrdev_region 分配设备号有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9835850/