473,508 Members | 3,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP!!! Shared Memory...

44 New Member
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 don´t get any compiler errors, nor runtime errors (no segmentation faults), but the printing result is always empty, void. I really don´t understand the reason why that happens! I´ve tried a bunch of stuff like mallocs, but nothing worked. I´d be most thankfull for any help

Best regards
Dec 22 '06 #1
2 2237
Banfa
9,065 Recognized Expert Moderator Expert
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
horace1
1,510 Recognized Expert Top Contributor
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

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

Similar topics

14
24946
by: S Green | last post by:
Does any one now if a shared memory module exists, written in python for a windows platform. I now one exists for unix? help most appreciated, S Green
0
4357
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
5
4410
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
11
4864
by: Michael Schuler | last post by:
The use of STL in shared memory poses a real problem since (non-smart) pointers are not allowed there. Is there any solution for containers in shared memory using smart pointers? Where can I...
5
7043
by: Jim | last post by:
Hello, I have a broken server that we are going to be moving off to a new server with a new version of DB2 but here is what I have right now: RedHat 7.0 (2.2.24smp) DB2 v6.1.0.40 I am...
9
18367
by: Maxwell2006 | last post by:
Hi, Is there a concept of shared named memory in .NET to allow multiple program use the name and then set/retrieve values? Thank you, Alan
4
3100
by: herbert | last post by:
I am coding a dozen "background" realtime apps for factory automation in .NET 2.0. The apps need to share a common memory as there are lots of variables to be shared (and synchronized of...
1
1853
by: zeny | last post by:
Hey everyone! I´ve been tying to create a shared memory segment with the size of a structure, as follows: typedef struct{ int id; char message; }data; In the line where i create the shared...
21
8350
by: llothar | last post by:
Hello, i need to manage a heap in shared memory. Does anybody know about a portable (win32+mac+posix) c implementation for this.
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.