简体   繁体   English

Ubuntu在两个进程之间共享内存,代码不起作用

[英]Ubuntu Shared Memory Between 2 Processes, Code Not Working

I'm writing two processes separately which should interact through shared memory and I just can't spot where I'm going wrong. 我分别编写了两个进程,这些进程应该通过共享内存进行交互,但我无法发现我要去哪里。 The first process has definitely created the shared memory and if reprogrammed, it can read from shared memory too. 第一个过程肯定创建了共享内存,如果重新编程,它也可以从共享内存中读取。

However, the second Output program can't seem to read from it at all. 但是,第二个Output程序似乎根本无法读取。 I use shmget in both programs to create a memory, then to join it. 我在两个程序中都使用shmget创建内存,然后将其加入。 I insert the char array nameArray with the Input program, then the Output should read it from shared memory and cout the name. 我在输入程序中插入了char数组nameArray,然后Output应该从共享内存中读取它并指定名称。

Any help is appreciated, code is below :) 任何帮助表示赞赏,代码如下:)

Input: 输入:

#include <iostream>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#define SHM_KEY 982
using namespace std;

main()
{
    int shmid;
    char nameArray[] = "Bill Gates";
    unsigned length = sizeof(nameArray)/sizeof(nameArray[0]);
    shmid=shmget(SHM_KEY,256,0777|IPC_CREAT);

    if(shmid!=(-1))     /*Error checking measure to ensure shared memory creation was successful*/
    {
        char * ptr=(char *)shmat(shmid,0,0);    /*Assigns the char pointer to the start of shared memory*/

        for(int i=0; i<length;i++)      /*For every letter in the nameArray*/
        {
            ptr=(char *)shmat(shmid,0,i);
            *ptr=nameArray[i];      /*Sends the local char array to shared memory*/
        }
    }
    else    /*Displays Error message given shared memory creation failue*/
    {
        cout << "Sorry, shared memory creation failed"
    }
}       

Output: 输出:

#include <iostream>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#define SHM_KEY 982
using namespace std;

main()
{
    int shmid;
    char nameArray[20];
    shmid=shmget(SHM_KEY,256,0777|IPC_CREAT);

    if(shmid!=(-1)) /*Error checking measure to ensure shared memory creation was successful*/
    {
        char * ptr=(char *)shmat(shmid,0,0);

        for(int i=0; i<11;i++)
        {
            cout << *((char *)shmat(shmid,0,i));
        }

    }
    else{}
}       

The code 编码

    for(int i=0; i<length;i++)      /*For every letter in the nameArray*/
    {
        ptr=(char *)shmat(shmid,0,i);
        *ptr=nameArray[i];      /*Sends the local char array to shared memory*/
    }

is not doing what you think it is doing. 没有按照您认为的去做。 Every call to shmat attaches the whole shared memory segment to another address, with different flags (ranging from 0 to length ; most of them meaningless). 每次对shmat调用都会将整个共享内存段附加到另一个地址,并带有不同的标志(范围从0length ;大多数标志没有意义)。

Remove shmat calls from the loop. 从循环中删除shmat调用。 What you really need is 您真正需要的是

    for(int i=0; i<length;i++)      /*For every letter in the nameArray*/
    {
        ptr[i]=nameArray[i];      /*Sends the local char array to shared memory*/
    }

and a similar change in the other program. 以及其他程序中的类似更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM