Friday, August 15, 2008

OS Concepts Part -1


















How helpless we all feel when any of our electronic devices stops working all of a sudden and shows no immediate sign of recovery..

This post helps us understand this master clock better; the one which drives electronic hardware at one end and the user at the other..

In the jargon of computers, we term this master clock as Operating Systems!!




During my primary education, the first OS i was introduced to was MS-DOS.

It is a non-graphical single user operating system by Microsoft primarily meant for desktop computers, developed over Intel 8086 micro processor. The high-level programming language used was BASIC (Beginnner's All-purpose Symbolic Instruction Code )

MS-DOS gave a alphabetic notation to each hard drive: C used to be the boot disk, A was primarily meant for floppies, D used to be the CD Driver.

MS-DOS provides a text based command line interface un
like the later operating systems like Microsoft ME, NT, 2000 or XP which provide a graphical user interface.

As i moved to the college, i was thrown to some more jargons, with quite complex names i would say, whose correct pronunciation i am still not aware of: Linux, Solaris, Unix.....



At the first go, these OS lo
oked quite un-user friendly to me!
Unix to m
e looked like a hierarchy of file system with certain paths pre-stored in the user profile, something that provides lot many shells to work in; e.g. the bash shell.



UNIX OS was developed by Bell Labs to address th
e compatibility issues posed by the usage of various PC vendors. They gifted the computer world a special code known as Kernel, written in a middle language C.

Linux provides a great package of C
compliers, libraries, man help pages, development and debugging tools. It can be downloaded completely free from the internet.

"De gustibus et coloribus non disputandum est": there's a Linux for everyone.

Solaris on the other hand, is a multi-tasking, multi-processing UNIX-based operating system designed by SUN microsystems.

Remember, all these Unix based OS are case-sensitive, which sounds complex but then makes the system more secure !

When i joined DSP group in my company Aricent 4 years back, i was informed that all GSM products here are developed on 2 RTOS (real time operating system ) BIOS and QNX ???

Wikipedia enlightened me further that RTOS is a multi-tasking operating system intended for real-time applications like mobiles, robots and spacecrafts. The response time in RTOS is quite less, not even within our imagination.
Imagine, 2 people talking on the phone: say one is in New Jersey and the other is in Saudi Arabia. As soon as the guy from the west utters a word, the mind of the guy in the east immediately starts processing the input.



To meet such tight time constraints, RTOS will even need to drop data at times. In terms of GSM, every meaningful bunch of data is transmitted every 477 us. RTOS works on a lot many threads which can be further prioritized.

What is this 'thread' now ??

As defined in whatis.com, a thread is the information needed to serve one individual user or a particular service request. If multiple users are using the program or concurrent requests from other programs occur, a thread is created and maintained for all of them.

Now many thread can be contained inside one process sharing some common resource, say all the pages which need to be printed are a part of the I/O process all sharing the printer software queue buffer.

Computer users take it for granted that their systems can do more than one thing at a time. We play songs, check e-mails, video chat on yahoo messenger, review an adobe document & produce L1 code using TI CCS studio almost at the same time..)

Processing time for a single processor is shared among processes and threads. That signifies why multi processors always remain in such a high demand!

Every thread has a priority. Threads with higher priority are more important to a process and should be allocated processor time before lower priority threads.

When a higher priority thread enters the ready state, the operating system generally pre-empts the currently running thread. This operation is known as 'pre-emptive scheduling'.

Now a higher priority thread could indefinitely postpone the execution of lower priority threads. This indefinite postponement is in one word described as 'starvation'.

Any thread will not always remain in action. It always has some sleep interval. Threads sleep when they momentarily do not have work to perform. e.g. a word processor thread periodically writes a copy of the current document to disk for recovery purposes. This thread sleeps for sure in between those successive backups..)

Now lets try to understand further what would happen when two different threads, acting on the same data interfere! It somewhere reflects that both the threads follow just one algorithm and some of their steps overlap.

Consider a simple class called Counter:
class Counter {
private int c = 0;

public void increment() {
c++;
}

public void decrement() {
c--;
}

public int value() {
return c;
}

}
As explained in the code segment above, increment API() adds 1 to variable c and decrement API() subtracts 1 from c.

Suppose Thread A invokes increment at about the same time Thread B invokes decrement. If the initial value of c is 0, their interleaved actions might follow this sequence:

  1. Thread A: Retrieve c.
  2. Thread B: Retrieve c.
  3. Thread A: Increment retrieved value; result is 1.
  4. Thread B: Decrement retrieved value; result is -1.
  5. Thread A: Store result in c; c is now 1.
  6. Thread B: Store result in c; c is now -1.
Thread A's result is lost, overwritten by Thread B. This particular interleaving is only one possibility. Under different circumstances it might be Thread B's result that gets lost, or there could be no error at all. Because these are unpredictable, thread interference bugs can be difficult to detect and fix.

What is the key to avoiding such memory inconsistency errors ???

Well, the answer in one word would be "Synchronization", something that i will ponder upon in detail in my next post!

It's time now to do a quality check and see how many questions we can answer -:)

1. Give examples of two situations in which user convenience conflicts with efficient use of a computer system.

2. Comment on validity of the following statement: "A dormant program also consumes resources."

3. An OS cannot meet the response requirement of a real time application if it is executed as a single process.Explain with an example how creation of multiple processes can help to meet the response requirements of the application in the same OS ?

4. If two independent events e1 and e2 have the probability of occurrence of pr1 and pr2, where both pr1 and pr2 <1, the probability that both events occur at the same time is pr1 * pr2. A computer system containing two CPUs is to be designed that the probability that both CPUs fail is 1 in 10000. What should be the probability of failure of a CPU?
.
.
5. An application is to be coded using threads. Describe the conditions under which you would recommend use of
.
(a) kernel-level threads
.
(b) user-level threads

1 comment:

Sandy said...

I was struggling to find enough time to read this post of yours since sunday, when I had first glanced it. The post for sure is quite quick to cover too many things, which people who are familiar with understand. But still, its quite informative for novices even.

The part that fascinated me the most was the journey through various operating systems, which reminds me of my own musings. I remember how QNX was at one time had become a craze as being the GUI RTOS on a single floppy disk with an included browser.

There is so much thats coming to my mind, that I am anxiously waiting for the next in the series.

Cheers for the good work!


Mindbox