共享内存段权限被拒绝

发布于 2025-01-21 02:52:46 字数 3080 浏览 0 评论 0原文

运行其他文件时,我正在使用共享内存段进行共享数据和更改。

我的代码

#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。 我如何授予他们允许?

ipcs -m

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?

IPCS -m

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2025-01-28 02:52:46

如果您的程序创建共享内存对象,则创建时的权限设置为 0,即根本没有权限。例如,请参阅 https:// 中的 shmflg 文档pubs.opengroup.org/onlinepubs/9699919799/functions/shmget.html

您可能需要类似的内容

#include <sys/stat.h> // for S_IR*** & S_IW*** macros

shmget(keyValue, sizeof(char[10][10]), IPC_CREAT | S_IRUSR | S_IWUSR);

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.html

You might need something like

#include <sys/stat.h> // for S_IR*** & S_IW*** macros

shmget(keyValue, sizeof(char[10][10]), IPC_CREAT | S_IRUSR | S_IWUSR);

Your code might have run without error before if there was already a shared memory object with sufficient permissions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文