Compaq Fortran
User Manual for
OpenVMS Alpha Systems


Previous Contents Index


Chapter 13
Interprocess Communication

This chapter contains information on how to exchange and share data among local and remote processes.

Local processes involve a single OpenVMS Alpha processor, and remote processes involve separate processors that are interconnected by means of DECnettm.

The following topics are discussed:

13.1 Compaq Fortran Program Section Usage

You may need to change program section attributes to allow shared access to an installed shareable image.

The storage required by a Compaq Fortran program unit is allocated in contiguous areas called program sections (PSECTs). The Compaq Fortran compiler implicitly declares these PSECTs:

Each common block you declare causes allocation of a PSECT with the same name as the common block. (The unnamed common block PSECT is named $BLANK.) Memory allocation and sharing are controlled by the linker according to the attributes of each PSECT; PSECT names and attributes are listed in Table 13-1.

Each procedure in your program is named according to the name specified in the PROGRAM, BLOCK DATA, FUNCTION, or SUBROUTINE statement used in creating the object module. The defaults applied to PROGRAM and BLOCK DATA statements are source-file-name$MAIN and source-file-name$DATA, respectively.

Table 13-1 PSECT Names and Attributes
PSECT Name Use Attributes
$CODE$ Executable code PIC, CON, REL, LCL, SHR, EXE, NORD, NOWRT, OCTA
$LINK$ Linkage information (procedure descriptors, linkage pairs, and literals) NOPIC, CON, REL, LCL, NOSHR, NOEXE, RD, NOWRT, OCTA
$DATA$ Initialized user local static variables and compiler temporary variables NOPIC, CON, REL, LCL, NOSHR, NOEXE, RD, WRT, OCTA
$BSS$ Uninitialized user local variables and compiler temporary variables NOPIC, CON, REL, LCL, NOSHR, NOEXE, RD, WRT, OCTA
$LITERAL$ Literals used in FORMAT statements NOPIC, CON, REL, LCL, NOSHR, NOEXE, RD, WRT, OCTA
$BLANK Blank common block NOPIC, OVR, REL, GBL, NOSHR, NOEXE, RD, WRT, OCTA
names Named common blocks NOPIC, OVR, REL, GBL, NOSHR, NOEXE, RD, WRT, OCTA

You can use the cDEC$ PSECT (such as !DEC$ PSECT) directive or use a Linker options file to change some of the attributes of a common block.

Table 13-2 describes the meanings of Compaq Fortran PSECT attributes.

Table 13-2 Compaq Fortran PSECT Attributes
Attribute Meaning
PIC/NOPIC Position independent or position dependent code
CON/OVR Concatenated or overlaid
REL/ABS Relocatable or absolute
GBL/LCL Global or local scope
SHR/NOSHR Shareable or nonshareable
EXE/NOEXE Executable or nonexecutable
RD/NORD Readable or nonreadable (reserved by Compaq)
WRT/NOWRT Writable or nonwritable
LONG/QUAD/OCTA Longword, quadword, or octaword alignment

When the linker constructs an executable image, it divides the executable image into sections. Each image section contains PSECTs that have the same attributes. By arranging image sections according to PSECT attributes, the linker is able to control memory allocation. The linker allows you to allocate memory to your own specification by means of commands you include in an options file that is input to the linker.

For More Information:

13.2 Local Processes---Sharing and Exchanging Data

Interprocess communication mechanisms provided for local processes include the following capabilities:

These capabilities are discussed in the sections that follow.

VOLATILE declarations are required when you use certain run-time features of the operating system, including values that can be read or written by methods other than direct assignment, or during a routine call.

If a variable can be accessed using rules in addition to those provided by the standard Fortran 90/95 language, declare the variable as VOLATILE. For example, if a variable in COMMON can change value by means of an OpenVMS AST routine or condition handler, you must declare that common block variable or the entire COMMON block as volatile.

Consider the following uses of variables as candidates for a VOLATILE declaration if asynchronous access might occur:

13.2.1 Sharing Images in Shareable Image Libraries

If you have a routine that is invoked by more than one program, you should consider establishing it as a shareable image and installing it on your system.

Establishing a routine as a shareable image provides the following benefits:

Installing a shareable image as shared (INSTALL command, /SHARED qualifier) can also save memory.

