(edited 9-Jul-91) (Using the Foreground/Background Monitor\int_fb_chap)

If your processor can support a mapped monitor, you should investigate using it rather than the unmapped FB monitor, as mapped monitors are capable of utilizing the full power of your processor. See (map_req_sec) to determine if your processor can support mapped monitors.

If the test you performed in (map_req_sec) indicates you cannot use a mapped monitor, you can use the FB monitor and run two programs concurrently.

Use the on-line index utility, INDEX, and see the on-line help utility, HELP, for more information on topics described in this chapter. (The Foreground/Background Environment)

The foreground/background environment is designed so that two programs can run concurrently. One of these programs you designate as the foreground program. The system gives priority to the foreground program (or job, as it is usually called) and lets it run until some condition, perhaps waiting for an I/O completion, causes it to relinquish control to the other program (the background job). The system then lets the background job run until the foreground job again requires control, and so on. In this way, the two programs share system resources. Whenever the foreground program is idle, the background program can run. Yet whenever the foreground program requires service, its requests are immediately satisfied. To the user at the terminal, the two programs appear to run simultaneously.

Foreground priority programs are generally time critical. For example, you may want to designate as the foreground job a program that collects and analyzes data. Background programs are usually non-time critical. Because the keyboard monitor and many RT--11 utilities are designed to run as the background job, you can continue to do program development by using monitor commands to run the editor, the FORTRAN compiler, the linker, and so forth.

To perform the following exercises your system must have a clock. Verify whether your system has a clock by entering the TIME command twice. If the time displayed changes, your system has a clock. If your system does not have a clock you should go to (feat_part). (Running the Foreground/Background Programs)

Two programs are provided for you to run a foreground/background demonstration. The background job is called DEMOBG, and the foreground job DEMOFG. The function of the foreground job is to send messages every two seconds to the background job, telling it to cause the terminal to beep. The background job recognizes these messages and beeps once for each message sent by the foreground job.

Although the foreground job is always active, sending messages to the background job every two seconds, other programs besides DEMOBG can be executed in the background. Only when DEMOBG is active, however, is the circuit complete so that messages can be successfully received and honored. During the periods when DEMOBG is not running, the foreground program enters the messages in the monitor message queue. Once you restart DEMOBG in the background, the system immediately dequeues all the messages since the last exit of DEMOBG, resulting in many successive beeps. When the queue is empty, the normal send/receive cycle resumes, and the terminal beeps every two seconds as each current message is sent and honored. (Creating the Background Job )

The background program DEMOBG.MAC is an assembly language source file and must be assembled and linked before you can use it. When you execute DEMOBG, it displays a message on the terminal. It is assumed that you have set the date. (Editing the Background Job )

Use KED to modify the background job, DEMOBG.MAC. One of the lines of the message that is output by the program has a semicolon character preceding it, which makes the line a comment field. This will prevent the line from being printed as part of the message. Thus, the semicolon must be deleted from that line.

Change the line

; .ASCII /WELL DONE./

to

.ASCII /WELL DONE./ (Running the Background Job)

The demonstration in (int_macro_chap) illustrates assembling and linking MACRO programs. If you are not familiar with assembly/link operations, type the command lines as shown.

Assemble the background job. (.)(MACRO DEMOBG/LIST(RET))

Link the .OBJ file produced by the assembler to create a runnable job. (.)(LINK DEMOBG(RET))

Now run the background job and check the results. (.)(RUN DEMOBG(RET)) (RT-11 DEMONSTRATION PROGRAM) (IF INCORRECTLY EDITED, THIS IS THE LAST LINE.) (WELL DONE.)

If you did not delete the semicolon character, the last line will not be output. Return to the monitor by typing two successive CTRL/Cs. ((CTRL/C)) ((CTRL/C)) (^C) (^C) (.) (Using the FB Monitor)

The FB monitor provides you with commands that let you control the two-job environment. They let you interact with the two jobs and let the two jobs interact with one another. (Communication in a Two-Job Environment)

When two jobs run simultaneously, you must have some means of indicating the job to which you are directing commands. Likewise, the two jobs must have the means to identify themselves when they have messages to print. The following conventions apply to system communication in a two-job environment. (numbered) The foreground job has priority. If both the foreground and the background job are ready to print output at the same time, the foreground job prints first. The foreground job prints a complete line, then the background job prints a complete line, and so on. Either job can interrupt your input at the terminal if it has a message to print. Messages printed by the background job are preceded by the characters B(>). Messages printed by the foreground job are preceded by the characters F(>). Typed commands are initially directed to the background job. You can redirect control alternately to the foreground and background jobs by using the CTRL/F and CTRL/B commands.

To direct typed input to the foreground job, press (CTRL/F). This command instructs the monitor that all subsequent terminal input -- commands and text -- is directed to the foreground job. Typing this command causes the system to print an F(>) on the terminal, unless output is already coming from the foreground job. Command input remains directed to the foreground job until the foreground job terminates, or until it is redirected to the background job through CTRL/B.

To direct typed input to the background job, press (CTRL/B). This command instructs the monitor that all subsequent terminal input -- commands and text -- is directed to the background job. Typing this command causes the system to print a B(>) on the terminal, unless output is already coming from the background job. Command input remains directed to the background job until redirected to the foreground job through CTRL/F. (Creating the Foreground Job)

The foreground program DEMOFG is an assembly language source file; it must be assembled and linked before you can use it. (.)(MACRO DEMOFG/LIST (RET))

