我有一個家庭作業要撰寫兩個程式,一個生產者和一個消費者。生產者必須將一個正整數作為用戶的輸入,對其進行計算(在本例中為 Collat??z 猜想),并將結果存盤在共享記憶體中。消費者程式必須訪問該結果并列印它。
問題是我已經能夠創建、存盤和列印字符陣列(使用mmapand memcpy),但我似乎無法對整數或整數陣列做同樣的事情,這是我將通過計算生成的資料型別。我也嘗試將我的計算存盤為字符陣列,但無法將整數轉換為字符。
目前,生產者程式只是將結果列印到控制臺而不是存盤它們。
制片人
#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;
}
uj5u.com熱心網友回復:
單整數
請記住,在生產者程式中,您已將整數存盤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(a void *)型別轉換為整數指標(int *),您可以取消參考以獲取input2. 此外,"%d"用于整數而不是"%s".
整數陣列
要列印出一個整數陣列,而不僅僅是一個整數,你必須在ptrfor中有足夠的空間number_of_ints * sizeof(int)。在每個 Collat??z 猜想迭代中,您只需將結果存盤在陣列中,如下所示:
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://www.uj5u.com/ruanti/519695.html
標籤:Clinux指针共享内存
