Pro-Procrastinator
Pro-Procrastinator

Reputation: 11

Shared memory segment Permission denied

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

Upvotes: 0

Views: 1223

Answers (1)

Bodo
Bodo

Reputation: 9845

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.

Upvotes: 1

Related Questions