1

Is it not possible to create two IPC shared memory segments from one process?

I am trying to create two shared memory from a single process one for sharing data with seperate process and other for shared with its child process. I am using shmget, i also tried to get the shared memory information in the system with ipcs. Output of which is something like this:

------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x0beefbaf 0 root 666 225544 4 0x00000000 98305 root 666 4 0 

Above output showed that two shared segment do get created, but shmget return only 0 as its output. Hence one shared memory segment is getting attached twice.

What is the problem with shmget or it is not possible to create two shared memory from one process.

2 Answers 2

1

You're probably using the same key for the shared memory segment - if you want more than one shared memory segments then you need to use different keys. See the man page for ftok for the standard way of obtaining a key.

There are system level limits in place to prevent the use of too much memory - kernel.shmmax and the related sysctl properties.

This is a very simple example app that does exactly what the question asks:

#include <sys/shm.h> #include <stdio.h> #include <fcntl.h> #include <stdlib.h> int main(int argc, char **argv) { key_t key1; key_t key2; if (-1 != open("/tmp/foo", O_CREAT, 0777)) { key1 = ftok("/tmp/foo", 0); } else { perror("open"); exit(1); } if (-1 != open("/tmp/foo2", O_CREAT, 0777)) { key2 = ftok("/tmp/foo2", 0); } else { perror("open"); exit(1); } printf("%x %x\n", key1, key2); int id1 = shmget(key1, 0x1000, IPC_CREAT | SHM_R | SHM_W); int id2 = shmget(key2, 0x2000, IPC_CREAT | SHM_R | SHM_W); printf("%x %x\n", id1, id2); void *addr1 = shmat(id1, 0, 0); if (addr1 == (void *)-1) perror("shmat1"); void *addr2 = shmat(id2, 0, 0); if (addr2 == (void *)-1) perror("shmat2"); printf("%p %p\n", addr1, addr2); shmctl(id1, IPC_RMID, NULL); shmctl(id2, IPC_RMID, NULL); } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply, but i can see the 2 shared memory segment with ipcs. Shared memory id is 0 and a number which is greater that of /proc/sys/kernel/shmmni (4096). I don't know why the second shared memory is getting a number bigger than 4096 and even after creating both segement it is returning the same id for both ie, 0 which is the ID for the first segment. By the way the requirement of shared memory is quite lower that shmmax and shmall.
When you create/access a shared memory segment the first parameter is a key. If you use the same value for the key, then you will access the same shared memory segment. The ID has nothing to do with the limit on the number of segments - it's for the number of concurrent segments. The reason for using ftok is that it allows the unique specification of a key based on an existing file - the original algorithm used the inode number as a seed for the key.
Yes Petesh, I have used two seperate key for two shared memory configuration, as of both the calls to shmget is creating different shared memory segment, but returning the same id.
Sorry I have found the problem and rectified that. Thanks for your precious time and helping hand.
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 :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.