Multiprogramming and

Memory Management


One of OS-9's most valuable capabilities is multiprogramming

sometimes called timesharing or multitasking. This feature lets

your computer run more than one process at the same time.

Multiprogramming can be a time saving advantage in many sit

uations. For example, you can edit one program while the system

prints another. Or you can use your Color Computer to control a

household alarm system, lighting, and heating and at the same

time use it for routine work or entertainment.


OS-9 uses multiprogramming regularly for internal functions.

You can use it by putting an ampersand at the end of a com

mand line. Doing this causes the shell to run your command as

a background, or concurrent, task.


To run several processes simultaneously, OS-9 must coordinate

its input/output system and CPU time and allocate its memory

as needed. This chapter gives you some basic information about

how OS-9 manages its resources to optimize system efficiency

and make efficient multiprogramming a reality.


Processor Time Allocation
and Timeslicing

CPU time is the most precious resource of a computer. If the

CPU is busy with one task it cannot perform another. For exam

ple, many processes must wait for you to enter information from

the terminal. While the process is waiting, your computer's CPU

must also wait. Your computer is limited by your typing speed.


On many systems there is no way around such a bottle neck.

However, OS-9 is more efficient. It assigns CPU time to pro

cesses only as they need it.


To do this, OS-9 uses timeslicing. Timeslicing, as described in

the following paragraphs, lets all active processes share CPU

time.


A real-time clock interrupts the Color Computer's CPU 60 times

each second. The interruption points are called ticks, and the

spaces between ticks are called timeslices.



OS-9 Commands Reference

OS-9 allocates timeslices to each process. At any tick it can sus

pend execution of one process and begin execution of another.

This starting and stopping of processes does not affect their

execution.


How often OS-9 gives a process timeslices depends on the pro

cess's priority relative to the priority of other active processes.

You can access priority using a decimal number from 0 through

255, where 255 is the highest priority.


OS-9 automatically gives the shell a priority of 128. Because

child processes inherit their parents' priorities, the shell's child

processes also have priorities of 128. You can find a process's

priority with the PROCS command, and can change it using the

SETPR command.


You cannot compute the exact percentage of CPU time assigned

to any particular process, because there are some dynamic vari

ables involved, such as the time the process spends waiting for

1/O devices. But you can approximate the percentage by dividing

the process's priority by the sum of the priority of all active

processes:







Note: Timeslicing happens so quickly that it looks as if all

processes execute simultaneously and continuously. If, how

ever, the computer becomes overloaded with processing, you

might notice a delay in response to input from the termi

nal. Or, you might notice that a procedure program takes

longer than usual to run.


Process States

The CPU time allocation system automatically assigns each pro

cess one of three states that describes its current status. Process

states are important for coordinating process execution. A pro

cess can have only one state at any instant, although state

changes can be frequent. The states are:


      Active-Applies to processes currently able to work

      that is, those not waiting for input or for anything else.

      These are the only processes assigned CPU time.


4-2
        Multiprogramming and Memory Management / 4


    Waiting-Applies to processes that the system suspends

    until another process terminates. This state allows coor

    dination of sequential process execution. The shell, for

    example, is in the waiting state during the execution of a

    command it has initiated.


    Sleeping-Applies to a process suspending itself for a

    specified time, or until receipt of a signal. (Signals are

    internal messages that coordinate concurrent processes.)

    This is the typical state of processes waiting for input/

    output operations.


    The shell does not assign CPU time to sleeping or wait

    ing processes. It waits until they become active. The

    PROCS command gives information about process states.


Creation of Processes

If a parent process creates more than one child process, the chil

dren are called siblings with respect to each other. If you exam

ine the parent/child relationship of all processes in the system, a

hierarchical lineage becomes evident. In fact, this hierarchy

resembles a family tree. (The family concept makes it easy to

describe relationships between processes.) OS-9 literature uses

the family concept extensively in describing OS-9's multipro

