How can I share a structure with pointers between processes in C?

I am trying to put a file in a structure, but I have problems with memory sharing, because I can access the fields in the created process where the mapping occurs, but cannot access the arrays (it can only access int) in other processes. I tried a lot of different things, but the way I present the following is one that makes more sense to me, since I am allocating memory correctly using shmget.

For clarity: The only thing that separates is the integer lim_thread. Other fields are in a memory area that I cannot access. What for? As I can see, pointers point to a shared memory area.

configs.txt:

Threads = 5
Domains = uc.pt; edu
LocalDomain = so.local
NamedPipeEstatisticas = statistics

structure:

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;
}
+4
2

kaylum, . , , .

, : . , , ( ), .

, , .

+4

, . shmget/shmat /, - .

, (, , .. _thread ).

. 50 + ,

0

Source: https://habr.com/ru/post/1617364/


All Articles