Understanding /dev, Major and Minor Numbers
The /dev directory in Linux is where you find device filesβspecial files that represent hardware devices and virtual devices managed by the kernel. These files act as an interface between user space applications and device drivers. Each device file in /dev is associated with a specific driver in the kernel, and interacting with these files allows you to communicate with the underlying hardware or virtual device. Device files are typically categorized as either character devices or block devices, and each is identified by a pair of numbers: the major and minor numbers.
device_registration_example.c
12345678910111213141516171819202122232425262728293031323334#include <linux/module.h> #include <linux/fs.h> #include <linux/init.h> #define MY_MAJOR 240 #define MY_MINOR 0 #define MY_DEVICE_NAME "mychardev" static int __init my_driver_init(void) { int result; dev_t dev = MKDEV(MY_MAJOR, MY_MINOR); result = register_chrdev_region(dev, 1, MY_DEVICE_NAME); if (result < 0) { printk(KERN_ALERT "Failed to register device region\n"); return result; } printk(KERN_INFO "Device registered with major %d and minor %d\n", MY_MAJOR, MY_MINOR); return 0; } static void __exit my_driver_exit(void) { dev_t dev = MKDEV(MY_MAJOR, MY_MINOR); unregister_chrdev_region(dev, 1); printk(KERN_INFO "Device unregistered\n"); } module_init(my_driver_init); module_exit(my_driver_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Simple device registration example");
Device files in /dev are mapped to kernel drivers using the major and minor numbers. The major number identifies the driver associated with the device, telling the kernel which driver should handle operations for that device file. The minor number is used by the driver to distinguish between different devices or instances it manages. In the code above, you register a character device region with a specific major and minor number, establishing a link between a device file (which you can create in /dev) and your driver. When a user space application opens the device file, the kernel uses the major number to route requests to your driver's functions.
create_device_node.c
123456789#include <stdio.h> #include <stdlib.h> int main() { // This would normally be run as: mknod /dev/mychardev c 240 0 system("mknod /dev/mychardev c 240 0"); printf("Device node /dev/mychardev created with major 240 and minor 0\n"); return 0; }
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.85
Understanding /dev, Major and Minor Numbers
Swipe to show menu
The /dev directory in Linux is where you find device filesβspecial files that represent hardware devices and virtual devices managed by the kernel. These files act as an interface between user space applications and device drivers. Each device file in /dev is associated with a specific driver in the kernel, and interacting with these files allows you to communicate with the underlying hardware or virtual device. Device files are typically categorized as either character devices or block devices, and each is identified by a pair of numbers: the major and minor numbers.
device_registration_example.c
12345678910111213141516171819202122232425262728293031323334#include <linux/module.h> #include <linux/fs.h> #include <linux/init.h> #define MY_MAJOR 240 #define MY_MINOR 0 #define MY_DEVICE_NAME "mychardev" static int __init my_driver_init(void) { int result; dev_t dev = MKDEV(MY_MAJOR, MY_MINOR); result = register_chrdev_region(dev, 1, MY_DEVICE_NAME); if (result < 0) { printk(KERN_ALERT "Failed to register device region\n"); return result; } printk(KERN_INFO "Device registered with major %d and minor %d\n", MY_MAJOR, MY_MINOR); return 0; } static void __exit my_driver_exit(void) { dev_t dev = MKDEV(MY_MAJOR, MY_MINOR); unregister_chrdev_region(dev, 1); printk(KERN_INFO "Device unregistered\n"); } module_init(my_driver_init); module_exit(my_driver_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Simple device registration example");
Device files in /dev are mapped to kernel drivers using the major and minor numbers. The major number identifies the driver associated with the device, telling the kernel which driver should handle operations for that device file. The minor number is used by the driver to distinguish between different devices or instances it manages. In the code above, you register a character device region with a specific major and minor number, establishing a link between a device file (which you can create in /dev) and your driver. When a user space application opens the device file, the kernel uses the major number to route requests to your driver's functions.
create_device_node.c
123456789#include <stdio.h> #include <stdlib.h> int main() { // This would normally be run as: mknod /dev/mychardev c 240 0 system("mknod /dev/mychardev c 240 0"); printf("Device node /dev/mychardev created with major 240 and minor 0\n"); return 0; }
Thanks for your feedback!