HP Fortran Programmer's Guide (September 2007)
Controlling data storage
Sharing data among programs
Chapter 3110
The $HP$ SHARED_COMMON directive 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 -m command 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.
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.
! when alarm is set by wake_up.f90, this program breaks out
! of the DO loop, prints message (which wake_up.f90 has
! written to), and exits.
COMMON /shared_data/ alarm, message
alarm = .FALSE.
! Wait for alarm to be set...
DO WHILE (alarm .EQ. .FALSE.)
! sleep(1) is an HP-UX system call that suspends a process
! for the number of seconds specified by the argument.
! The %VAL function tells Fortran that sleep expects its
! argument to be passed by value.
CALL sleep(%VAL(1))
END DO
! Message from wake.f90:
PRINT *, message