The steps to creating and installing a shareable image are as follows:

  1. Compile the source file containing that routine that you want to establish as a shareable image.
  2. Link the shareable image object file that results from step 1, specifying any object files that contain routines referenced by the shareable image object file.
    The OpenVMS Linker provides a variety of options that you should consider before performing the link operation. For detailed information on shareable images and linker options, see the OpenVMS Linker Utility Manual.
  3. Create a shareable image library using the Library Utility's LIBRARY command. For detailed information on creating shareable image libraries, see the Guide to Creating OpenVMS Modular Procedures.
  4. Install the shareable image (the results of step 3) on your system as a shared image by using the Install Utility's INSTALL command (with the /SHARED qualifier). For detailed information on how to perform this operation, see the OpenVMS Install Utility Manual.

Any programs that access a shareable image must be linked with that image. When performing the link operation, you must specify one of the following items on your LINK command:

The resulting executable image contains the contents of each object module and a pointer to each shareable image.

13.2.2 Sharing Data in Installed Common Areas

Sharing the same data among two or more processes can be done using installed common areas.

Typically, you use an installed common area for interprocess communication or for two or more processes to access the same data simultaneously.

13.2.2.1 Creating and Installing the Shareable Image Common Area

To communicate between processes using a common area, first install the common area as a shareable image:

  1. Create the common area---Write a Compaq Fortran program that declares the variables in the common area and defines the common area. This program should not contain executable code. For example:


      COMMON /WORK_AREA/ WORK_ARRAY(8192) 
      END 
    

    This common area can use the BLOCK DATA statement.
    When compiling the source file that contains the common block declarations, consistently use the /ALIGNMENT and /GRANULARITY qualifiers (see Section 13.2.2.3). For example:


    $ FORTRAN/ALIGN=COMMONS=NATURAL/GRANULARITY=LONGWORD INC_COMMON.F90
    

  2. Make the common area a shareable image---Compile the program containing the common area and use the LINK/SHAREABLE command to create a shareable image containing the common area. You need to specify a Linker options file (shown here as SYS$INPUT to allow typed input) to specify the PSECT attributes of the COMMON block PSECT and include it in the global symbol table:


    $ LINK/SHAREABLE INC_COMMON ,SYS$INPUT/OPTION    SYMBOL_VECTOR=(WORK_AREA=PSECT)    PSECT_ATTR=WORK_AREA,SHR    [Ctrl/Z] 
    

    With Compaq Fortran on OpenVMS Alpha systems, the default PSECT attribute for a common block is NOSHR (see Section 13.1). To use a shared installed common block, you must specify one of the following:


    If the !DEC$ PSECT (same as cDEC$ PSECT) directive specified the SHR attribute, the LINK command is as follows:


    $ LINK/SHAREABLE INC_COMMON ,SYS$INPUT/OPTION            SYMBOL_VECTOR=(WORK_AREA=PSECT)            [Ctrl/Z] 
    

    The source line containing the !DEC$ PSECT directive is as follows:


    !DEC$ PSECT /INC_COMMON/ SHR 
    

  3. Copy the shareable image---Once created, you should copy the shareable image into SYS$SHARE: before it is installed. The file protection of the .EXE file must allow write access for the processes running programs that will access the shareable image (shown for Group access in the following COPY command):


    $ COPY/LOG DISK$:[INCOME.DEV]INC_COMMON.EXE SYS$SHARE:*.*/PROTECTION=G:RWE
    

    If you do not copy the installed shareable image to SYS$SHARE, before running executable images that reference the installed shareable common image, you must define a logical name that specifies the location of that image.

  4. Install the shareable image---Using an account with CMKRNL privilege, invoke the interactive Install Utility. When the INSTALL> prompt appears, type a line containing the following:
    1. The CREATE (or ADD) command
    2. The complete file specification of the shareable image that contains the common area (file type defaults to EXE)
    3. The qualifiers /WRITABLE and /SHARED

    The Install utility installs your shareable image and reissues the INSTALL> prompt. Type EXIT to exit. For example:


    $ INSTALL
    INSTALL> CREATE SYS$SHARE:INC_COMMON/WRITABLE/SHARED
    INSTALL> EXIT
    $
    

    A disk containing an installed image cannot be dismounted until you invoke the Install Utility and type DELETE, followed by the complete file specification of the image.

