Linux共享内存。发送多条消息。 [英] Linux shared memory. Sending multiple messages.

查看:120
本文介绍了Linux共享内存。发送多条消息。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序在共享内存中生成两条消息。我想要的是用于存储不同数据的消息。当我运行这个程序时,我得到了这个:

数据写在消息1中:这是消息1

数据写在消息2中:这是消息1



但它不应该在 str2 中有文字,因为字符串This is message 1只能用 str。 <写br $>




程序在str2中打印与str相同的消息。如何解决这个问题?











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; 
}





我的尝试:



我试图使用不同的密钥,不同的shmid但第二条消息的数据与第一条消息的数据相同。如果我在str2中写一些东西,它会覆盖第一个消息str。我有一个reader.c,我想打印两个不同的消息。



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.

推荐答案

// shmat to attach to shared memory
char *str2 = (char*) shmat(shmid2,(void*)0,0);
 printf("Data written in message2: %s\n",str2);



您创建 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.


我使用共享内存很多并且用于各种不同的事情。你不能做的一件事是保存指针,因为一个进程的地址空间中的指针对另一个进程无效。但是,您可以在进程之间保存和使用偏移量。无论如何,我通常将数据结构映射到共享内存。它就像投射指向结构数据的指针一样简单。您可以使用几乎任何类型的数据声明一个结构,但不建议使用指针。这是一个例子:
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:
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);

正如Richard所说,您需要一种机制来控制对数据的访问。在此示例中,每次处理新数据时,一个进程可以递增InCount,而另一个进程在每次处理数据时递增OutCount。这只是一种可能性,您可以调整数据和逻辑来完成您需要它做的事情。

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.


这篇关于Linux共享内存。发送多条消息。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