Shared Memory

When trying to access newly created shared memory the program crashes.
For testing I have typed in a code example from QNX 4 Real-time Operating
System by Frank Kolnick ( page 272-273 if you have the text ) so it should
not be a coding problem.
shm_open, ltrunc, mmap all seem to work fine. Mmap always returns the same
pointer ( 0x40100000 ).
The program crashes when I try to access the shared memory ie trying to
write something to that memory location. Here is a code snippet with all
the error checking, includes etc removed.

main ( void )
{
int mem;
char *where;

mem = shm_open(“mem_test”, O_CREAT | O_RDWR, 0777);
ltrunc(mem, 4096, SEEK_SET );
where = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);

strcpy(where,“original string”); // this is the line it crashes on

munmap( where, 4096 );
close (mem);
shm_unlink(where, 4096);
}

I have tried the cc, gcc and qcc and they all do the same thing.
Any help would be appreciated.

Wenham

Hi Wenham,

Have you tried running the application in Watcom Debugger? Where does
it crash?

Erick.


Wenham <burn0051@home.com> wrote:

When trying to access newly created shared memory the program crashes.
For testing I have typed in a code example from QNX 4 Real-time Operating
System by Frank Kolnick ( page 272-273 if you have the text ) so it should
not be a coding problem.
shm_open, ltrunc, mmap all seem to work fine. Mmap always returns the same
pointer ( 0x40100000 ).
The program crashes when I try to access the shared memory ie trying to
write something to that memory location. Here is a code snippet with all
the error checking, includes etc removed.

main ( void )
{
int mem;
char *where;

mem = shm_open(“mem_test”, O_CREAT | O_RDWR, 0777);
ltrunc(mem, 4096, SEEK_SET );
where = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);

strcpy(where,“original string”); // this is the line it crashes on

munmap( where, 4096 );
close (mem);
shm_unlink(where, 4096);
}

I have tried the cc, gcc and qcc and they all do the same thing.
Any help would be appreciated.

Wenham

“Wenham” <burn0051@home.com> wrote in message
news:91to13$d30$1@inn.qnx.com

When trying to access newly created shared memory the program crashes.

For testing I have typed in a code example from QNX 4 Real-time Operating
System by Frank Kolnick ( page 272-273 if you have the text ) so it should
not be a coding problem.



shm_open, ltrunc, mmap all seem to work fine. Mmap always returns the
same
pointer ( 0x40100000 ).

The program crashes when I try to access the shared memory ie trying to
write something to that memory location.

That’s not what I got when I ran the sample. It crash
AFTER main, that would be in exit code of the C lib.

Here is a code snippet with all
the error checking, includes etc removed.


main ( void )
{
int mem;
char *where;

mem = shm_open(“mem_test”, O_CREAT | O_RDWR, 0777);
ltrunc(mem, 4096, SEEK_SET );
where = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);

strcpy(where,“original string”); // this is the line it crashes on

munmap( where, 4096 );
close (mem);

this is what is causing the problem, the compilier should have spit out
warning (wrong prototypes). shm_unlink takes the name of the share mem as
argument.
I changed it to shm_unlink(“mem_test”) and the sample work fine.

shm_unlink(where, 4096); "
}

I have tried the cc, gcc and qcc and they all do the same thing.

qcc??? Are ya sure this was on QNX4??

Any help would be appreciated.

Wenham

Previously, Wenham wrote in qdn.public.qnx4.devtools:

When trying to access newly created shared memory the program crashes.
For testing I have typed in a code example from QNX 4 Real-time Operating
System by Frank Kolnick ( page 272-273 if you have the text ) so it should
not be a coding problem.
shm_open, ltrunc, mmap all seem to work fine. Mmap always returns the same
pointer ( 0x40100000 ).
The program crashes when I try to access the shared memory ie trying to
write something to that memory location. Here is a code snippet with all
the error checking, includes etc removed.

main ( void )
{
int mem;
char *where;

mem = shm_open(“mem_test”, O_CREAT | O_RDWR, 0777);
ltrunc(mem, 4096, SEEK_SET );
where = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);

strcpy(where,“original string”); // this is the line it crashes on

munmap( where, 4096 );
close (mem);
shm_unlink(where, 4096);
}

I have tried the cc, gcc and qcc and they all do the same thing.
Any help would be appreciated.

  1. You should test the return code from shm_open, as it might have
    failed.

  2. You should test the return code from mmap, as it might have
    failed.

  3. Your mmap probably failed, returned NULL, and you passed NULL to
    strcpy, which caused a segmentation fault. Are you running this
    program as ROOT? You will need to.

Andrew

The code below, with appropriate includes added will not
compile on my system. The following works fine.

#include <libc.h>
#include <fcntl.h>
#include <sys/mman.h>
main ( void )
{
int mem;
char *where;

mem = shm_open(“mem_test”, O_CREAT | O_RDWR, 0777);
ltrunc(mem, 4096, SEEK_SET );
where = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);

strcpy(where,“original string”); // this is the line it crashes on

munmap( where, 4096 );
close (mem);
shm_unlink(“mem_test”);
}


main ( void )
{
int mem;
char *where;

mem = shm_open(“mem_test”, O_CREAT | O_RDWR, 0777);
ltrunc(mem, 4096, SEEK_SET );
where = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);

strcpy(where,“original string”); // this is the line it crashes on

munmap( where, 4096 );
close (mem);
shm_unlink(where, 4096);
}

Mitchell Schoenbrun --------- maschoen@pobox.com