For More Information:

13.2.2.2 Creating Programs to Access the Shareable Image Common Area

After the common area has been installed as a shareable image, use the following steps to access the data from any executable program:

  1. Include the same variable declarations and common area declarations in the accessing program or programs.
    All common block data declarations must be compatible wherever the common block is referenced.
  2. Compile the program.
    When compiling the program that contains the common block declarations, consistently use the same /ALIGNMENT and /GRANULARITY qualifiers used to compile the common block data declaration program that has been installed as a shareable image (see Section 13.2.2.3).
    For example, assume the two programs named INCOME and REPORT that will access a common block WORK_AREA in the installed shareable image INC_COMMON:


    $ FORTRAN/ALIGN=COMMONS=NATURAL/GRANULARITY=LONGWORD INCOME.F90
    $ FORTRAN/ALIGN=COMMONS=NATURAL/GRANULARITY=LONGWORD REPORT.F90
    

  3. Link the accessing program against the installed common area program.
    You must use an options file to specify the common area program as a shareable image.
    The following are LINK commands:


    $ LINK INCOME, INCOME/OPTION
    $ LINK REPORT, INCOME/OPTION
    

    The linker options file INCOME.OPT contains the following lines:


      INC_COMMON/SHAREABLE 
      PSECT_ATTR=WORK_AREA, SHR 
    

    If a !DEC$ PSECT (cDEC$ PSECT) directive specified the SHR PSECT attribute, the linker options file INCOME.OPT would contain the following line:


      INC_COMMON/SHAREABLE 
    

    The source line containing the !DEC$ PSECT directive would be as follows:


    !DEC$ PSECT /INC_COMMON/ SHR 
    

    For each executable image that references the installed shareable image containing the shared common area, you must specify the SHR PSECT attribute by using either:


    The two programs access the same area of memory through the installed common block (program section name) WORK_AREA in the installed shareable image INC_COMMON.
  4. If the installed image is not located in SYS$SHARE, you must define a logical name that specifies the location of that image. The logical name (in this example INC_COMMON) is the name of the installed image.
  5. Execute the accessing program.
    In the previous series of examples, the two programs INCOME and REPORT access the same area of memory through the installed common block WORK_AREA in the installed shareable image INC_COMMON.

For More Information:

13.2.2.3 Synchronizing Access

If more than one process or thread will write to a shared global section containing COMMON block data, the user program may need to synchronize access to COMMON block variables.

Compile all programs referencing the shared common area with the same value for the /ALIGNMENT and /GRANULARITY qualifiers. For example:


$ FORTRAN/ALIGN=COMMONS=NATURAL /GRANULARITY=LONGWORD INC_COMMON

Using /GRANULARITY=LONGWORD for 4-byte variables or /GRANULARITY=QUADWORD for 8-byte variables ensures that adjacent data is not accidentally effected. To ensure access to 1-byte variables, specify /GRANULARITY=BYTE. Because accessing data items less than four bytes slows run-time performance, you might want to consider synchronizing read and write access to the data on the same node.

Typically, programs accessing shared data use common event flag clusters to synchronize read and write access to the data on the same node. In the simplest case, one event flag in a common event flag cluster might indicate that a program is writing data, and a second event flag in the cluster might indicate that a program is reading data. Before accessing the shared data, a program must examine the common event flag cluster to ensure that accessing the data does not conflict with an operation already in progress.

Other ways of synchronizing access on a single node include the OpenVMS lock manager system services (SYS$ENQ and SYS$DEQ), the hibernate and wake system services (SYS$HIBER and SYS$WAKE), or using Assembler code.

For More Information:

13.2.3 Creating and Using Mailboxes to Pass Information

It is often useful to exchange data between processes, as when synchronizing execution or sending messages.

A mailbox is a record-oriented pseudo I/O device that allows you to pass data from one process to another. Mailboxes are created by the Create Mailbox system service (SYS$CREMBX). The following sections describe how to create mailboxes and how to send and receive data using mailboxes.

13.2.3.1 Creating a Mailbox

SYS$CREMBX creates the mailbox and returns the number of the I/O channel assigned to the mailbox. You must specify a variable for the I/O channel. You should also specify a logical name to be associated with the mailbox. The logical name identifies the mailbox for other processes and for Compaq Fortran I/O statements.

