?
Solved

shared memory unix c/c++

Posted on 2003-03-31
6
Medium Priority
?
486 Views
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>

using

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).



0
Comment
Question by:substand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
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.

Gary
0
 
LVL 10

Author Comment

by:substand
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?

0
 
LVL 10

Author Comment

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:substand
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?

0
 
LVL 6

Expert Comment

by:GaryFx
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.  

Gary
0
 
LVL 10

Author Comment

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

well, i guess it was a dumb question overall.

0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

762 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