Cannot share memory (mmap)

Mar 07, 2005 3 Replies

Hi,



I have serious problems sharing memory in my embedded Linux application. I have looked up the Linux documentation, found some code snippets and in addition used it for years in Windows but it will not initialze here. Here is my condensed code:



int init_mmap(void) { unsigned char result; int fd;



fd=fopen("shared_mem.dat", "a+"); shared_mem_prt=mmap(0, 1024, PROT_READ+PROT_WRITE, MAP_SHARED, fd,



0);

result=0; return result; }



The file already exists, has the correct size and does open. The problem is that the mmap always return -1 which means a failure and errno return subsequently 9. I have tried various other open modes and shar modes but each time with the same result.



Can sombody please tell me what I am doing wrong?



I need this to communicate between two different applications. Is shared memory the best way to do it or are there other ways in Linux? (Sorry but my background is windows)



Thanks,



Jens.



What exactly do you want to do ?

What is your operating system ? If it is uClinux, it seems dosen't support PROT_WRITE in MAP_SHARED options.

As I recall, some architectures also require PROT_EXEC with PROT_WRITE due to hardware limitations.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required