shm_open, mmap and linking...

Hi…

I am trying to do the following:

<…>

SemLen = sizeof(SemaForos);

//…Basically we have to attach an existing shared memory object for
the
//…where the semaphores reside on…
if ( (SemFD = shm_open( NameSemObjPtr, O_RDWR, SHM_MODE) ) == -1 )
{ perror("\nSemCreate::error creating shared memory block for
semaphore::…");
return (ICL_FALSE);
}
if ( (SemMngrPtr = (SemaForos *) mmap(0, SemLen, PROT_READ|PROT_WRITE,
MAP_SHARED, SemFD, 0) )
== MAP_FAILED )
{ printf("\nSemAttach:: mmap() failed::…%s\n",strerror(errno) );
close(SemFD); //…close the file descriptor
return ( ICL_FALSE );
}
//…this address will be different for each process…
printf("\n Map Address for SemPtr is %6.6x\n", SemMngrPtr);

//…increment by one the number of semaphores out there…
SemMngrPtr->NumActiveSemaphores += 1;
//SemLen = SemMngrPtr->Size; //…size of the acutal shared mem

<…>

I compile by: qcc -Vgcc_ntox86 -o main main.C,
execute via: ./main
and get a core dump. :frowning:(

The problem comes from: SemMngrPtr->NumActiveSemaphores += 1;
(i.e. when I comment the above line, all is fine.)
What am I doing wrong?? The same code works perfectly fine in
QNX4.25…

Finally, do I have to explicitly link the *.so libraries??? If so,
how?? Any pointers to the right direction?


I am going to sleep now…

Thanks… Bests.

Miguel

You have to set size of memory block with ftruncate() before mapping it,
or you get SIGBUS. I think you have to do the same thing on QNX4 (but
using ltrunc).

  • igor

Miguel Simon wrote:

Hi…

I am trying to do the following:

SemLen = sizeof(SemaForos);

//…Basically we have to attach an existing shared memory object for
the
//…where the semaphores reside on…
if ( (SemFD = shm_open( NameSemObjPtr, O_RDWR, SHM_MODE) ) == -1 )
{ perror("\nSemCreate::error creating shared memory block for
semaphore::…");
return (ICL_FALSE);
}
if ( (SemMngrPtr = (SemaForos *) mmap(0, SemLen, PROT_READ|PROT_WRITE,
MAP_SHARED, SemFD, 0) )
== MAP_FAILED )
{ printf("\nSemAttach:: mmap() failed::…%s\n",strerror(errno) );
close(SemFD); //…close the file descriptor
return ( ICL_FALSE );
}
//…this address will be different for each process…
printf("\n Map Address for SemPtr is %6.6x\n", SemMngrPtr);

//…increment by one the number of semaphores out there…
SemMngrPtr->NumActiveSemaphores += 1;
//SemLen = SemMngrPtr->Size; //…size of the acutal shared mem

I compile by: qcc -Vgcc_ntox86 -o main main.C,
execute via: ./main
and get a core dump. > :frowning:> (

The problem comes from: SemMngrPtr->NumActiveSemaphores += 1;
(i.e. when I comment the above line, all is fine.)
What am I doing wrong?? The same code works perfectly fine in
QNX4.25…

Finally, do I have to explicitly link the *.so libraries??? If so,
how?? Any pointers to the right direction?

I am going to sleep now…

Thanks… Bests.

Miguel