Nnamdi Kohn <nnamdi.kohn@tu-bs.de> wrote:
Thank you, David. I didn’t mention, that I actually used the “/” at start of
the names and no other in the names.
Ok – not what you showed, and not doing that could give exactly the
behaviour you described.
Do you do a shm_unlink() anywhere?
What does an “ls -l /dev/shmem” show after you’ve created the two
shared memory areas in process1?
I’ve included a pair of demo programs that use some shared memory
to communicate between them.
They just use one object – but using two objects should be no
different, just have to make sure you don’t get your names, fds,
and pointers confused.
In reality I named the fds “fd_RxShMem” and “fd_TxShMem”, just to avoid my
own confusion. Is there a piece of code that certainly works for installing
two different ShMem objects between two processes? What else could the
reason be? Might it be that there is just ONE ShMem object possible per
process? Couldn’t be!
No, you can have any number of shared memory objects between processes.
/*
- shmemcreator.c
-
- This module demonstrates shared memory and semaphores
- by creating some shared memory and putting something in it (including
- a semaphore structure). It then posts to the semaphore to tell other
- processes that it is okay to read from the shared memory.
-
- This one is meant to be run in tandem with shmemuser.c.
-
- Run it as: shmemcreator shared_memory_object_name
- Example: shmemcreator wally
-
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
/* shmem.h contains the structure that is overlayed on the shared memory */
#include “shmem.h”
/*
char *progname = “shmemcreator”;
main( int argc, char *argv[] )
{
char *text = “Text by shmemcreator.c”;
int fd;
shmem_t *ptr;
if ( argc != 2 ) {
printf( “use: shmemcreator shared_memory_object_name\n” );
printf( “Example: shmemcreator wally\n” );
exit( EXIT_FAILURE );
}
/* create the shared memory object */
fd = shm_open( argv[1], O_RDWR | O_CREAT, S_IRWXU );
if ( fd == -1 ) {
printf( “%s: error creating the shared memory object ‘%s’: %s\n”,
progname, argv[1], strerror(errno) );
exit( EXIT_FAILURE );
}
/* set the size of the shared memory object */
ftruncate( fd, sizeof(shmem_t) );
/* get a pointer to a piece of the shared memory */
ptr = mmap( 0, sizeof(shmem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
/*
- initialize the semaphore
- The 1 means it can be shared between processes. The 0 is the
- count, 0 meaning sem_wait() calls will block until a sem_post()
- is done.
*/
sem_init( &ptr->semaphore, 1, 0 );
strcpy( ptr->text, text ); /* write to the shared memory */
printf( “%s: Shared memory created and semaphore initialized to 0.\n”
“%s: Wrote text ‘%s’ to shared memory.\n”
“%s: Sleeping for 20 seconds. While this program is sleeping\n”
“%s: run ‘shmemuser %s’.\n”,
progname, progname, ptr->text, progname, progname, argv[1] );
sleep( 20 );
printf( “%s: Woke up. Now posting to the semaphore.\n”, progname );
sem_post( &ptr->semaphore );
close( fd );
munmap( ptr, sizeof(shmem_t) );
shm_unlink( argv[1] );
}
/*
- shmemuser.c
-
- This module demonstrates shared memory and semaphores
- by opening some shared memory, waiting on a semaphore (whose
- semaphore structure is in the shared memory) and then printing out
- what it finds in the shared memory.
-
- This one is meant to be run in tandem with shmemcreator.c.
-
- Run it as: shmemuser shared_memory_object_name
- Example: shmemuser wally
-
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
/* shmem.h contains the structure that is overlayed on the shared memory */
#include “shmem.h”
/*
char *progname = “shmemuser”;
main( int argc, char *argv[] )
{
int fd;
shmem_t *ptr;
if ( argc != 2 ) {
printf( “use: shmemuser shared_memory_object_name\n” );
printf( “Example: shmemuser wally\n” );
exit( EXIT_FAILURE );
}
/* open the shared memory object */
fd = shm_open( argv[1], O_RDWR, S_IRWXU );
if ( fd == -1 ) {
printf( “%s: error opening the shared memory object ‘%s’: %s\n”,
progname, argv[1], strerror(errno) );
exit( EXIT_FAILURE );
}
/* get a pointer to a piece of the shared memory */
ptr = mmap( 0, sizeof(shmem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
/* wait on the semaphore */
printf( “%s: Waiting on the semaphore. Run ‘pidin’. I should be SEM blocked.\n”, progname );
sem_wait( &ptr->semaphore );
printf( “%s: The shared memory contains ‘%s’\n”, progname, ptr->text );
close( fd );
munmap( ptr, sizeof(shmem_t) );
}
-David
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.