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.

Difference between size_t and ssize_t

Remember how typedef is used to give alternate names to data types and how this is done to add an extra layer of context to our variables? Of course you do, you seem very intelligent, almost a genius. 
Today I'll be presenting an example based on the very concept. Be sure not to miss out, it's as ubiquitous  in C as obesity is in Cities.

size_t is an alias for a 16 bit integer. It is unsigned in nature. How did this alias came to form? Via the following statement (as I'd like to think):

typedef unsigned int size_t;

What is the use case for this alias? Representing sizes of things in memory. It literally stands for size of type. So whenever you wish to store the size of some variable in another variable, make sure to have the second variable be of the type size_t. Yes, you can very well ignore this advice and have it be of unsigned int but doing so will make it clear for any one reading your code that what this variable is there for i.e. storing sizes of stuff. It's not an overhead. You don't have to worry about writing the above line of code to be able to use this alias. It has been done for you and comes as part of the C language. This fact shall come as a ringing endorsement for the legitimacy of the size_t type. In fact, the famously used sizeof operator in C has been defined as returning the type size_t. Why? well because it returns the size of something.

ssize_t is an alias for a 16 bit signed integer. This means that it can even take negative values. It exists due to some statement buried deep in C, similar to the following one:

typedef int ssize_t;

This alias is used in cases where there is a chance of a failure in memory allocation. But first back up a bit. When you assign some amount of memory to a pointer, then this can either result in your pointer getting the requested amount of memory or a failure of your request and no memory being allocated. In the former (preferred) case, the allocated memory (in bytes) is returned using ssize_t type. If the allocation fails then a negative number is returned, which is again done using the ssize_t type as it can hold negative values as well.

You're definitely a genius now. It's pandemic.