Saturday 30 July 2016

What's your Major?

Minor differences don't mean there can't be Major similarities (and vice-versa).
I promise this would make much more sense once you've read your way through this post. 
In UNIX, Linux and I don't know what else, every I/O operation is done through files (at least conceptually). Files on disks are files but even the peripherals attached to your computer are logically mapped to files in order to communicate data between them and the user. To find these files explore the /dev directory. 
With each  such device file, there are two numbers associated. These are the major number and the minor number. I'll come to them in a minute but first let's talk about device drivers. Device drivers are necessary pieces of code that interface hardware of excruciating variety with your operating system. This immediately presents a way in which we can classify various hardware: a peripheral can be classified by the kind of driver it uses to interface with the operating system. As an example, all USB drives can be thought as devices represented by a USB driver. Now since there are many drivers residing concurrently inside of your OS kernel, each driver must (can) be uniquely identified with a number. These are your major numbers. Every device file is given the major number of the driver which the device needs (uses).
A computer is generally capable of dealing with several devices, even if they are of the same kind (handled by the same driver and hence having the same major number). In order to differentiate with devices having the same major number, we assign them another number which is essentially serves as a serial number for all the devices of the same major. These are your minor numbers
So, a file representing a device, has with it, two numbers, major and minor. Major represents the driver that device belongs to and Minor represents that device's unique identity among all the devices with the same major. It is absolutely possible for devices to have the same major and majors to have devices of many minors. To check these out, use the following command:

ls -l /dev

It'll have an output similar to this:

 crw-rw-rw-    1 root     root       1,   3 Apr 11  2002 null
 crw-------    1 root     root      10,   1 Apr 11  2002 psaux
 crw-------    1 root     root       4,   1 Oct 28 03:04 tty1
 crw-rw-rw-    1 root     tty        4,  64 Apr 11  2002 ttys0
 crw-rw----    1 root     uucp       4,  65 Apr 11  2002 ttyS1
 crw--w----    1 vcsa     tty        7,   1 Apr 11  2002 vcs1
 crw--w----    1 vcsa     tty        7, 129 Apr 11  2002 vcsa1 
 crw-rw-rw- 1 root root 1, 5 Apr 11 2002 zero 

The 5th and 6th column show the major and minor numbers (of the files) respectively.

No comments:

Post a Comment