首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在两个程序之间共享内存中存储和访问数据?

如何在两个程序之间共享内存中存储和访问数据?
EN

Stack Overflow用户
提问于 2022-10-19 17:01:38
回答 1查看 65关注 0票数 1

我有一个家庭作业要写两个程序,一个生产者和一个消费者。生产者必须将一个正整数作为用户的输入,预先对其进行计算(在本例中是Collatz猜想),并将结果存储在共享内存中。使用者程序必须访问此结果并打印它。

问题是,我已经能够创建、存储和打印字符数组(使用mmapmemcpy),但我似乎不能对整数或整数数组做同样的操作,这是我在计算中将生成的数据类型。我也尝试将计算存储为字符数组,但无法将ints转换为字符。

目前,生产者程序只是将结果打印到控制台,而不是存储结果。

生产者

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main()
{
    /* The size (in bytes) of shared-memory object */
    const int SIZE = 4096;

    /* The name of shared-memory object */
    const char* Obj = "Shm";

    /* The shared-memory file descriptor */
    int shm_fd;

    /* The pointer to shared-memory object */
    void* ptr;

    /* Create the shared-memory object */
    shm_fd = shm_open(Obj, O_CREAT | O_RDWR, 0666);

    /* Configure the size of the shared-memory object */
    ftruncate(shm_fd, SIZE);

    /* Map the shared-memory object in the address space of the process */
    ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

    if (ptr == MAP_FAILED)
    {
        printf("Map failed\n");
        return -1;
    }


    //** NEW CODE //

    // calculate based on user input 
    int userInput; // variable to store user int input

    printf("please input a starting number for the collatz conjecture: \n");
    if (scanf("%d", &userInput) == 1) {
        // print each number in the conjecture 
        printf("%d", userInput);
        
        
        // TEST AREA 
    
        int input2 = 5; // THIS WORKS TO SAVE A STRING, BUT NOT INTS
        
        //char inputAr[SIZE];

        //char temp[] = (char)input2;
        //strcat(inputAr, temp);
        
        memcpy(ptr, &input2, SIZE);

        //  TEST AREA

        //char input[SIZE];
        //char temp[10];
        //strcpy(temp, "40123456");
        ////char temp[] = "4";
        //strcat(input, temp);
        //strcpy(temp, " 6");
        //strcat(input, temp);
        //memcpy(ptr, &input, SIZE);




        printf(" ");
        while (userInput != 1) {
            if (userInput % 2 == 0) {
                userInput = userInput / 2;
                printf("%d", userInput);
                printf(" ");
            }
            else {
                userInput = userInput * 3 + 1;
                printf("%d", userInput);
                printf(" ");
            }
        }// end while

    }

    //** END NEW CODE

    /* Create a message and write it to the shared-memory object */
    //printf("Please input a character string: \n");
    //fgets(ptr, SIZE, stdin);
    printf("Writing the message to the shared memory is done! \n");

    return 0;
}

消费者

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main()
{

    /* The size (in bytes) of shared-memory object */
    const int SIZE = 4096;

    /* The name of shared-memory object */
    const char* Obj = "Shm";

    /* The shared-memory file descriptor */
    int shm_fd;

    /* The pointer to shared-memory object */
    void* ptr;

    /* Open the shared-memory object */
    shm_fd = shm_open(Obj, O_RDONLY, 0666);

    if (shm_fd == -1)
    {
        printf("Shared memory failed\n");
        exit(-1);
    }

    /* Map the shared-memory object in the address space of the process */
    ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);

    if (ptr == MAP_FAILED)
    {
        printf("Map failed\n");
        exit(-1);
    }

    
    /* Read from the shared-memory object */
    //printf("Test message: %d", (int*)ptr);
    printf("\n");
    printf("The message read from the shared memory: %s", (char*)ptr);
    

    /* Remove the shared-memory object */
    if (shm_unlink(Obj) == -1)
    {
        printf("Error removing %s\n", Obj);
        exit(-1);
    }

    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-19 18:10:08

单整数

请记住,在生产者程序中,您已经将整数input2存储在共享内存中,因此需要将其打印为整数,而不是字符数组。而不是

代码语言:javascript
复制
printf("The message read from the shared memory: %s", (char *)ptr);

你需要把它写成

代码语言:javascript
复制
int *integer_array = (int *) ptr;
printf("The message read from the shared memory: %d", *integer_array);

这会将ptr (即void *)类型转换为整数指针(int *),您可以取消引用以获得input2的值。此外,"%d"用于整数,而不是"%s"

整数阵

要打印出一个整数数组,而不仅仅是一个整数数组,就必须在ptr中为number_of_ints * sizeof(int)提供足够的空间。在每次Collatz猜想迭代中,只需将结果存储在数组中,如下所示:

代码语言:javascript
复制
int *integer_array = (int *) ptr;
while (userInput != 1) {
    if (userInput % 2 == 0) {
        userInput = userInput / 2;
    } else {
        userInput = userInput * 3 + 1;
    }

    *(integer_array++) = userInput;
}

一旦生产者程序完成,消费者程序可以打印出以下每一个数字:

代码语言:javascript
复制
int *integer_array = (int *) ptr;
for (int i = 0; i < number_of_ints; i++) {
    printf("%d ", integer_array[i]);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74129245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档