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: second call to mmap() results in error


> So if the question is why do you need to include ftruncate() in the code,
> take a look here:

No, that was not the question.  W/o ftruncate(), _referencing_ the memory
becomes a problem (and the original code did not do that, but simply mmap'ed
two files).

> > Adding the <unistd.h> fixes the problem in all its iterations

OP privately wrote:

> The problem is also fixed by correct type-casting in the calls to mmap() ...
> 
>  mmap1 = mmap(NULL, (size_t) 524304, PROT_WRITE | PROT_READ, MAP_SHARED,
> shm_fd1, 0);
> 
> The address returned from the call to mmap() -- with or without the
> ftruncate() -- was bad without the (size_t) specification.

(note "with or without ftruncate()")

And I ask again (last time, I promise), how on Earth adding <unistd.h> to
the code below would make the code suddenly working?  (Like I said before,
that code worked for me, with -Wall showing nothing, from the very beginning.)

Anton Lavrentiev
Contractor NIH/NLM/NCBI


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <sys/mman.h>

int main()
{
        int shm_fd1, shm_fd2;         
        char *mmap1, *mmap2;
 /*   get fd for each block of memory  */
  shm_fd1 = shm_open("/block1", O_CREAT | O_RDWR, 0666);
  if (shm_fd1 == -1) {
                fprintf(stderr, "Couldn't get fd for block1 (%s)\n",
strerror(errno));
                exit(1);
  }
  shm_fd2 = shm_open("/block2", O_CREAT | O_RDWR, 0666);
  if (shm_fd2 == -1) {
                fprintf(stderr, "Couldn't get fd for /UNI_queue (%s)\n",
strerror(errno));
                exit(1);
  }
  /*  map each block  */
  mmap1 = mmap(NULL, 524304, PROT_WRITE | PROT_READ, MAP_SHARED, shm_fd1,
0);
        if (mmap1 == (char *)-1) {
                fprintf(stderr, "Couldn't map memory for /block1 (%s)\n",
strerror(errno));
                exit(1);
        }
  mmap2 = mmap(NULL, 524304, PROT_WRITE | PROT_READ, MAP_SHARED, shm_fd2,
0);
        if (mmap2 == (char *)-1) {
                fprintf(stderr, "Couldn't map memory for /block2 (%s)\n",
strerror(errno));
                exit(1);
        }
        fprintf(stdout, "Shared memory initialized\n");
        exit(0);
}



--
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]