Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This program generate two messages in shared memory. What I want is messages to store different data. When I run this program I get this:
Data written in message 1: This is message 1
Data written in message 2: This is message 1

But it shouldn't have text in str2 because the string "This is message 1" is only written in str.


The program prints in str2 the same message as str. How to fix this?





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

int main() 
{ 
//MESSAGE1
    // ftok to generate unique key 
    key_t key = ftok("shmfile",65); 
  
    // shmget returns an identifier in shmid 
    int shmid = shmget(key,1024,0666|IPC_CREAT); 
  
    // shmat to attach to shared memory 
    char *str = (char*) shmat(shmid,(void*)0,0); 
  
  strcpy(str,"This is message 1");
  
    printf("Data written in message 1: %s\n",str); 
      

    //MESSAGE2
    
    // ftok to generate unique key 
    key_t key2 = ftok("shmfile2",77); 
  
    // shmget returns an identifier in shmid 
    int shmid2 = shmget(key2,1024,0666|IPC_CREAT); 
  
    // shmat to attach to shared memory 
    char *str2 = (char*) shmat(shmid2,(void*)0,0); 
     printf("Data written in message2: %s\n",str2); 
    
    
    
  
    return 0; 
}


What I have tried:

I tried to use different keys, different shmid but again the second message have the same data as the first. And if I write something in str2 it overwrites the first message str. I have a reader.c where I want to print the two different messages.
Posted
Updated 15-Dec-18 7:43am
v9

I use shared memory a LOT and for all kinds of different things. The one thing you can't do with it is save pointers because a pointer in one process' address space is not valid to another process. However, you can save and use offsets between processes. Anyway, I usually "map" a data structure to shared memory. It's as easy as just casting a pointer to the data of your structure. You could declare a structure with nearly any kind of data you want although pointers are not recommended. Here's one example:
C++
const size_t BufferSize = 63;
typedef TextBuffer[BufferSize+1];

typedef struct
{
    int         InCount;
    int         OutCount;
    TextBuffer  String1;
    TextBuffer  String2;
    float       Float1;
    float       Float2;
    double      Double1;
    double      Double2;
} SmMemData;

// attach to the shared memory and 'map' it to the data

ShMemData *psmd = (ShMemData *) shmat(shmid,(void*)0,0);
As Richard mentioned, you need a mechanism for controlling access to the data. In this example, one process could increment InCount every time it puts new data in and the other process increments OutCount every time it processes the data. That's just one possibility and you can adapt the data and logic to do what you need it to do.
 
Share this answer
 
C++
// shmat to attach to shared memory
char *str2 = (char*) shmat(shmid2,(void*)0,0);
 printf("Data written in message2: %s\n",str2);

You create str2 to point to an area of shared memory. However, you never write anything to that area so it contains whatever was last written there. In this case it contains the original message because that block of memory has not been re-used.
 
Share this answer
 
Comments
shihappns 15-Dec-18 5:13am    
Yeah, but when I write something in str2 it overwrites the data. I have reader.c program which I want to print the two messages. For example Hide   Copy CodeHide   Copy Code
 strcpy(str2,"This is message 2");
in writer.c and in reader.c I print the first messagae - str and then it shows me: This is message 2. Aren't they supposed to be in different block of memory?
Richard MacCutchan 15-Dec-18 5:38am    
Yes, but you have detached the first block so it may have become free for re-use. You need to synchronise the two activities so that they keep connected to a shared block until both have finished using it. Do not assume that the partner has attached and read the data before you release it.
shihappns 15-Dec-18 5:45am    
I removed the detach but same thing happens. What's the way to synchronise them?
Richard MacCutchan 15-Dec-18 5:57am    
You must add something to your code so each partner knows when a message is ready to be processed, or has been processed. You have no control over which activity is running at any time, so it is up to you to create a mechanism between them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900