I am trying to initialize a 2D char array of strings into POSIX shared memory to be shared between 3 other processes. There are plenty of tutorials on how to use a pointer to share a single string or an integer between processes, but I could find no examples on how to initialize 1D or 2D arrays using mmap(). I have posted what I have so far below. It is the first program, which creates the shared memory object and initialize the array char files[20][2][100] with the value files[0][0][0] = '\0'.
What is the proper method to initialize and share an array in C?
For context, I coded a simple version of this project (at the suggestion of helpful S.O. gurus) which does not use shared memory and combines all 4 processes (separated by /**********/) into one. It's below.
CODE SO FAR: (program initializes shared memory object and array)
Code:
#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_RDWR, 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;
}
CONTEXT PROGRAM: (not POSIX)
Code:
#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;
}