I'm trying to put a file in a struct but I'm having problems sharing the memory as I can access the fields in the created process where the mapping happens but cannot access the arrays(can only access the int) in the other processes. I tried a lot of different things but the way I present next is the one that makes more sense to me as I'm allocating memory the right way with shmget.
For clarity: The only thing that is being shared is the integer lim_thread. The other fields are in a area of memory that I can not access. Why? As I see the pointers are pointing to a region of memory that is shared.
configs.txt:
Threads = 5
Domains = uc.pt; edu
LocalDomain = so.local
NamedPipeEstatisticas = statistics
struct:
typedef struct configs
{
int lim_thread;
char (*valid_domains)[MAX_DOMAIN_SIZE]; //max size for valid domains
char *domain;
char *stat_pipe;
sem_t sem;
} CONFIGS;
main.c:
/*Shared memory for configs*/
CONFIGS *_configs;
int _shmid_configs;
int main(int argc, char *argv[]) {
pid_t config_pid; //will hold the configuration process id
_shmid_configs = shmget(IPC_PRIVATE, sizeof(CONFIGS), IPC_CREAT|0666);
_configs = shmat(_shmid_configs, NULL, 0);
/*Semaphores*/
sem_init( &( _configs->sem), 1, 0);
//initializes processes
if( ( config_pid = fork() ) < 0) {
perror("Failed creating configuration manager process");
num_forks++;
}
else if( config_pid == 0 ) {
init_config();
exit(0);
}
sem_wait(&(_configs->sem));
/////////////////////////DEBUG////////////////////////////////
printf("%d\n", _configs->lim_thread);
printf("%s\n", *(_configs->valid_domains+1));
printf("%s\n", _configs->domain);
printf("%s\n", _configs->stat_pipe);
//////////////////////////////////////////////////////////////
return 0;
}
configs.c
#define MAX_LINE_SIZE 1000
int init_config() {
FILE *fp;
char domains[MAX_LINE_SIZE], line[MAX_LINE_SIZE], *saveptr, *aux_char;
int count = 0, aux;
int temp_shmid;
if( ( fp = fopen( "./configs.txt", "r")) == NULL) {
perror("Failed to open configs.txt");
return -1;
}
fscanf( fp,"Threads = %d\n", &(_configs->lim_thread));
//To start reading "Domains = "
fscanf(fp, "Domains = ");
fgets(domains, MAX_LINE_SIZE, fp);
domains[strlen(domains) -1] = '\0';
//counts the number of domains
for(aux = 0; aux < strlen(domains); aux++) {
if( domains[aux] == ';' ) {
count++;
}
}
//creates shared memory for the valid domains
temp_shmid = shmget(IPC_PRIVATE, (count+1) * sizeof( char[MAX_DOMAIN_SIZE]), IPC_CREAT|0666);
_configs->valid_domains = shmat( temp_shmid, NULL, 0);
//copies all the data to the struct
strcpy( *(_configs->valid_domains), strtok_r(domains, "; ", &saveptr) );
aux = 1;
while( ( aux_char = strtok_r( NULL, "; ", &saveptr) ) != NULL) {
strcpy( *(_configs->valid_domains + aux), aux_char);
aux++;
}
fscanf(fp, "LocalDomain = %s\n", line);
temp_shmid = shmget(IPC_PRIVATE, (strlen(line) + 1) * sizeof(char), IPC_CREAT|0660);
_configs->domain = (char*)shmat(temp_shmid, NULL, 0);
strcpy(_configs->domain, line);
fscanf(fp, "NamedPipeEstatisticas = %s\n", line);
temp_shmid = shmget( IPC_PRIVATE, (strlen(line) +1) * sizeof(char), IPC_CREAT|0660);
_configs->stat_pipe = (char*)shmat(temp_shmid, NULL, 0);
strcpy(_configs->stat_pipe, line);
fclose(fp);
sem_post( &(_configs->sem));
/////////////////////////DEBUG////////////////////////////////
printf("%d\n", _configs->lim_thread);
printf("%s\n", *(_configs->valid_domains+1));
printf("%s\n", _configs->domain);
printf("%s\n", _configs->stat_pipe);
//////////////////////////////////////////////////////////////
return 0;
}
As kaylum pointed out, each process might map a shared memory block to a different virtual address. Hence pointers cannot be shared, you need to work with offsets.
Allocate a single block of shared memory that you divide in two parts: a table of content and a data area. The table of content consists of variables that contain either a value or (instead of a pointer), the offset between the start of the shared memory block and the start of a data element inside the data area.
Then to obtain the address of a data element a process simply adds its offset to the address of the shared memory block in its address space.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments