0
int shmid;
int* locat;

//create shared memory segment
shmid = shmget(6666, size, 0666);
if (shmid < 0) {
    perror("shmget");
    exit(1);
}

locat = (int *) shmat(shmid, NULL, 0);
if (locat == (int *) -1) {
    perror("shmat");
    exit(1);
}

I am setting up shared memory as such, yet I keep getting this error: shmget: No such file or directory

This code was working fine, not sure why this occurs now.

ohbrobig
  • 939
  • 2
  • 13
  • 34
  • Your shared memory creation has failed and `shmget` returned `-1`. Have a look at http://stackoverflow.com/questions/7495326/understanding-shared-memory-using-c – MrKiwi Dec 02 '16 at 06:58

2 Answers2

1

As the man says

IPC_CREAT

Create a new segment. If this flag is not used, then shmget() will find the segment associated with key and check to see if the user has permission to access the segment.

You have to add IPC_CREAT to your shmget call

shmid = shmget(6666, size, IPC_CREAT | 0666);

You could also use the IPC_EXCL to ensure that the segment is newly created

IPC_EXCL

This flag is used with IPC_CREAT to ensure that this call creates the segment. If the segment already exists, the call fails.

LPs
  • 16,045
  • 8
  • 30
  • 61
0

There are two things :

  1. When you want to initialize a shared memory ( corresponding to a particular key value ) , you have to BITWISE OR the permission number with IPC_CREAT.

Just like

shmget(6666 , size , 0666|IPC_CREAT);
  1. When you want to attach the same segment ( identified by the key value ) to another process , IPC_CREAT is not mandatory as the shared memory has already been created the the logical address space.
Varad Bhatnagar
  • 599
  • 1
  • 7
  • 19