[英]Initialize a 1D or 2D array in shared memory
我正在嘗試將字符串的2D char數組初始化為POSIX共享內存,以在其他3個進程之間共享。 關於如何在進程之間使用指針共享單個字符串或整數的方法,有很多教程,但是我找不到如何使用mmap()
初始化1D或2D數組的示例。 我在下面發布了我到目前為止的內容。 這是第一個程序,它創建共享內存對象並使用值files[0][0][0] = '\\0'
初始化數組char files[20][2][100]
。
在C中初始化和共享數組的正確方法是什么?
對於上下文,我編寫了此項目的簡單版本(在有用的SO專家的建議下),該版本不使用共享內存,而是將所有4個進程(由/**********/
分隔)組合為一個。 在下面
在上一篇文章中,我曾問過一個類似的涉及結構的問題,但我的項目必須使用多維數組。 對這個問題的答案任何見解將是有益的這里 。
謝謝。
代碼偏小:(程序初始化共享內存對象和數組)
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
int main (int argc, char *argv[])
{
char files [20][2][100];
/* the size of shared memory object */
int size = sizeof(files);
/* name of the shared memory object */
const char *name = "/PROJ4_SHARED_MEM";
/* shared memory file descriptor */
int shm_fd;
/* pointer to shared memory obect */
void *ptr;
/* create the shared memory object */
shm_fd = shm_open(name, O_CREAT | O_RDRW, 0666);
/* configure the size of the shared memory object */
ftruncate(shm_fd, size);
/* memory map the shared memory object */
ptr = mmap(0, size, PROT_WRITE, MAP_SHARED, shm_fd, 0);
/* save array to the shared memory object. */
/*****WHERE I LOSE IT*****/
return 0;
}
上下文程序:(非POSIX)
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int main (int argc, char *argv[])
{
char files [20][2][100];
files [0][0][0] = '\0';
char file_name[100];
char file_contents[100];
char search[100];
char answer;
int again = 0;
int counter;
int i = 0;
/**************************************************/
while (again == 0)
{
printf("Enter a filename: ");
scanf("%s", &file_name);
getchar();
printf("Enter file contents (string): ");
fgets (file_contents, 100, stdin);
for (i = 0; i < 20; i++)
{
if (files[i][0][0] == '\0')
{
strcpy(files[i][0],file_name);
strcpy(files[i][1],file_contents);
files [i+1][0][0] = '\0';
break;
}
else if (i == 19)
{
printf("ERROR: The directory is full.\n\n");
}
}
counter++;
printf("Save another file y/n ?: ");
scanf(" %c", &answer);
if (answer == 'n')
{
again = 1;
}
}
printf("\n\n");
again = 0;
/**************************************************/
printf("You have saved the following files:\n");
for (i = 0; i < 20; i++)
{
if (files[i][0][0] == '\0')
{
break;
}
printf("%s \n", files[i][0]);
}
printf("\n\n");
/**************************************************/
printf("Would you like to open a file? (y/n) ?: ");
scanf(" %c", &answer);
if (answer == 'n')
again = 1;
while (again == 0)
{
printf("Enter a filename: ");
scanf(" %s", &file_name);
for (i = 0; i < 20; i++)
{
if (strcmp(files[i][0], file_name) == 0)
{
printf("%s \n", files[i][1]);
break;
}
else if (i == 19)
{
printf("ERROR: The file was not found.\n\n");
}
}
printf("Search for another file (y/n) ?: ");
scanf(" %c", &answer);
if (answer == 'n')
{
again = 1;
}
printf("\n\n");
}
getchar();
return 0;
}
該代碼具有指向共享內存的指針,因此,與任何指針一樣,該代碼可以通過索引該指針來訪問共享內存中的任何特定項。
#define INDEX_1_SIZE (20)
#define INDEX_2_SIZE (2)
#define INDEX_3_SIZE (100)
#define SIZE (INDEX_1_SIZE * INDEX_2_SIZE * INDEX_3_SIZE)
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
然后:
int i, j, k; // loop counters
// initialize the shared memory to '\0'
for(i=0;i<INDEX_1_SIZE; i++_
{
for(j=0; j<INDEX_2_SIZE; j++)
{
for(k=0;k<INDEX_3_SIZE; k++)
{
ptr[i][j][k] = '\0';
}
}
}
例如,有更快的方法可以執行對'\\ 0'的初始化:
memset( ptr, '\0', SIZE );
不需要局部變量:“文件”
但是,由於許多不同的進程可以同時訪問共享內存,
該代碼需要所有進程都使用的命名信號量,因此在任何時候只有一個進程正在訪問共享內存,以避免出現“競爭”情況。 這是一個很好的討論,並帶有示例用法,用於信號量http://www.cs.cf.ac.uk/Dave/C/node26.html代碼應檢查錯誤,例如:(當然,使用您自己的變量名)
des_mutex = shm_open(MUTEX, O_CREAT | O_RDWR | O_TRUNC, mode);
if (des_mutex < 0)
{
perror("failure on shm_open on des_mutex");
exit(1);
}
if (ftruncate(des_mutex, sizeof(pthread_mutex_t)) == -1)
{
perror("Error on ftruncate to sizeof pthread_cond_t\n");
exit(-1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.