2
HugeTLB - Large Page Support in the Linux Kernelshmget的:操作不允许
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#define MB_1 (1024*1024)
#define MB_8 (8*MB_1)
char *a;
int shmid1;
void init_hugetlb_seg()
{
shmid1 = shmget(2, MB_8, SHM_HUGETLB
| IPC_CREAT | SHM_R
| SHM_W);
if (shmid1 < 0) {
perror("shmget");
exit(1);
}
printf("HugeTLB shmid: 0x%x\n", shmid1);
a = shmat(shmid1, 0, 0);
if (a == (char *)-1) {
perror("Shared memory attach failure");
shmctl(shmid1, IPC_RMID, NULL);
exit(2);
}
}
void wr_to_array()
{
int i;
for(i=0 ; i<MB_8 ; i++) {
a[i] = 'A';
}
}
void rd_from_array()
{
int i, count = 0;
for(i=0 ; i<MB_8 ; i++)
if (a[i] == 'A') count++;
if (count==i)
printf("HugeTLB read success :-)\n");
else
printf("HugeTLB read failed :-(\n");
}
int main(int argc, char *argv[])
{
init_hugetlb_seg();
printf("HugeTLB memory segment initialized !\n");
printf("Press any key to write to memory area\n");
getchar();
wr_to_array();
printf("Press any key to rd from memory area\n");
getchar();
rd_from_array();
shmctl(shmid1, IPC_RMID, NULL);
return 0;
}
问题>我没有root权限运行该代码。我应该怎么做才能解决权限问题?
$ gcc hugetlb-array.c -o hugetlb-array -Wall
$ ./hugetlb-array
shmget: Operation not permitted
没有使用SHM_HUGETLB,代码运行正常,没有问题。
$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000002 32768 myid 600 2097152 1
您可以在此处提供更多详细信息吗?我阅读了这个链接,但仍然不知道这个问题。 – q0987
你的意思是我必须先获得root权限才能运行我的代码?不幸的是,我无法获得根权限。 – q0987
是的,你仍然需要root权限(但不是每次运行)。嗯,除了可能如果你输入一个新的IPC命名空间(这需要输入一个新的UID命名空间)?参见'unshare(1)' - 我还没有尝试过这种方法,只是作为一个无权限的chroot/mount替换。 – o11c