public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: second call to mmap() results in error
Date: Thu, 30 Jan 2014 12:46:00 -0000	[thread overview]
Message-ID: <20140130124558.GA2821@calimero.vinschen.de> (raw)
In-Reply-To: <001801cf1db6$66c47c40$344d74c0$@lbmsys.com>

[-- Attachment #1: Type: text/plain, Size: 2676 bytes --]

On Jan 30 07:25, Steven Bardwell wrote:
> > > On 29/01/2014 19:12, Corinna Vinschen wrote:
> > > >On Jan 29 09:00, Steven Bardwell wrote:
> > > >>My application needs several areas of shared memory, and I am getting
> > an
> > > >>error ("No such device") on the second call to mmap(). The first call
> > works
> > > >>fine.
> > 
> > Sorry guys, but it still works fine for me.  I tried your testcase on W7
> > 32, W7 64 in 32 and 64 bit, and on Windows 8.1 64 in 32 and 64 bit.  I
> > tried it with Cygwin 1.7.27 and with the latest snapshot.  I'm always
> > getting the output "Shared memory initialized" and no error at all.
> > 
> > 
> > Any chance one of you guys could debug this further, by stepping through
> > the Cygwin mmap64 function, preferredly using the latest snapshot or,
> > a self-built Cygwin DLL from?
> > 
> > 
> > Corinna
> 
> I reinstalled Cygwin, rebooted and the error persisted. Running 'gdb' and
> stepping through the program showed that the call to mmap() fails for /block1
> also -- it is returning an invalid address. This simplification of the program
> shows that error on my machine ('Bus error (core dumped)' ) occurs
> when it tries to do the memcpy() to the mapped address.

Try this:

> #include <stdio.h>
> #include <stdlib.h>

  #include <unistd.h>

> #include <string.h>
> #include <sys/types.h>
> #include <sys/errno.h>
> #include <fcntl.h>
> #include <sys/mman.h>
> 
> int main()
> {
>   int shm_fd1;
>   char *mmap1;
>   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);
>   }
>   ftruncate(shm_fd1, 524304);
>   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);
>   }
>   memcpy(mmap1, "ABCDEF\0", 7);
>   fprintf(stdout, mmap1);
> 
>   fprintf(stdout, "Shared memory initialized\n");
>   exit(0);
> }

The reason is that ftruncate is defined with the second argument being
off_t, which is 8 byte.  524304 is an int (4 byte) only, though.  Since
ftruncate is declared in unistd.h, but you didn't include unistd.h, the
2nd parameter to ftruncate is auto-propagated to int, which results in
an invalid new file length, and which makes ftruncate fail.  Since you
missed to check ftruncate's return value... you get the idea.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2014-01-30 12:46 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-29 14:01 Steven Bardwell
2014-01-29 18:12 ` Corinna Vinschen
2014-01-29 19:08   ` Steven Bardwell
2014-01-29 19:33   ` Marco Atzeri
2014-01-30  9:58     ` Corinna Vinschen
2014-01-30 12:25       ` Steven Bardwell
2014-01-30 12:46         ` Corinna Vinschen [this message]
2014-01-30 13:20           ` Steven Bardwell
2014-01-30 17:01             ` Corinna Vinschen
2014-01-30 17:24               ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-01-30 18:30                 ` Larry Hall (Cygwin)
2014-01-30 18:36                   ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-01-30 18:40                     ` Christopher Faylor
2014-01-30 18:44                       ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-01-30 18:49                         ` Larry Hall (Cygwin)
2014-01-30 18:52                           ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-01-30 19:01                             ` Larry Hall (Cygwin)
2014-01-30 19:04                               ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-01-30 19:30                                 ` Larry Hall (Cygwin)
2014-01-30 19:46                                   ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-01-30 20:19                                     ` Larry Hall (Cygwin)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140130124558.GA2821@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).