The SYS$CREMBX system service also allows you to specify the message and buffer sizes, the mailbox protection code, and the access mode of the mailbox; however, the default values for these arguments are usually sufficient.

The following segment of code creates a mailbox named MAILBOX. The number of the I/O channel assigned to the mailbox is returned in ICHAN.


  INCLUDE '($SYSSRVNAM)' 
  INTEGER (KIND=2) ICHAN 
  ISTATUS = SYS$CREMBX(,ICHAN,,,,,'MAILBOX') 

Note

Do not use MAIL as the logical name for a mailbox. If you do so, the system will not execute the proper image in response to the OpenVMS command MAIL.

13.2.3.2 Sending and Receiving Data Using Mailboxes

Sending data to and receiving data from a mailbox is like other forms of Compaq Fortran I/O. The mailbox is simply treated as a record-oriented I/O device.

Use Compaq Fortran formatted sequential READ and WRITE statements to send and receive messages. The data transmission is performed synchronously; a program that writes a message to a mailbox waits until the message is read, and a program that reads a message from a mailbox waits until the message is written before it continues transmission. When the writing program closes the mailbox, an end-of-file condition is returned to the reading program.

Do not attempt to write a record of zero length to a mailbox; the program reading the mailbox interprets this record as an end-of-file. Zero-length records are produced by consecutive slashes in FORMAT statements.

The following sample program creates a mailbox assigned with the logical name MAILBOX. The program then performs an open operation specifying the logical name MAILBOX as the file to be opened. It then reads file names from FNAMES.DAT and writes them to the mailbox until all of the records in the file have been transmitted.


     CHARACTER (LEN=64) FILENAME 
     INCLUDE '($SYSSRVNAM)' 
     INTEGER (KIND=2) ICHAN 
     INTEGER (KIND=4) STATUS 
 
     STATUS = SYS$CREMBX(,ICHAN,,,,,'MAILBOX') 
     IF (.NOT. STATUS) GO TO 99 
 
     OPEN (UNIT=9, FILE='MAILBOX', STATUS='NEW',  & 
           CARRIAGECONTROL='LIST', ERR=99) 
 
     OPEN (UNIT=8, FILE='FNAMES.DAT', STATUS='OLD') 
 
 10  READ (8,100,END=98) FILENAME 
     WRITE (9,100) FILENAME 
 
100  FORMAT(A) 
     GO TO 10 
 
 98  CLOSE (UNIT=8) 
     CLOSE (UNIT=9) 
     STOP 
 
 99  WRITE (6,*) 'Mailbox error' 
     STOP 
     END 

The following sample program reads messages from a mailbox that was assigned the logical name MAILBOX when it was created. The messages comprise file names, which the program reads. The program then types the files associated with the file names.


     CHARACTER(LEN=64)  FILNAM 
     CHARACTER(LEN=123)  TEXT 
 
     OPEN (UNIT=1, FILE='MAILBOX', STATUS='OLD') 
  1  READ (1,100,END=12) FILNAM 
100  FORMAT (A) 
     OPEN (UNIT=2, FILE=FILNAM, STATUS='OLD') 
     OPEN (UNIT=3, FILE='SYS$OUTPUT', STATUS='NEW') 
 
  2  READ (2,100,END=10) TEXT 
     WRITE (3,100) TEXT 
     GO TO 2 
 
 10  CLOSE (UNIT=2) 
     CLOSE (UNIT=3) 
     GO TO 1 
 12  END 

For More Information:

13.3 Remote Processes---Sharing and Exchanging Data

If your computer is a node in a DECnet network, you can communicate with other nodes in the network by means of standard Compaq Fortran I/O statements. These statements let you exchange data with a program at the remote computer (task-to-task communication) and access files at the remote computer (resource sharing). There is no apparent difference between these intersystem exchanges and the local interprocess and file access exchanges.

Remote file access and task-to-task communications are discussed separately in the sections that follow.

The system manager at the remote system needs to create the necessary network objects and security controls (such as proxy access). Network file specifications might need to use access control strings, depending on how the remote system access has been implemented.

For More Information:

13.3.1 Remote File Access

