Sharing data among programs

If you are designing an application that requires multiple threads of control that share the same data, the design can take either of two forms:

The program makes calls to the threads library:

/usr/lib/libpthread.sl

which creates multiple threads executing in a single process and therefore all sharing the same address space.

which creates multiple threads

The application consists of several programs that run simultaneously in separate processes and that access an HP-UX shared memory segment.

The first approach is beyond the scope of this manual and requires that you have an understanding of how to call the threads library.1 The second approach is described here.

To share data among several HP Fortran programs that are executing simultaneously in separate processes, use the $HP$ SHARED_COMMONdirective. This directive enables you to create a common block that is accessible by HP Fortran programs executing in different processes.

The $HP$ SHARED_COMMONdirective causes the compiler to insert HP-UX system calls to perform shared memory operations. To the programmer, the programs sharing the memory segment appear as though they were program units in the same program, accessing a set of common block variables.

Following are two programs to illustrate how the $HP$ SHARED_COMMONdirective works:

The first program, go_to_sleep.f90, must execute first. Because it executes first, it creates the shared memory segment and then enters a DOloop, where it waits until the second program starts to execute. You can use the ipcs -m command to confirm that a shared memory segment has been created.

When the second program, wakeup.f90, starts to execute, it writes to the shared common block variables, one of which causes go_to_sleep.f90to break out of the DOloop and run to completion.

The $HP$ SHARED_COMMONdirective must appear at the beginning of the specification part of the main program unit of each program sharing the memory segment. Also, the common block specified for sharing must have the same layout in all files in which it is declared.

You can use the ipcs -mcommand both to determine that HP-UX has created a shared memory segment and, after the programs complete execution, to confirm that it has been released.

Specifying the +Oparalleloption causes the compiler to transform eligible loops in an HP Fortran program for parallel execution on HP 9000 systems. For information about compiling for parallel execution, see “Compiling for parallel execution” (page 100).

The following two examples illustrate these concepts.

Example 3-3 go_to_sleep.f90

PROGRAM main

!This program, go_to_sleep.f90, and its companion, wake_up.f90,

!share data in a common block, using the $HP$ SHARED_COMMON

!directive. Execute this program first. After it starts to

!execute, use ipcs(1) to confirm that a shared memory segment

!has been created. In a separate process, run wake.f90.

!When it executes, it assigns to alarm, ending this program. LOGICAL :: alarm

CHARACTER(LEN=8) :: message

!Declare a common block, shared_data, for sharing among

!multiple, simultaneously executing programs. Each program

!that shares the common block must reference it by the same

!key, 'scb1'

!$HP$ SHARED_COMMON KEY=’scb1’ /shared_data/

! Declare a common block with two variables: alarm and message.

72 Controlling data storage

Page 72
Image 72
HP UX Fortran Software manual Sharing data among programs, Usr/lib/libpthread.sl, Which creates multiple threads