!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
!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
!The shared memory segment is destroyed when this program halts.
END
IMPORTANT In the example above, you must use +U77to access the correct sleep in the Fortran library. If you use +U77, the line above:
CALL sleep (%VAL(1)) should instead read: CALL sleep (1)
nl
PROGRAM main
!This program, wake_up.f90, should be run just after its
!companion, go_to_sleep.f90, starts to execute but in a
!separate process. The $HP$ SHARED_COMMON directive
!enables both programs to share the same memory.
!Directive puts the common block in shared memory.
$SHARED_COMMON KEY=’scb1’ /shared_common/ LOGICAL :: alarm
CHARACTER(LEN=8) :: message
!Declare a named common block for shared memory. It must
!be laid out n exactly the same way in both programs. COMMON /shared_common/ alarm, message
!Write to message, sleep reads it.
message = "I'm up!"
!Set alarm to wake up sleep. alarm = .TRUE.
!The shared memory segment will now be detached.
!However, because go_to_sleep is still running,
!the segment will still be present in memory until
!it stops executing, too.
END
Following are the command lines to compile each program:
$ f90
Run the first program in any process by doing the following:
$ go_to_sleep
In another process, use the following command line to confirm that a shared memory segment has been created for the program (the last in the list is the newly created one):
$ ipcs |
| |
IPC status from /dev/kmem as of | Fri Mar 21 15:55:29 1997 | |
T ID KEY MODE OWNER GROUP |
| |
Shared Memory: |
| |
m 0 | 0x4119c72b | root |
m 1 | 0x4e180002 | root |
m 2 | 0x41187bf4 | root |
m 3 | 0x00000000 | sys |
m 7004 0x43186ea0
Sharing data among programs 73