Monday, May 7, 2012

The Wizard of OS

Imagine five philosophers who have met to dine together. They sit at a round table, with a bowl of noodles in front of each, and a chopstick between the bowls. Every philosopher thinks for a while, and when he gets hungry, picks up a chopstick from one side, if it is free, and then the chopstick from the other side. Only when he is in possession of both chopsticks can he eat. Now, if all the philosophers decide to eat at the same time, and all of them pick up their left chopsticks, none of them can eat - there are no more chopsticks free. They all starve.

That is the famous problem of the dining philosophers - just one of the problems that a multi-tasking operating system has to handle. Some changes have to be made to the allegory of course - the philosophers are running programs, the chopsticks represent the resources required for input or output, and the bowl of noodles the input or output itself.

An operating system is a set of programs that make a computer come alive. A computer is a piece of junk, a mess of semiconductors and wires until an operating system gets to work. The operating system sits in the computer's memory, and co-ordinates all that goes on in the computer. It waits for the user to give a command, and when the command finally comes, understands it, and executes it. It allows the user to load application programs like word processors, spreadsheets, and database systems into the memory, and gets the computer to run them.

At a "lower" level, the operating system provides services to the user's programs. When the word-processor makes a request to open (begin to use) a file, the operating system actually does the work of moving the heads of the disk drive to the correct location, and reading the data from there into special places in memory. It is the operating system that magically turns an inanimate disk drive, consisting of just magnetic platters and heads into a coherent file system. It sets aside some space on the disk for a table where filenames and other information about files, such as the date they were created and their size, can be stored. Then it provides the means of accessing information stored in the files, by furnishing services to open, close, read from and write to files, for example.

The operating system also takes charge of all the input and output devices connected to a computer, like the keyboard, terminal screen, printer and disk drives, and co-ordinates their activities. This not an easy job - what is the use of a computer that, for example, if when it is busy printing something loses what the user types on his keyboard. When anything important like the pressing of a key occurs, it is often the operating system that makes the computer suspend the calculations that it was performing, and attend to the cause of the alarm. A modern computer is capable of switching back and forth quickly from one job to another. Very often, we use application programs that display a clock in one corner of the screen, which changes its time every second, without affecting what we do; we don't realise that every second the computer's attention has left us to attend to the clock.

At a "higher" level, an operating system typically provides a "shell" where the user can enter commands. (The part of the operating system that provides the basic organisation of the hardware, and the services is called the "kernel".) The shell understands many commands, for example a command to list all the files on a disk - such a command is "dir" on MS-DOS, and "ls" on Unix. Other commands include those to copy a file, to delete it from the disk, and so on. Since the user interacts with the shell (which in turn interacts with the kernel, which uses the underlying hardware), and users are now increasingly novices, the shell is the subject of improvement. Newer operating systems, like Windows-NT and the Macintosh operating system present a graphical user interface (GUI - programmers say "gooey": that's what they think of it) to the user. The user sees a lovely multicoloured screen, and gets his or her work done using a mouse, by moving to pictures (called icons), and clicking mouse buttons. There is no need to remember and to type any command at all.

The development of operating systems naturally enough parallels the development of computers. In the earliest computers, where miles of magnetic tape had to be loaded and unloaded manually, the computer idled for a lot of time. Therefore a "batch operating system" was invented, where a series of operations could be specified at one time, in a batch, at executed one after the other, quickly. With faster disk drives coming into the picture, multi-programming was tried out. This means running several programs at the same time, and arises out a simple fact. However fast we try to make disks drives, they are still not able to match the computing speeds of a computer - still, programs need disks to store their large amounts of data permanently. So, when a program is either reading or writing data, the disks may be working hard, but the computer itself is idle. At this time the multi-programming operating system comes in and switches the computer's attention to another program that is at a calculations stage. Thus, a multi-programming system makes efficient use of an entire computer installation.

Computers are getting still faster, and their users more demanding. Clearly, a multi-programming system with say fifty users typing letters would not work: each user would get attended to once in a long time. For letter writing and other interactive applications, the computer has to respond fast, or what you have typed at the keyboard will take centuries to come on the screen. For such people, we have "time sharing" systems, where the computer switches from one person to another every hundredth of a second or so (an average typist can type thirty words in a second). We have to make sure that the epistolers are happy, and that the philosophers don't starve.

© 1994-2012, Sualeh Fatehi. All rights reserved.
This article was written in 1994, and published in Express Computer, India's leading national computer weekly, in October 1997.