Operating System - HP-UX
1819794 Members
3220 Online
109607 Solutions
New Discussion юеВ

second call to shmat in same process

 
pramodsharma
Advisor

second call to shmat in same process

If the calling process is already attached to the shared memory segment, shmat() fails and returns SHM_FAILED regardless of what value is passed in shmaddr.

This is not done in other *NIX platforms.

Is there any mechanism to know this is already attached by someone in the process and if yes how to know the address to which it is attached , so that I can use it.

I really don't want to make shared memory point global now.

#include
#include
#include
#include
extern int errno ;


int main()
{
int id = -1 , id2;
key_t key = ftok("/PRAMODTEST" , 700);
id = shmget(key , 16 , 0600 | IPC_CREAT);

if( -1 != id)
{
void *sharedMem = shmat( id ,NULL ,SHM_RDONLY) ;
if((int)sharedMem == -1 )
{
printf("Attaching failed %d , %s \n" , errno , strerror(errno) );
}
else
{
void *p = shmat(id ,NULL ,0) ;
if((int)p == -1 )
{
printf("Second attach is failed %d , %s \n" , errno , strerror(errno));
}
else
{
printf("Second attach paased\n");
shm_unlink("/PRAMODTEST");
}

shmdt(sharedMem);
}
}
else
printf("shmget failde\n");
}


Thanks in advance.

2 REPLIES 2
Don Morris_1
Honored Contributor

Re: second call to shmat in same process

Yes, that's in the man page and all:

"If the calling process is already attached to the shared memory segment, shmat() fails and returns SHM_FAILED regardless of what value is passed in shmaddr. See exceptions for MPAS processes below."

Quickly skimming over the gory details, this differs from other OS's because HP-UX is a Mostly Global Address Space, they're Mostly (or Completely) Private. And that's because PA doesn't allow (easily) multiple virtual addresses at the same time for the same physical memory (aliasing).

Keep track of things within your own process (as you've considered and which I really think is the best option) or restrict yourself to 11iv2 and higher, IPF only and use MPAS (keeping in mind that using MPAS too much and generating a _lot_ of aliases slows down the system as a whole [because of the collision chains in the translation tables]).
pramodsharma
Advisor

Re: second call to shmat in same process

Closing thread.