Mail Archives: cygwin/2014/01/30/07:46:17
--hTKW8p8tUZ/8vLMe
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
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 getti=
ng
> > an
> > > >>error ("No such device") on the second call to mmap(). The first ca=
ll
> > works
> > > >>fine.
> >=20
> > 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.
> >=20
> >=20
> > 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?
> >=20
> >=20
> > Corinna
>=20
> I reinstalled Cygwin, rebooted and the error persisted. Running 'gdb' and
> stepping through the program showed that the call to mmap() fails for /bl=
ock1
> also -- it is returning an invalid address. This simplification of the pr=
ogram
> 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>
>=20
> int main()
> {
> int shm_fd1;
> char *mmap1;
> shm_fd1 =3D shm_open("/block1", O_CREAT | O_RDWR, 0666);
> if (shm_fd1 =3D=3D -1) {
> fprintf(stderr, "Couldn't get fd for block1 (%s)\n", strerror(errno));
> exit(1);
> }
> ftruncate(shm_fd1, 524304);
> mmap1 =3D mmap(NULL, 524304, PROT_WRITE | PROT_READ, MAP_SHARED, shm_fd=
1, 0);
> if (mmap1 =3D=3D (char *)-1) {
> fprintf(stderr, "Couldn't map memory for /block1 (%s)\n", strerror(er=
rno));
> exit(1);
> }
> memcpy(mmap1, "ABCDEF\0", 7);
> fprintf(stdout, mmap1);
>=20
> 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
--=20
Corinna Vinschen Please, send mails regarding Cygwin to
Cygwin Maintainer cygwin AT cygwin DOT com
Red Hat
--hTKW8p8tUZ/8vLMe
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQIcBAEBAgAGBQJS6kmGAAoJEPU2Bp2uRE+gEAQP/Rb7FF/efOTAq9vdv82wcQ0w
tQaeHnL0ciP0kcUC/C+eKJeywh3Wt1pYkYhM7Z148o/3o6F/LXv8QCWyb4mY7vNC
WrixwQtrJnsCLJEte53SxKqJgSTuYH7g3F6QAuqnVlcBnumzN2Ng5PpLzpy9Ncx9
Iy+D2sayUhB6GFRBOtOY2/AjplQEitryRguA/YbnaCpkHv8KnSJ5JbYIy1a73jhj
3UGFAgSVs1BsBYyOs7JiPOksm59Znu6HPadlvh1h9LzXQpnzMKuIQWPrg8fENzXJ
k+gRhKT1/CrAT9Ts7DqPIdyRntVHiSR58e9bm1K49UjmunQ8YlthoPJepUj/0FYg
xVABSxQZLeHVv7fNZrmMOfXkM09b0Lh1Lg2G3ZXoMqbjb7DWzI0KnLqDneLdHpwT
FC3fOWCLQ1didZ1NB2s0G4uXIYqpW3eeOE8PvXJhBFbAj5B3tIISXrqy8KAE39w8
lDrNgV3UZBTmO+IVk4Gv4tA0cdW7Bd3726CkhXVzRFBUdCqx71Y0F/4RTYRllJHi
ZHhJf13cPzdDcUeyXbw16C1MgOlU7m7nnw5xBo/k/pv7d8mIMRJAlkDJGCHSUpbk
12u/Q07mF4Rc2p6QKIoPipOoeEqs3M+z2HiFuM6UCQ31ZHHJ3via9WQiOXPvUbF+
QUGeMggB+z5ec6zi30U1
=kIS+
-----END PGP SIGNATURE-----
--hTKW8p8tUZ/8vLMe--
- Raw text -