How to share a fortran common across tasks

Here is an example that demonstrates using IPC shared memory to share common blocks across fortran tasks. Note the address to which the common block is mapped is arbitrary, check to ensure that no library you link with conflicts.

[bdavis@localhost ~]$ cat a.f

       IMPLICIT NONE
       CHARACTER*16 S
       INTEGER*4 I,STAT,KEY,SIZE,ADDR
       DATA KEY  / Z'FFF100' /
       DATA SIZE / Z'00001000' /
       DATA ADDR / Z'40200000' /
       COMMON / GLOBAL01 / S
       CALL X_INCLD(KEY,ADDR,SIZE,STAT)
       IF (STAT.EQ.0) THEN
           PRINT*
           PRINT*
           PRINT* 
           DO I=1,30
                PRINT*,'S from shared memory is ',S
                CALL SLEEP(1)
           ENDDO
       END IF
       END
[bdavis@localhost ~]$ cat b.f

       IMPLICIT NONE
       CHARACTER*16 S
       INTEGER*4 I,STAT,KEY,SIZE,ADDR
       DATA KEY  / Z'FFF100' /
       DATA SIZE / Z'00001000' /
       DATA ADDR / Z'40200000' /
       COMMON / GLOBAL01 / S
       CALL X_INCLD(KEY,ADDR,SIZE,STAT)
       IF (STAT.EQ.0) THEN
           DO I=1,30
                WRITE(S,*)'COUNT IS ',I
                CALL SLEEP(2)
           ENDDO
       END IF
       END
[bdavis@localhost ~]$ cat shared_mem.c

#include 
#include 
#include 
#include 
#include 
// most web browser eat the file names above.
// they are : sys/types.h sys/shm.h string.h stdio.h unistd.h

void
x_incld__ (int *key, int *addr, int *size, int *status)
{
  int shmid;
  if ((shmid = shmget (*key, *size, IPC_CREAT | IPC_EXCL | 0666)) == -1)
    {
      shmid = shmget (*key, *size, 0);
    }

  *status = (int) shmat (shmid, (const void *) *addr, SHM_REMAP);

  if (*status == *addr)
    {
      *status = 0;
    }
  else
    {
      *status = -1;
    }
  return;
}
[bdavis@localhost ~]$ g77 -Wall -g -o a a.f shared_mem.c -Wl,--defsym -Wl,global01_=0x40200000

[bdavis@localhost ~]$ g77 -Wall -g -o b b.f shared_mem.c -Wl,--defsym -Wl,global01_=0x40200000

[bdavis@localhost ~]$ ./a&;./b&

[1] 7234
[2] 7235
[bdavis@localhost ~]$  
 S from shared memory is  COUNT IS  1    
 S from shared memory is  COUNT IS  1    
 S from shared memory is  COUNT IS  2    
 S from shared memory is  COUNT IS  2    
 S from shared memory is  COUNT IS  3    
 S from shared memory is  COUNT IS  3    
 S from shared memory is  COUNT IS  4    
 S from shared memory is  COUNT IS  4    
 S from shared memory is  COUNT IS  5    
 S from shared memory is  COUNT IS  5    
 S from shared memory is  COUNT IS  6    
 S from shared memory is  COUNT IS  6    
 S from shared memory is  COUNT IS  7    
 S from shared memory is  COUNT IS  7    
 S from shared memory is  COUNT IS  8    
 S from shared memory is  COUNT IS  8    
 S from shared memory is  COUNT IS  9    
 S from shared memory is  COUNT IS  9    

usage home