Linux Character driver - Creating multiple device nodes

In our previous post, we created a single device /dev/my_null, let's modify the code to create multiple device nodes for example: 5 device nodes : /dev/my_null0, /dev/my_null1, /dev/my_null2, /dev/my_null3, /dev/my_null4

While allocating the device numbers, we need to specify the number of minor devices in the count argument:

int alloc_chrdev_region (dev_t *  dev,  unsigned  baseminor, unsigned  count, const char * name);

We need to create an array of struct cdev, and register each of the cdev with one minor number.

Code:



Output:



Notes:

Try to change the MAX_DEVICES macro to other number and check how many maximum devices you can create

Comments

  1. I liked the approach of creating multiple devices. I have an issue, all these device nodes are based on the same file fops. So if all these devices are invoked concurrently by different programs, For eg. my_null0 is opened and at the same time my_null1 is also opened... wont it create any problem? i am new to device drivers, so pardon me if i am missing anything.

    ReplyDelete

Post a Comment

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig