0

I want to communicate between two docker containers using shared memory. In both containers, a simple C program is running.

I tried this example and it works very well: Shared Memory with Docker containers (docker version 1.4.1)

However, when I create a new shared memory area, the Shared-Memory-ID is always "0". I already tried to using a key generated via ftok() and also using the key IPC_PRIVATE:

key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, IPC_CREAT | 0666);

printf("%d \n", shmid);

void *shmdata = shmat(shmid, NULL, 0);

I expect that the ID won't always be 0 and that it will be different when a different key is used.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63

2 Answers2

0

I expect that the ID won't always be 0 and that it will be different when a different key is used.

The problem is that your expectation is unfounded. The ID returned by shmget() is a local identifier within your program, similar in nature to a file handle. Sticking with the file analogy, it is the key that plays the role of file name -- the global identifier. The specifications say only that valid shared-memory IDs are non-negative, but it is natural for them to be numbered from zero.

If you open a second, distinct shared-memory segment at the same time then you should see a different shared-memory ID assigned to it. Probably 1.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

I was facing similar kind of issue and got fixed by removing ftok() function for creating key. Instead I manually gave value as key.

Eg :

key_t key = ftok("shmfile", 65); ( Old)

key_t key = 1234; ( Modified )

This fixed my issue :)