PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8011 Location: Salford, UK
|
Posted: Wed Apr 08, 2009 2:22 pm Post subject: Sharing inter-process memory with FTN95 under Win32 |
|
|
From version 5.30 of FTN95 you can access memory that was created in another process. The keyword SHARENAME is provided as an FTN95 extension to the standard ALLOCATE command in order to create memory in one process that can be accessed from another.
In the following illustration we create two programs that share common memory. The two programs (called shareA and shareB) are listed below. After compiling the programs using FTN95, the operation is as follows...
1. Start up program shareA.
2. Start up program shareB in a separate Command Prompt window.
3. Type a message as input into the instance of shareA.
4. View the same message as it appears as output in instance of shareB
The two programs use a common semaphore in order to synchronise the events.
Here is the code for shareA.f90...
Code: |
include <windows.ins>
integer(2) c
character,pointer::msg*80
allocate(msg, SHARENAME="MyMemory")
print*, "Type a message to send. Do not use spaces..."
read*, msg
call SIGNAL_SEMAPHORE@("MySemaphore")
print*, "Read the message then press any key to terminate"
call get_key@(c)
deallocate(msg)
end
|
Here is the code for shareB.f90...
Code: |
include <windows.ins>
integer(2) c
character,pointer::msg*80
allocate(msg, SHARENAME="MyMemory")
print*, "Waiting for message..."
call WAIT_ON_SEMAPHORE@("MySemaphore")
print*, msg(1:len_trim(msg))
print*, "Press any key to terminate"
call get_key@(c)
end
|
The planned new FTN95 feature for the file-mapping of memory uses the same keyword SHARENAME, but is not functioning as intended and will now be included in the next release of FTN95.
Here is some sample code to illustrate what will be available in the next release. When you run the following program, the relevant file will be created on the first run and there is no output. On the second run, the file now exists and its content is output.
Code: | character,pointer::msg*80
character(*),parameter:: myData = "C:\TechSupport\test.dat"
logical file_exists@, exists
exists = file_exists@(myData)
allocate(msg, SHARENAME=myData)
if(.NOT. exists) then
msg = "Data created!"
else
print*, msg
endif
end |
If the drive letter for the path is lower case then the file is mapped for reading only. A file mapping occurs when the second character of the SHARENAME is a colon, otherwise you get inter-process shared memory without file mapping. |
|