gramming functions.)


OS-9's fork function automatically performs the sequence of oper

ations required to create a new process and initially allocate

resources to it.


If for any reason, fork cannot perform any part of the sequence,

the system stops and fork sends its parent an error code. The

most frequent reason for failure is the unavailability of required

resources (especially memory), or the inability of the system to

find the specified program.


A process can create many processes, subject only to the availa

bility of unassigned memory. When the parent issues a fork

request to OS-9, it must specify certain information:


      0 A primary module-The name of the program to be

      executed by the new process. The program can already

      be present in memory, or OS-9 can load it from a disk

      file with the same name.


                                4-3

OS-9 Commands Reference

    0 Parameters-Data to be passed to and used by the new

      process. OS-9 copies this data to part of the child pro

      cess's memory area. (Parameters frequently pass file

      names, initialization values, and other information.)


The new process inherits some of its parent's properties,

including:


A user number For use by the file security system to

identify all processes belonging to a specific user. (This

is not the same as the process ID, which identifies a pro

cess.) OS-9 obtains this number from the system pass

word file when a user logs on. The system manager is

always User 0.


Standard input and output paths The three paths:

input, output, and error, used for routine input and out

put. Most paths can be shared simultaneously by two or

more processes.


Current directories-The data directory and the execu

tion directory.


Process priority.

As part of the fork operation, OS-9 automatically assigns:

    · A process ID, a number in the range 1 to 255 that iden

      tifies the process. Each process has a unique number.


    · Enough memory to support the new process. In OS-9, all

      processes share a memory address. OS-9 allocates a data

      area for the process's parameters and variables and a

      stack for each process's use. It needs a second memory

      area in which to load the process if it does not reside in

      memory.


    4-4

      Multiprogramming and Memory Management / 4


    In summary, each new process has:
    A primary module
    Parameters
    A user number
    Standard I/O paths
    Current directories
    A priority
    An ID number
    Memory

    Basic Memory Management Functions

    Memory management is an important OS-9 function. OS-9 auto

    matically allocates all system memory to itself and to processes,

    and also keeps track of the logical contents of memory (the pro

    gram modules that are resident in memory at any given time).

    The result is that you seldom need to bother with the actual

    memory addresses of programs or data.


    The operating system and each process have individual address

    spaces. Each address space has the potential to contain up to 64

    kilobytes of RAM memory. Using memory management unit

    (MMU) hardware, OS-9 moves memory into and out of each

    address space as required for system operations.


    Although each unit is subject to the 64K maximum program

    size, you can run several processes simultaneously and utilize

    more than 64K overall. The system logically assigns RAM mem

    ory in 256-byte pages, but the MMU's hardware block size is

    8K. Each of these physical blocks has an extended address that

    is called a block number. For example, the 8K physical block

    residing at address $3C000 to $3DFFF is Block Number $3C.


    Within an address space, OS-9 assigns memory from higher

    addresses downward for program modules and from lower

    addresses upward for data areas. The following chart shows this

    organization:


                                  4-5

    OS-9 Commands Reference

                      highest address

    program modules

    (RAM or ROM)


    unused space

    (RAM or empty)


            data areas

            (RAM)

            lowest address


    Loading Program Modules into Memory

    When performing a fork operation, OS-9 first attempts to locate

    the requested program module by searching the module direc

    tory, which has the address of every module present in memory.

    The 6809 instruction set supports a type of program called re

    entrant code, which means that processes can share the code of a

    program simultaneously.


    Since almost all OS-9 family software is re-entrant, the system

    can make the most efficient use of memory. For example, suppose

    that OS-9 receives a request (from a process) to run BASIC09

    (which requires 22 kilobytes of memory), but has already loaded

    it into memory for another process. Because the software is re

    entrant, OS-9 does not have to load it again and use another

    22K of memory. Instead the new process shares the original

    BASIC09 by including the location of the BASIC09 module in its

    memory map.


    OS-9 automatically keeps track of how many processes are using

    each program module, and deletes the module when all processes

    using it terminate.


    If the requested program does not yet reside in memory, OS-9

    uses its name as a pathlist (filename) and attempts to load the

    program from disk.


    4-6
          Multiprogramming and Memory Management / 4


    Every program module has a module header describing the pro

    gram and its memory requirements. OS-9 uses the header to

    determine how much memory the process needs for variable stor

    age. The module header includes other information about its pro

    gram, and is an essential part of the OS-9 machine language

    operation.


    You can also place commands or programs into memory using

    the LOAD command. Doing so makes the program available to

    OS-9 at any time, without having to be loaded from disk. A pro

    gram is physically loaded into memory only if it does not already

    reside there.


    LOAD causes OS-9 to copy the requested module from a file into

    memory, verifying the CRC (Cyclic Redundancy Check). If the

    module is not already in the module directory, OS-9 adds it.


    If the program module is already in memory, the load process

    still begins in the same way. But, when OS-9 attempts to add

    the module to the module directory and notices that the module

    is already there, it merely increments the known module's link

    count (the number of processes using the module).


    When OS-9 loads multiple modules in a single file, it associates

    them logically in the memory management system. You cannot

    deallocate any of the group modules until all modules have zero

    link counts. Similarly, linking to one module within a group

    causes all other modules in the group to be mapped into the pro

    cess's address space.


    Deleting Modules From Memory

    UNLINK is the opposite of LOAD. It decreases a program mod

    ule's link count by one. When the count becomes zero (presum

    ing that the module is no longer used by any process), OS-9

    deletes the module, deallocates its memory, and removes its

    name from the module directory.


    Warning: Never use the UNLINK command on a program
    or a module not previously installed using LOAD. Unlink
    ing a module you did not LOAD (or LINK) might perma
    nently delete it when the program terminates. The shell
    automatically unlinks programs loaded by fork.

                                  4-7

    OS-9 Commands Reference

    Suppose you plan to use the COPY command ten times in a row.

    Normally, the shell must load COPY each time you enter the

    command. But if you load the COPY module into memory and

    then enter your string of commands, you don't have to wait for

    the system to load and unload COPY repeatedly. When you fin

    ish using COPY, use UNLINK to unlock the module from mem

    ory. The sequence looks like this:


      load copy ENTER

      copy filel file1a ENTER

      copy filet file2a ENTER

      copy file3 file3a ENTER

      copy file4 file4a ENTER

      copy files file5a ENTER

      copy file6 fileGa ENTER

      copy file? file7a ENTEfi

      copy file8 file8a ENTER

      copy file9 file9a ENTER

      copy file19 file19a ENTER

      unlink copy ENTER


    It is important to use UNLINK when you no longer need the

    program. Otherwise, the program continues to occupy memory

    that might be used for other purposes.


    Warning: Be careful not to unlink modules that are in use,
    because OS-9 deallocates the memory used by the module
    and destroys its contents. All programs using the unlinked
    module crash.

    Loading Multiple Programs

    Because all OS-9 program modules are position-independent, you

    can have more than one program in memory at the same time.

    Since position-independent code (PIC) programs don't have to be

    loaded into specific, predetermined memory addresses to work

    correctly, you can load them at different memory addresses at

    different times.


    PIC programs require special types of machine language

    instructions that few computers have. The ability of the 6809

    microprocessor to use PIC programs is a powerful feature and

    one of the greatest aids toward multiprogramming. You can load

    any number of program modules until available system memory

    is full.


    4-8
          Multiprogramming and Memory Management / 4


    OS-9 automatically loads each program module at non-overlap

    ping addresses. (Most operating systems write over the previous

    program's memory when loading a new program.) OS-9's tech

    nique means that you do not need to be concerned with absolute

    memory addresses.


    4-9