共享内存段权限被拒绝
运行其他文件时,我正在使用共享内存段进行共享数据和更改。
我的代码
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
bool eaten = false;
void onInterruptSignal()
{
fprintf(stderr, "\nPellet PID is: %d - Died due to the interrupt !-Eaten : %s\n ", getpid(), eaten ? " True " : "False");
exit(0);
}
void onTerminationSignal()
{
fprintf(stderr, "\nFish PID: %d - Died due to the termination !-Eaten : %s\n ", getpid(), eaten ? " True " : "False");
exit(0);
}
int main(int argc, char *argv[])
{
signal(SIGINT, onInterruptSignal);
signal(SIGTERM, onTerminationSignal);
int sharedMemoryID; // Shared memory ID which will be assigned in it when the shared memory segment create successfully
key_t keyValue = 1337;
char(*sharedDataArray)[10][10];
// Create shared memory segment which return some value. But it returns -1 if it is not able create shared memory segment
if ((sharedMemoryID = shmget(keyValue, sizeof(char[10][10]), IPC_CREAT )) < 0)
{
perror("Error while creating memory segment.\n");
exit(1);
}
// Add the sharedDataArray in Shared memory so that it can be access by others
if ((sharedDataArray = (char(*)[10][10])shmat(sharedMemoryID, NULL,0)) == (char(*)[10][10]) - 1)
{
perror("Error while attching sharedDataArray to shared memory.\n");
exit(1);
}
srand(time(NULL));
int row = rand() % 10;
int col = rand() % 10;
while (*sharedDataArray[row][col] != '#')
{
row = rand() % 10;
col = rand() % 10;
}
*sharedDataArray[row][col] = 0x50; // My terminal doesn't support extended ASCII, so we won't be using 0x50.
while (row < 9)
{
*sharedDataArray[row][col] = '#';
row++;
if (*sharedDataArray[row][col] == 'F')
{
*sharedDataArray[row][col] |= 0x50;
eaten =
true;
sleep(1);
break;
}
*sharedDataArray[row][col] = 0x50;
sleep(1);
}
if (!eaten)
{
*sharedDataArray[row][col] = '#';
}
else if (*sharedDataArray[row][col] == 0x50)
{
*sharedDataArray[row][col] = 'F';
}
sleep(1);
printf("Pellet PID is: %d, X: %d, Y: %d - eaten: %s\n", getpid(), row, col, eaten ? "True" : "False");
exit(0);
}
在附加共享内存时会遇到错误,它说
Error while attching sharedDataArray to shared memory.
: Permission denied
我尝试了所有命令以清除共享内存段,即ipcrm
,然后重新启动也关闭了我的PC。但是,当我再次打开时,它给了我与上面提到的相同的错误。
有人知道我如何摆脱这个错误吗?
编辑
shmget()
通过运行ipcs -m
创建共享内存ID,它显示了创建但没有说服力的SHMID。 我如何授予他们允许?
tia
I am using shared memory segment to shared data and changes when I run other files.
My code
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
bool eaten = false;
void onInterruptSignal()
{
fprintf(stderr, "\nPellet PID is: %d - Died due to the interrupt !-Eaten : %s\n ", getpid(), eaten ? " True " : "False");
exit(0);
}
void onTerminationSignal()
{
fprintf(stderr, "\nFish PID: %d - Died due to the termination !-Eaten : %s\n ", getpid(), eaten ? " True " : "False");
exit(0);
}
int main(int argc, char *argv[])
{
signal(SIGINT, onInterruptSignal);
signal(SIGTERM, onTerminationSignal);
int sharedMemoryID; // Shared memory ID which will be assigned in it when the shared memory segment create successfully
key_t keyValue = 1337;
char(*sharedDataArray)[10][10];
// Create shared memory segment which return some value. But it returns -1 if it is not able create shared memory segment
if ((sharedMemoryID = shmget(keyValue, sizeof(char[10][10]), IPC_CREAT )) < 0)
{
perror("Error while creating memory segment.\n");
exit(1);
}
// Add the sharedDataArray in Shared memory so that it can be access by others
if ((sharedDataArray = (char(*)[10][10])shmat(sharedMemoryID, NULL,0)) == (char(*)[10][10]) - 1)
{
perror("Error while attching sharedDataArray to shared memory.\n");
exit(1);
}
srand(time(NULL));
int row = rand() % 10;
int col = rand() % 10;
while (*sharedDataArray[row][col] != '#')
{
row = rand() % 10;
col = rand() % 10;
}
*sharedDataArray[row][col] = 0x50; // My terminal doesn't support extended ASCII, so we won't be using 0x50.
while (row < 9)
{
*sharedDataArray[row][col] = '#';
row++;
if (*sharedDataArray[row][col] == 'F')
{
*sharedDataArray[row][col] |= 0x50;
eaten =
true;
sleep(1);
break;
}
*sharedDataArray[row][col] = 0x50;
sleep(1);
}
if (!eaten)
{
*sharedDataArray[row][col] = '#';
}
else if (*sharedDataArray[row][col] == 0x50)
{
*sharedDataArray[row][col] = 'F';
}
sleep(1);
printf("Pellet PID is: %d, X: %d, Y: %d - eaten: %s\n", getpid(), row, col, eaten ? "True" : "False");
exit(0);
}
I am getting error on while attached the shared memory it says
Error while attching sharedDataArray to shared memory.
: Permission denied
I had try all commands to clear shared memory segment which is ipcrm
and then restart also shutdown my PC. But when i open again it gives me the same error as I mentioned above.
Anyone know how I get rid off this error please ?
EDITED
shmget()
create a shared memory id, by running ipcs -m
it shows the shmid which is created but it does not have persmissions.
How I grant them permit?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的程序创建共享内存对象,则创建时的权限设置为 0,即根本没有权限。例如,请参阅 https:// 中的
shmflg
文档pubs.opengroup.org/onlinepubs/9699919799/functions/shmget.html您可能需要类似的内容
Your code might have run without error before如果已经存在具有足够权限的共享内存对象。
If your program creates the shared memory object, it is created with permissions set to to 0, i.e. no permissions at all. See e.g. the documentation of
shmflg
in https://pubs.opengroup.org/onlinepubs/9699919799/functions/shmget.htmlYou might need something like
Your code might have run without error before if there was already a shared memory object with sufficient permissions.