![](/img/trans.png)
[英]Shared memory pages between two different independent program in linux and c
[英]How to make a struct stored in two different memory pages in linux kernel?
如果目標是使結構內存不連續,請使用指針並執行kmalloc。
struct first_half {
int a;
int b;
};
struct second_half {
int c;
int d;
};
struct A {
/* the first half */
struct first_half *fh;
/* the second half */
struct second_half *sh;
} ;
fh = (struct first_half *) kmalloc();
sh = (struct second_half *) kmalloc();
您可以通過在結構成員之間添加填充並將其分配在頁面邊界上來實現,例如:
struct A {
int a, b;
char padding[PAGE_SIZE - 2 * sizeof(int)];
int c, d;
};
但是,它只能處理一個大小的頁面(例如4KB),而存在巨大的不同大小的頁面(x86_64上為2MB和1GB)。
或者,如果不需要填充,則以這種方式為該結構分配內存,以使對象跨越頁面邊界。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.