By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,125 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,125 IT Pros & Developers. It's quick & easy.

HELP!!! Shared Memory...

P: 44
Hi everybody!

In the file "server.c" i created a shared memory segment of the size of a 1024 byte string and then i attached a string to the segment, just like this:

int key = 12345;
char *message = "hello_world";

if((shmid = shmget(key, sizeof(char)*1024, IPC_CREAT | 0666)) < 0){
perror("Error in shmget()!");
exit(1);
}

if((message = shmat(shmid, NULL, 0)) ==(char *) -1){
perror("Error in shmat!");
exit(1);
}



Then in the file "client.c" i try to get the message sent by the server.c trough the shared memory:


int key = 12345;
char *msg, *message;

if((shmid = shmget(key, sizeof(char)*1024, 0666)) < 0){
perror("Error in shmget!");
exit(1);
}

if ((msg = shmat(shmid, NULL, 0)) == (char*) -1) {
perror("Error in shmat!");
exit(1);
}

...

//after waiting for the server to put the message into the segment, it does:

message = msg;

printf("MESSAGE: %s\n", message);
fflush(stdout);



I dont get any compiler errors, nor runtime errors (no segmentation faults), but the printing result is always empty, void. I really dont understand the reason why that happens! Ive tried a bunch of stuff like mallocs, but nothing worked. Id be most thankfull for any help

Best regards
Dec 22 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. int key = 12345;
  2. char *message = "hello_world";
  3.  
  4. if((shmid = shmget(key, sizeof(char)*1024, IPC_CREAT | 0666)) < 0){
  5.         perror("Error in shmget()!");
  6.         exit(1);
  7. }
  8.  
  9. if((message = shmat(shmid, NULL, 0)) ==(char *) -1){
  10.         perror("Error in shmat!");
  11.         exit(1);
  12. }
  13.  
Not that I know anything about *nix shared memory but in this section of code where you call shmat you over write the pointer message that was pointing to the string constant "hello_world" with the pointer to the shared memory segment. You then don't copy anything into the shared memory segment, so when your client reads it there is nothing there. I suggest you try

Expand|Select|Wrap|Line Numbers
  1. int key = 12345;
  2. const char const *message = "hello_world";
  3. char *pshm;
  4.  
  5.  
  6. if((shmid = shmget(key, sizeof(char)*1024, IPC_CREAT | 0666)) < 0){
  7.         perror("Error in shmget()!");
  8.         exit(1);
  9. }
  10.  
  11. if((pshm = shmat(shmid, NULL, 0)) ==(char *) -1){
  12.         perror("Error in shmat!");
  13.         exit(1);
  14. }
  15.  
  16. strcpy(pshm, message);
  17.  
Dec 22 '06 #2

Expert 100+
P: 1,510
take a look at the example code on
http://www.cs.cf.ac.uk/Dave/C/node27.html

works on Fedora 6 OK
Dec 22 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.