The operating system is the most important program that runs on a computer. Every general-purpose computer must have an operating system to run other programs. Operating systems perform basic tasks, such as recognizing input from the keyboard, sending output to the display screen, keeping track of files and directories on the disk, and controlling peripheral devices such as disk drives and printers.
Operating systems can be classified as follows:
Operating systems provide a software platform on top of which other programs, called application programs, can run. The application programs must be written to run on top of a particular operating system. Your choice of operating system, therefore, determines to a great extent the applications you can run. For example, MS-Word is an application that runs on the Windows operating system, but not on the UNIX operating system.
As a user, you normally interact with the operating system through a set of commands. For example, the UNIX operating system contains commands such as cp and mv for copying files and changing the names of files, respectively. The commands are accepted and executed by a part of the operating system called the command processor or command line interpreter. Graphical user interfaces allow you to enter commands by pointing and clicking at objects that appear on the screen, as in the Windows operating system.
A task or process refers to the combination of a program being executed and bookkeeping information used by the operating system. For large multi-user and multitasking systems, the operating system has even greater responsibilities and powers. It is like a traffic cop -- it makes sure that different processes and users running at the same time do not interfere with each other. Processes running concurrently are often in contention for resources, like memory or processor time. Often only one process can have a certain resource at a time. Part of the operating system's job, then, is to decide a strategy for giving access to these resources.
You are already familiar with writing sequential programs. The majority of the Java programs from lab (such as HelloWorld, PigLatin, and PickAStudent) are sequential programs. That is, each has a beginning, an execution sequence, and an end. At any given time during the runtime of the program, there is a single point of execution. A thread is similar to the sequential programs described previously. A single thread also has a beginning, a sequence, and an end and at any given time during the runtime of the thread, there is a single point of execution. However, a thread itself is not a program; it cannot run on its own. Rather, it runs within a program. The following figure shows this relationship.
There is nothing new in the concept of a single thread. The real hoopla surrounding threads is not about a single sequential thread. Rather, it's about the use of multiple threads in a single program, running at the same time and performing different tasks. This is illustrated by the following figure:
The HotJava Web browser is an example of a multithreaded application. Within the HotJava browser you can scroll a page while it's downloading an applet or image, play animation and sound concurrently, print a page in the background while you download a new page, or watch three sorting algorithms race to the finish. You are used to life operating in a concurrent fashion...so why not your browser?
Some texts use the name lightweight process instead of thread. A thread is similar to a real process in that a thread and a running program are both a single sequential flow of control. However, a thread is considered lightweight because it runs within the context of a full-blown program and takes advantage of the resources allocated for that program and the program's environment.
As a sequential flow of control, a thread must carve out some of its own resources within a running program. (It must have its own execution stack and program counter for example.) The code running within the thread works only within that context. Thus, some other texts use execution context as a synonym for thread.
Suppose that there are many, many processes that are competing for one or two resources. Is the performance of the system affected? How?
Sometimes the operating system manages processes that are of differing importance. It may be that the priority of the process running should be taken into account when allocating resources. For example, there could be two processes running on a spaceship's computer. One controls the trajectory of the ship, and the other controls the air conditioning on the ship. Which should get more processor time? Describe a change to the model that includes a bias toward higher priority processes without starving the other processes.
Suppose that many processes are sharing one resource, a phone list. Some processes, called readers, only look up phone numbers, they never change them. Other processes, called writers, may look up numbers, but may also change the numbers. How many readers should be allowed access to the phone list at once? How many writers should be allowed access to the phone list at once? How many readers should be allowed access to the phone list while a writer has access?
The idea of having many things going on at the same time is known as multi-tasking. This idea has infiltrated many parts of our daily lives. For example, when is the last time that you ate without also watching TV or talking to someone? Have you ever walked into a room and been so engrossed in another thought that you forgot why you went there? Give other examples of daily multitasking. How hard would it be for you to do only one thing at a time?
The operating system has the responsibility to protect shared resources from misuse. In some cases, the consequences of misuse can be such that the shared data is corrupted. For example, suppose that Mr. Jones goes to the ATM to deposit $100.00 into his account, that currently contains $1,000.00. At the same time, the bank's interest program is adding 1% interest into the account. The following portions of code are run, where JONES_ACCOUNT is stored in one place that is shared by both processes: (The lines are numbered for reference only, and do not indicate order.)
|ATM code:||Interest calculator code:|
1) int amount = JONES_ACCOUNT;
2) amount = amount + 100;
3) JONES_ACCOUNT = amount;
4) int current = JONES_ACCOUNT;
5) current = current * 1.01;
6) JONES_ACCOUNT = current;
Assume that the operating system can swap processes at any point in between commands. If everything were to work correctly, Mr. Jones would end up with $1100 plus interest. If the shared resource is not protected, however, he could end up with less. Can you describe a situation in which Mr. Jones actually loses money? In particular, give an ordering of the statements in which this happens.