* Segfault when accessing mmaped memory on Cygwin
@ 2021-01-01 21:11 Thomas Koenig
2021-01-02 22:09 ` Thomas Koenig
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Koenig @ 2021-01-01 21:11 UTC (permalink / raw)
To: cygwin
Hi,
when trying out uf a certain shared memory allocator would
work on Cygwin, I tried out the sample program below (which
works on Linunx, *BSD, AIX and Solaris) and got a suprising
falure with a segmentation fault at the line
*i1 = 42;
mmap() had succeeded.
Is this a known issue, and would it be possible to work
around that?
Best regards
Thomas
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#define NAME "/random_namexxx"
int main()
{
int fd;
long pagesize;
void *p1, *p2;
volatile int *i1, *i2;
size_t size1, size2;
off_t offset;
pagesize = sysconf (_SC_PAGE_SIZE);
if (pagesize == -1)
{
perror ("sysconf failed");
exit (EXIT_FAILURE);
}
fd = shm_open (NAME, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd == -1)
{
perror ("shm_open failed");
exit (EXIT_FAILURE);
}
shm_unlink (NAME);
offset = 0;
size1 = pagesize;
p1 = mmap (NULL, size1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
if (p1 == MAP_FAILED)
{
perror ("mmap 1 failed");
exit (EXIT_FAILURE);
}
printf ("p1 = %p\n", p1);
ftruncate (fd, size1);
i1 = p1;
*i1 = 42;
size2 = 2 * size1;
p2 = mmap (NULL, size2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
if (p2 == MAP_FAILED)
{
perror ("mmap 1 failed");
exit (EXIT_FAILURE);
}
printf ("p2 = %p\n", p2);
i2 = p2;
ftruncate (fd, size2);
printf ("%d\n", *i2);
return 0;
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Segfault when accessing mmaped memory on Cygwin
2021-01-01 21:11 Segfault when accessing mmaped memory on Cygwin Thomas Koenig
@ 2021-01-02 22:09 ` Thomas Koenig
2021-01-02 22:19 ` Steven Bardwell
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Koenig @ 2021-01-02 22:09 UTC (permalink / raw)
To: cygwin
I wrote:
> when trying out uf a certain shared memory allocator would
> work on Cygwin, I tried out the sample program below (which
> works on Linunx, *BSD, AIX and Solaris) and got a suprising
> falure
Actually, the failure wasn't all that unexpected, given that
I had put the ftruncate into the wrong place...
It appears to work now, sorry for the noise.
Best regards
Thomas
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: Segfault when accessing mmaped memory on Cygwin
2021-01-02 22:09 ` Thomas Koenig
@ 2021-01-02 22:19 ` Steven Bardwell
0 siblings, 0 replies; 3+ messages in thread
From: Steven Bardwell @ 2021-01-02 22:19 UTC (permalink / raw)
To: cygwin
Thomas writes:
>> when trying out uf a certain shared memory allocator would work on
>> Cygwin, I tried out the sample program below (which works on Linunx,
>> *BSD, AIX and Solaris) and got a suprising falure
> Actually, the failure wasn't all that unexpected, given that I had put the ftruncate into the wrong place...
> It appears to work now, sorry for the noise.
Where is the proper place for the ftruncate() call?
Steve Bardwell
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-01-02 22:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-01 21:11 Segfault when accessing mmaped memory on Cygwin Thomas Koenig
2021-01-02 22:09 ` Thomas Koenig
2021-01-02 22:19 ` Steven Bardwell
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).