# Chapter 5. Multiprogramming and Memory Management

One of OS-9's most extraordinary abilities is multiprogramming, which is sometimes called timesharing or multitasking. Simply states, OS-9 lets you computer run more than one program at the same time. This can be a tremendous advantage in many situations. For example, you can be editing one program while another is being printed. Or you can use your Color Computer to control household automation and still be able to use it for routine work and entertainment.

OS-9 uses this capability all the time for internal functions. The simple way for you to do so is by putting a “&” character at the end of a command line which causes the shell to run your command as a “background task”.

The information presented in this chapter is intended to give you an insight into how OS-9 performs this amazing feat. You certainly don't have to know every detail of how multiprogramming works in order to use OS-9, but a basic working knowledge can help you discover many new ways to use your Color Computer.

In order to allow several programs to run simultaneously and without interference, OS-9 must perform many coordination and resource allocation functions. The major system resources managed by OS-9 are:

CPU Time
Memory
The input/output system

In order for the computer to have reasonable performance, these resources must be managed in the most efficient manner possible. Therefore, OS-9 uses many techniques and strategies to optimize system throughput and capacity.

## Processor Time Allocation and Timeslicing

CPU time is a resource that must be allocated wisely to maximize the computer's throughput. It is characteristic of many programs to spend much unproductive time waiting for various events, such as an input/output operation. A good example is an interactive program which communicates with a person at a terminal on a line-by line basis. Every time the program has to wait for a line of characters to be typed or displayed, it (typically) cannot do any useful processing and would waste CPU time. An efficient multiprogramming operating system such as OS-9 automatically assigns CPU time to only those programs that can effectively use the, time.

OS-9 uses a technique called timeslicing which allows processes to share CPU time with all other active processes. Timeslicing is implemented using both hardware and software functions. The system's CPU is interrupted by a real time clock many (60 in the Color Computer) times each second. This basic time interval is called a “tick”, hence, the interval between ticks is a time slice. This technique is called timeslicing because each second of CPU time is sliced up to be shared among several processes. This happens so rapidly that to a human observer all processes appear to execute continuously, unless the computer becomes overloaded with processing. If this happens, a noticeable delay in response to terminal input may occur, or “batch” programs may take much longer to run than they ordinarily do. At any occurrence of a tick, OS-9 can suspend execution of one program and begin execution of another. The starting and stopping of programs is done in a manner that does not affect the program's execution. How frequently a process is given time slices depends upon its assigned priority relative to the assigned priority of other active processes.

The percentage of CPU time assigned to any particular process cannot be exactly computed because there are dynamic variables such as time the process spends waiting for I/O devices. It can be roughly approximated by dividing the process's priority by the sum of the priority numbers of all processes:

                         Process Priority
    Process CPU Share = -------------------
                         Sum of All Active
                        Process' Priorities