UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-11-17 03:26 PM

lvasilis22
Member
Registered: 2008-11-17
Posts: 8

Re: Shared memory segment

I have created a process (named D) which creates 3 child processes (named A,B,C) and i want to create a shared memory segment between every child process and the patern process. I want my processes to communicate like this:

D <--> A
D <--> B
D <--> C

So, should i have to add three shmget() commands in my code with different keys?
For example, should i do something like this:

shmid = shmget(key1, 1024, 0644 | IPC_CREAT);
shmid = shmget(key2, 1024, 0644 | IPC_CREAT);
shmid = shmget(key3, 1024, 0644 | IPC_CREAT);

And if this is correct, will i have to put different keys? (something like this: key1=4321, key2=4356, key3=5678)?

In addition, shall i give random numbers to every key in shmget or i have to do something to get every key. For example, if i want to create a shared memory segment is correct to put as a key a number that i will chosse at random or i must use something like ftok()?

Offline

#2 2008-11-17 08:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Shared memory segment

A better option than using the old SysV shm*() stuff at all would be to use the POSIX
shm_open() + mmap() instead...  Then, you can use a pseudo "file name" to distinguish
each shared memory segment...  (Eg: "/yourappname.<child_pid>"...)

Alternatively, consider using a Unix domain socketpair() for communication between
the parent and children...  That's often a simpler approach, and avoids any synchronization
issues (ie: the other process knowing when it's safe to read/write the shared memory)...

Offline

#3 2008-11-18 09:45 PM

lvasilis22
Member
Registered: 2008-11-17
Posts: 8

Re: Shared memory segment

Offline

Board footer

Powered by FluxBB