To access a file on a remote system, include the remote node name in the file name specification. For example:


  BOSTON::DBA0:[SMITH]TEST.DAT;2 

To make a program independent of the physical location of the files it accesses, you can assign a logical name to the network file specification as shown in the following example:


$ DEFINE INVFILE MIAMI::DR4:[INV]INVENT.DAT 

The logical name INVFILE now refers to the remote file and can be used in the program. For example:


  OPEN (UNIT=10, FILE='INVFILE', STATUS='OLD') 

To process a file on the local network node, reassign the logical name; you do not need to modify the source program.

13.3.2 Network Task-to-Task Communication

Network task-to-task communication allows a program running on one network node to interact with a program running on another network node. This interaction is accomplished with standard Compaq Fortran I/O statements and looks much like an interactive program/user session.

The steps involved in network task-to-task communications are as follows:

  1. Request the network connection. The originating program initiates task-to-task communication. It opens the remote task file with a special file name syntax: the name of the remote task file is preceded with TASK= and surrounded with quotation marks. For example:


      BOSTON::"TASK=UPDATE" 
    

    Unless the remote task file is contained in the default directory for the remote node's DECnet account, you must specify the pertinent account information (a user name and password) as part of the node name:

    BOSTON"username password"::"TASK=UPDATE"


    The form of the remote task file varies, depending on the remote computer's operating system. For OpenVMS systems, this task file is a command file with a file type of COM. The network software submits the command file as a batch job on the remote system.

  2. Complete the network connection. When the remote task starts, it must complete the connection back to the host. On OpenVMS systems, the remote task completes this connection by performing an open operation on the logical name SYS$NET. When opening the remote task file or SYS$NET, specify either FORM='UNFORMATTED' or the combination of FORM='FORMATTED' and CARRIAGECONTROL='NONE'.
  3. Exchange messages. When the connection is made between the two tasks, each program performs I/O using the established link.
    Task-to-task communication is synchronous. This means that when one task performs a read, it waits until the other task performs a write before it continues processing.
  4. Terminate the network connection. To prevent losing data, the program that receives the last message should terminate the network connection using the CLOSE statement. When the network connection is terminated, the cooperating image receives an end-of-file error.

The following is a complete example showing how Compaq Fortran programs can exchange information over a network. The originating program prompts for an integer value and sends the value to the remote program. The remote program then adds one to the value and returns the value to the originating program. It is assumed that the remote operating system is an OpenVMS system.

The originating program on the local node contains the following source code:


     OPEN (UNIT=10, FILE='PARIS::"TASK=REMOTE"', STATUS='OLD', & 
           FORM='UNFORMATTED', ACCESS='SEQUENTIAL', IOSTAT=IOS, ERR=999) 
 
!  Prompt for a number 
     PRINT 101 
101  FORMAT ($,' ENTER A NUMBER: ') 
     ACCEPT *,N 
 
!  Perform the network I/O 
     WRITE (UNIT=10, IOSTAT=IOS, ERR=900) N 
     READ  (UNIT=10, IOSTAT=IOS, ERR=900) N 
 
!  Display the number and process errors 
 
     PRINT 102, N 
102  FORMAT (' The new value is ',I11) 
     GO TO 999 
 
900  PRINT *, 'Unexpected I/O Error Number ', IOS 
999  CLOSE (UNIT=10) 
     END PROGRAM 

The task file REMOTE.COM on the remote node contains the following OpenVMS DCL commands:


$ DEFINE SYS$PRINT NL:               ! Inhibit printing of log 
$ RUN DB0:[NET]REMOTE.EXE            ! Run remote program 
$ PURGE/KEEP=2 REMOTE.LOG            ! Delete old log files 

The remote program PARIS::DB0:[NET]REMOTE.EXE contains the following source code:


    OPEN (UNIT=10, FILE='SYS$NET', FORM='UNFORMATTED', & 
          ACCESS='SEQUENTIAL', STATUS='OLD') 
    READ (UNIT=10) N 
    N = N + 1 
    WRITE (UNIT=10) N 
    CLOSE (UNIT=10) 
    END PROGRAM 

For More Information:

On using DECnet, refer to the DECnet for OpenVMS Networking Manual and DECnet for OpenVMS Guide to Networking.


Previous Next Contents Index