简体   繁体   English

如何在shmat()共享内存段中给出附加起始地址?

[英]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) . 下面的这段代码正在运行,但问题在于shmat(seg_id,NULL,0) NULL in the 2nd argument means the Operating system will take care of the location on Users' behalf . 第二个参数中的NULL表示操作系统将代表用户处理该位置。 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 (OS - Ubuntu 11.04 ,Compiler gcc 4.5.2)我试过man shmat但完全不明白

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 我也试过这个来获取我们自己的shmget()地址,用于4K页面对齐

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); 然后用它作为shmat(seg_id,e,0); // thinking that e will be the address that I want. //认为e将成为我想要的地址。 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() . 我不确定你在这里用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. 您正在调用该函数从堆中分配一块内存,然后尝试使用posix_memalign()返回的相同地址作为映射共享内存段的位置。 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. 我看到你在调用shmat() posix_memalign()之前释放posix_memalign()返回的块,但是从堆中释放一块内存不会(通常)导致堆缩小,所以地址实际上仍然是堆。

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. 如果必须为shmat()选择自己的地址,则应选择与地址空间中的任何其他地址相距甚远的地址,以避免与堆或任何其他映射冲突,并避免堆与映射相遇因为它在你的过程中生长。 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. 如果这是您想要的,您可以让操作系统在第一个进程中选择位置,并在第二个和所有其他进程中使用相同的地址调用shmat() Even that may not work though, because other processes may happen to already have something mapped at the same address. 即使这可能不起作用,因为其他进程可能恰好已经映射到同一地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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