The output resulting from this MACRO command includes an object file called DEMOFG.OBJ and a listing file called DEMOFG.LST. The command creates both files on your default data (DK) volume. You must link the .OBJ file to produce a runnable foreground program. (int_link_chap) describes linking operations. If you are not familiar with linking operations, issue the following command as shown. Note that you use the LINK command here with the /FOREGROUND option.(1\This command option also applies to compiled FORTRAN programs that are to be linked as a foreground job.) This option produces a load module with a .REL file type which signifies to the system that the file can be run as the foreground program; the priority job. (.)(LINK/FOREGROUND DEMOFG (RET)) (Executing the Foreground and Background Jobs)

Now, you are ready to operate the two-job environment. Many times, you have to consider the devices that are used for output in a foreground/background environment. For example, if your program assumes that the output device is a printer and you want to output to another device, use the ASSIGN command, as described in (aln_sec).

You do not have to consider that information for the demonstration programs that are provided, since the foreground job communicates with the background job, and both jobs send their output to the terminal.

When you use the FB monitor, you must always load into memory the peripheral device handlers needed by the foreground job. You can determine if a device handler is already loaded by issuing the SHOW command. You use the LOAD command to load a device handler in memory. For example, if your foreground job requires the use of the printer, you must load the printer (LP or LS) device handler. You should also specify the job name with the command. For a foreground job, the job name is F; for a background job, the job name is B. You can display numerous examples of device handler loading and job name assignments by typing or printing the FB monitor start-up command file, STRTFB.COM.

Use the FRUN command to run a foreground job, which is similar to RUN except that the system automatically loads and starts the execution of the foreground .REL program. (To execute a FORTRAN foreground job, you should use the /BUFFER:n option with the FRUN command. The argument (n) represents, in octal, the number of words of memory to allocate.)

Use this command to start the execution of DEMOFG.REL. (.)(FRUN DEMOFG(RET)) (F>) (FOREGROUND DEMONSTRATION PROGRAM) (SENDS A MESSAGE TO THE BACKGROUND PROGRAM "DEMOBG") (EVERY 2 SECONDS, TELLING IT TO BEEP.) (B>)

The foreground program DEMOFG is now running and queuing the message for the background program every two seconds. You now execute the background program DEMOBG to let it receive the messages that were queued and to beep the terminal. (.)(RUN DEMOBG (RET)) (RT-11 DEMONSTRATION PROGRAM) (IF INCORRECTLY EDITED, THIS IS THE LAST LINE.) (WELL DONE.)

The terminal beeps several times in rapid succession as the monitor dequeues the messages, and then every two seconds as the foreground job sends its message to the background job.

You can run other jobs in the background. First, terminate the background job DEMOBG by pressing (CTRL/C) twice: ((CTRL/C)) ((CTRL/C)) (.)

Execute a DIRECTORY command in the background to get a listing of all the .OBJ files on the default data (DK) volume by typing: (.)(DIRECTORY *.OBJ(RET))

The foreground job is still running and queuing its messages to the monitor. Rerun the background program to collect all the foreground messages while the background job was stopped and the directory was printing. (.)(RUN DEMOBG (RET)) (RT-11 DEMONSTRATION PROGRAM) (IF INCORRECTLY EDITED, THIS IS THE LAST LINE.) (WELL DONE.)

The terminal again beeps several times in succession and then beeps once every two seconds. Stop the background job by pressing (CTRL/C) twice: ((CTRL/C)) ((CTRL/C)) (.)

Now, stop the foreground job and remove it from memory. To do this, you must first press (CTRL/F) to direct terminal input to the foreground: (.)((CTRL/F)) (F>)

The system prints the characters F(>) to remind you that you are now directing command input to the foreground job. Press (CTRL/C) twice to interrupt and terminate the execution of the foreground job and return control to the background job: ((CTRL/C)) ((CTRL/C)) (B>)

You should unload the foreground job to reclaim memory space for background use. Use the UNLOAD command as follows: (UNLOAD F (RET)) (.)

F represents the foreground job; you should use this code whenever you want to unload the foreground job. To unload any loaded device handlers, you must use their 2-letter device mnemonics.

The foreground program has access to all the system features available to a background program -- opening and closing files, reading and writing data, and so on. However, before you begin to write and use programs in the foreground, read the (ssm_book) for coding restrictions. (Useful Information in (mon_part))

System jobs, as described in Chapter (int_sysjob_chap\value), are a variant of foreground jobs. System jobs can also be run as the foreground job, as shown in the FB monitor start-up command file, STRTFB.COM. The main difference is you can run multiple system jobs at the same time while you can run only one foreground job. Therefore, even if your processor cannot support a mapped monitor, you can learn a lot about using the foreground environment by: (numbered) Printing STRTFB.COM. Examining Sections (sysjob_bef_sec\value) through (sysjob_ind_sec\value), Section (sysjob_abort_sec\value), and Section (sysjob_unload_sec\value).

Remember that those sections describe running multiple system jobs and you are interested in the information in terms of running a single foreground job. Also, remember that you can run the SL command line editor and still run a foreground and background job; SL behaves like a handler and not like a job. Examine the information in (int_startup_chap) while looking at your listing of STRTFB.COM. Again, much of the information in Chapter (int_startup_chap\value) is useful. The requirements for running a system job are much like those for running the same job as the foreground job. (Chapter Summary)

The following is a summary of control commands used in a foreground/background environment.

((CTRL/B)\bold) (simple) Directs all keyboard input to the background job (until CTRL/F).

((CTRL/F)\bold) (simple) Directs all keyboard input to the foreground job (until CTRL/B). (FRUN) () (simple) Loads and starts execution of the foreground job. (LOAD) (dd) (simple) Brings the indicated device handler into memory; the handler becomes resident in memory. (UNLOAD)(dd) (simple) Takes the indicated device handler out of memory, reclaiming its memory space; the handler becomes nonresident in memory. (UNLOAD)(F) (simple) Reclaims the memory space used by the foreground job.