简体   繁体   中英

How to give starting address to attach to that in shmat() shared memory segment?

All headers are included as per requirement. This piece of code below is working but the problem is with shmat(seg_id,NULL,0) . NULL in the 2nd argument means the Operating system will take care of the location on Users' behalf . But we can give our own memory location but how ? I don't know , Please help . (OS - Ubuntu 11.04 ,Compiler gcc 4.5.2) I tried man shmat but didn't understand it completely

typedef struct {
         int id;
} emp;

int main() {
         emp *e;
         int seg_id;
         const int size=sizeof(emp);
         seg_id=shmget( IPC_PRIVATE, size, S_IRUSR | S_IWUSR); 
         pid_t pid=fork();
         if(pid<0) {
                 printf("Error");
                 return -1;
         }
         else if(pid == 0) {
                e=(emp *)shmat(seg_id,NULL,0);     
                 e->id=123;
                 shmdt(e);                       
         }
         else {
                 wait(NULL);
                 e=(emp *)shmat(seg_id,NULL,0);  
                 printf("\nEmp Id : %d\n\n",e->id);
                 shmdt(e);                          
                 shmctl(seg_id, IPC_RMID, NULL);  
         }
         return 0;
}

I tried this also to get our own address for shmget() for 4K page alignment

emp *e;
void **p;
posix_memalign(&p,4096,4096); // alignment as of pagesize 4K 
e=p; // get the first address of the aligned memory 
free(p); // free the allocated memory

then used this as shmat(seg_id,e,0); // thinking that e will be the address that I want. But it's giving segmentation fault

Or, Is there a problem with the 3rd argument also ? Any help or suggestion will be greatly appreciated

I'm not sure what you're trying to do here with posix_memalign() . You are calling that function to allocate a block of memory from the heap and then trying to use the same address that was returned by posix_memalign() as the location to map the shared memory segment. That address is obviously not available since it's part of the heap!

I see that you free the block returned by posix_memalign() before calling shmat() , but freeing a block of memory from the heap doesn't (in general) cause the heap to shrink, so the address is in fact still part of the heap.

If you must choose your own address for shmat() , you should choose an address that is far from anything else in your address space, to avoid conflicting with your heap or any other mapping, and to avoid having the heap run up against your mapping as it grows during the life of your process. Determining such an address is inherently unportable.

The only reason I can think of for wanting to choose a fixed address for your mapping is to make sure it's mapped as the same address in several different processes (so that internal pointers can work). If that's what you want, you can let the OS choose the location in the first process and call shmat() with that same address in the second and all other processes. Even that may not work though, because other processes may happen to already have something mapped at the same address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM