shared memory unix c/c++

Posted on 2003-03-31
Medium Priority
Last Modified: 2013-12-26
Here's what I know about shared memory (programming) and what i have in my program so far.  most of this was provided by my professor.

i need to include these header files:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>


int shmget (long key, int nbytes, int flags)

will get me nbytes bytes of shared memory and returns a shared memory id:
so i want 2 shared variables of 8 byte each in my program i used:

int shmid = shmget(key, 8, 0666 | IPC_CREAT);
int shmid2 = shmget(key, 8, 0666 | IPC_CREAT);

now, using

char *shmat (int shmid, int address, int flags)

It "attaches the shared memory segment to an address space"

so in my program I have:

char *variable1 = shmat(shmid, 0, 0);
char *variable2 = shmat(shmid2, 0, 0);

however, when compiling, it says void* cannot be used to initilize type char*.  
also, i tried a normal assignment statement, and it tells me void* cannot be assigned to char*.

However, my professor said otherwise.

Can someone help me with what I'm trying to do?

Basically I want to share 2 8 byte strings among 4 processes (3 of which are fork()ed from the main process).

Question by:substand
  • 4
  • 2

Accepted Solution

GaryFx earned 500 total points
ID: 8241494
This isn't very different from the problem of assigning the result of malloc to a variable.  You need to explicitly cast the result to be the appropriate type (one of the few places such casts are appropriate).

In C, you would do
  char *variable1 = (char*)shmat(shmid,0,0);

In C++, though the above would work, most people prefer:

char *variable1 = reinterpret_cast<char*>(shmat(shmid,0,0));

Having said that, I think your code is still wrong.  The two calls to shmget use the same key, and therefore return the same segment.  It makes more sense to me to declare one shared segment containing both char arrays.

LVL 10

Author Comment

ID: 8241655
ok.  wow. i feel really stupid for not thinking of casting it.  i don't know why it never occured to me.

anyway, since that was so easy, and you brought up the other point, about using the same key:

i figured it wouldn't work like that, but i posted it that way in hopes someone would notice it and let me know what else to do.  I figured I should just get 16 bytes allocated using shmget.  

however, i ran into the problem: how do i then differentiate between the 8 bytes i need for my first variable and the 8 bytes i need for my second variable?

LVL 10

Author Comment

ID: 8241698
i guess i could just use the address shmat gives me + 8?

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 10

Author Comment

ID: 8241714
since this question turned out to be alot easier than 125 points, would you mind answering:

how can i access this shared memory through another one of the forked processes?


Expert Comment

ID: 8242517
If you got the shared memory before forking, then (at least on Linux) the child process inherits access to the shared memory, so you don't need to do anything (other than to synchronize on it).

If you got the shared memory after forking, then the way it's shared is by using the same key in the call on shmget.  You should generate the key with ftok (and to be pedantic, declare it to be of type key_t, not int).

And yes, you can use the address +8.  

LVL 10

Author Comment

ID: 8242867
ok, i assumed that if i declared it before forking it would work.

well, i guess it was a dumb question overall.


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses
Course of the Month6 days, 16 hours left to enroll

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question