[comp.unix.questions] Major and Minor Devices

vijay@ddsw1.MCS.COM (Vijay Gurbani) (02/05/91)

Hi :
  Could anybody explain what the major and minor devices are?

  Thanks...
  -vijay
-- 
--
Vijay Gurbani       | "I'm not bad...I'm just drawn that way"- Jessica Rabbitt
vijay@ddsw1.mcs.com | "I'm not a bug...I'm just a feature implemented that 
Chicago, IL         |________ way...!!" 

mike (02/07/91)

In an article, ddsw1.MCS.COM!vijay (Vijay Gurbani) writes:
>  Could anybody explain what the major and minor devices are?

By way of introduction ...

Under UNIX, devices are "special" files that are two flavors, character-special
and block-special.  Character-special files are used for character-at-a-time
devices, such as terminals, modems, printers, etc.  Block-special files are
used when the driver buffers I/O on the device, such as disks.  Note
that in the case of disks, there are usually both character-special and
block-special files, which are used for different reasons.

Now, to answer the question ...

Each device file has a device number, which is broken down into a major device
and minor device number.  Simply put, the major device number lets the kernel
know which device you are talking about; the minor device number is used by
the device driver itself, with different minor numbers telling the driver
to behave in different ways.

Device drivers are linked in with the kernel (in most cases anyway; there are
some exceptions), and their major number is defined in the /usr/sys/master
file (or wherever the particular flavor of UNIX chooses to keep it; for
example, XENIX calls it /usr/sys/conf/xenixconf, but the principal is the 
same).

So, let's say that you have a tape device driver that is linked in
with the kernel, and the major device number is 15.

# ls -l /dev/rmt0*
crw-rw-rw-   1 root     system    15,  0 Feb  6 00:14 /dev/rmt0
crw-rw-rw-   1 root     system    15,  4 Aug  3 1990  /dev/rmt0.4

This would show that /dev/rmt0 and /dev/rmt0.4 both refer to the same
device, because the major device number for both character-special files
is 15.  In this case, a minor device number of 0 means "normal tape"
operation, and 4 means "no rewind on close" of the tape device.
Most devices' minor number is determined by which bits are turned on.
So, if the driver writer used bit 3 to turn on "no rewind", then a
binary 00000100 would be used, which is decimal 4.

Hope this rambling helped explain things a bit.
-- 
Michael Stefanik                       | Opinions stated are not even my own.
Systems Engineer, Briareus Corporation | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
technoignorami (tek'no-ig'no-ram`i) a group of individuals that are constantly
found to be saying things like "Well, it works on my DOS machine ..."

guy@auspex.auspex.com (Guy Harris) (02/08/91)

>Each device file has a device number, which is broken down into a major device
>and minor device number.  Simply put, the major device number lets the kernel
>know which device you are talking about; the minor device number is used by
>the device driver itself, with different minor numbers telling the driver
>to behave in different ways.

A little *too* simply put, I'd say.  The major device number mainly
indicates what *type* of device; one thing the minor device number is
used for, although not the only thing, is to indicate which particular
device of that type is being referred to.  I.e.:

># ls -l /dev/rmt0*
>crw-rw-rw-   1 root     system    15,  0 Feb  6 00:14 /dev/rmt0
>crw-rw-rw-   1 root     system    15,  4 Aug  3 1990  /dev/rmt0.4
>
>This would show that /dev/rmt0 and /dev/rmt0.4 both refer to the same
>device, because the major device number for both character-special files
>is 15.

Well:

	alpha1% ls -l /dev/rast[012]
	crw-rw-rw-  1 root      87,   0 Jan 14 12:19 /dev/rast0
	crw-rw-rw-  1 root      87,   1 Jan 10 08:23 /dev/rast1
	crw-rw-rw-  1 root      87,   2 Jan 15 09:04 /dev/rast2

They all have the same major device number, but they aren't the same
device; they're three different tape drives.  The lower 2 bits of the
minor device number, on that particular system, specify the unit number.
The next bit up indicates the "normal" vs. "no rewind on close" flag:

	alpha1% ls -l /dev/*rast0
	crw-rw-rw-  1 root      87,   4 Feb  2 00:30 /dev/nrast0
	crw-rw-rw-  1 root      87,   0 Jan 14 12:19 /dev/rast0

(Also, for assorted reasons, the flavor of UNIX running on that
particular system also uses part of the *major* device number to
indicate which unit is being referred to; that's actually not strictly
necessary for tapes, but it *is* for disks, since:

	you can hang up to 60 disks on those machines;

	each disk can have up to 8 partitions, and each partition is a
	separate device with its own minor device;

	minor device numbers in that OS, as in most versions of UNIX,
	are 8 bits long.

That sort of thing is relatively rare, and may get rarer as systems go
to larger minor device numbers; minor device numbers are longer than 8
bits in most if not all System V Release 4 systems, for example.)