我有一个家庭作业要写两个程序,一个生产者和一个消费者。生产者必须将一个正整数作为用户的输入,预先对其进行计算(在本例中是Collatz猜想),并将结果存储在共享内存中。使用者程序必须访问此结果并打印它。
问题是,我已经能够创建、存储和打印字符数组(使用mmap和memcpy),但我似乎不能对整数或整数数组做同样的操作,这是我在计算中将生成的数据类型。我也尝试将计算存储为字符数组,但无法将ints转换为字符。
目前,生产者程序只是将结果打印到控制台,而不是存储结果。
生产者
#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;
}消费者
#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;
}发布于 2022-10-19 18:10:08
单整数
请记住,在生产者程序中,您已经将整数input2存储在共享内存中,因此需要将其打印为整数,而不是字符数组。而不是
printf("The message read from the shared memory: %s", (char *)ptr);你需要把它写成
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猜想迭代中,只需将结果存储在数组中,如下所示:
int *integer_array = (int *) ptr;
while (userInput != 1) {
if (userInput % 2 == 0) {
userInput = userInput / 2;
} else {
userInput = userInput * 3 + 1;
}
*(integer_array++) = userInput;
}一旦生产者程序完成,消费者程序可以打印出以下每一个数字:
int *integer_array = (int *) ptr;
for (int i = 0; i < number_of_ints; i++) {
printf("%d ", integer_array[i]);
}https://stackoverflow.com/questions/74129245
复制相似问题