This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Shared memory handling for mixed C/FORTRAN program




Thanks for your help, your suggestions have helped me greatly but I'm still stuck. 



>> I am porting to cygwin a program in FORTRAN/C that relies on C
>> routines to create a shared memory region allowing various independent
>> FORTRAN routines to share data. Program compiles and runs ok on Linux
>> with g77/gcc compilers. I am compiling on cygwin 1.7.33-2(0.280/5/3)
>> with gfortran/gcc (4.8.3).
>> 
>> One C routine creates the shared memory section of a user-defined
>> size. This seems to work just fine, although I had to add two lines to
>> the sys/shm.h file:
>> 
>> 
>> #define SHM_R        0400        /* or S_IRUGO from <linux/stat.h> */
>> #define SHM_W        0200        /* or S_IWUGO from <linux/stat.h> */
>
>These are non-standard Linux extensions.  You should use the normal
>permission bits from sys/stat.h, like S_IRUSR, S_IWUSR, etc.


Ok, thank you, changed that.



>> The shm library functions seem to return reasonable info (page size
>> and address, for removal of the shm section).
>> 
>> 
>> Each FORTRAN routine then calls a C routine to find the shared memory,
>> with a C routine returning pointers to two positions in the section
>> intended for different kinds of data:
>> 
>> 
>> 
>> #define PAGESIZE 1024
>
>PAGESIZE on Cygwin is not 1024, and the right value to use for
>XSI SHM is SHMLBA (== 64K on Cygwin)



Setting PAGESIZE to SHMLBA creates problems elsewhere in the program (then PAGESIZE is too big for the program to handle, a problem I have yet to debug) so only unless there is no workaround (please see below) I'd like to avoid trying to use SHMLBA for the time being.

Regardless of the choice of PAGESIZE, only a total of 4kB can be stored sequentially in the shared memory section handed by shmat. If I try to write more than this number of bytes to the shm section the program crashes. 


>> int findshm(char**pptr,  /* Address of the parameter pointer */
>> float**cptr)  /* Address of the data pointer */
>> 
>> ....calls to shm library functions ....
>> 
>> shmaddr =0; p =shmat(shmid,shmaddr,(SHM_R |SHM_W)); *pptr =p; *cptr
>> =(float*)(p +PAGESIZE); return npages;
>> 
>> 
>> The calling FORTRAN code looks like this:
>> 
>> integer pptr,cptr integer npages npages =findshm(pptr,cptr)
>> 
>> Although the total size of the created memory section npages is ok,
>> the amount of memory following cptr is too small on cygwin (but not in
>> Linux) and the program crashes for larger datasets with
>> 
>> Program received signal SIGSEGV:Segmentationfault -invalid memory
>> reference.



>Does shmat actually return a non-NULL value? 

shmat seems to work as desired, the pointers it returns are being passed properly between C and Fortran


> Are you running cygserver? 

I am using the server. 


> Did you check if it works from plain C?  If not, do you have a simple testcase in plain C?

I modified the first C utlity, which sets up the shared memory section, to use the addresses it receives from shmat to try to write as much data as possible into the section. I could read/write to the full shm section from within that utility. 

I also modified a second C routine which looks for the memory section and returns addresses into the shm section. When attempting to read/write within that second routine the program crashes when accessing addresses more than 4 kB from the start of the shm section. 

I found this 
https://www.cygwin.com/ml/cygwin/2009-01/msg00492.html
and links from there, which suggest that this 4kB "barrier" is probably related to cygwin's memory handling. 

If this is in fact a likely source of the problem, is there any way to work around this 4 kB limit while continuing to use the shm library? I can think of using multiple pointers  to separate sections (perhaps as a pointer array), but that amounts to an extensive rewrite. 

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]