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

Joaquim Ferrer

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;
}
user1157391

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I share a class between processes?

From Dev

How can i share a serialport between 2 different processes in Python

From Dev

How do I share stdout between multiple processes?

From Dev

How can I share a ContextMenu between Components

From Dev

Share list between multiple forked processes (C)

From Java

How to share mmap between python and node processes

From Dev

Can I use the same pipe between 2 children processes in C?

From Dev

How can I use extern to share globe variables between source files in C++?

From Dev

How can I use extern to share globe variables between source files in C++?

From Dev

How can I return an anonymous struct in C?

From Java

How can I initialize a struct in C?

From Dev

How can I allocate a pointer to a struct in C?

From Dev

How can I make this struct work in C?

From Dev

How can i dereference a pointer to struct in C?

From Dev

How can this behaviour of an array of struct pointers be explained?

From Dev

How can I share sessions between Chrome and Paw?

From Dev

How can I share content-properties between templates

From Dev

How can I share clover coverage data between maven and IntelliJ

From Dev

How can I share my clipboard between two X servers?

From Dev

How can I share my clipboard between two X servers?

From Dev

How can I share clover coverage data between maven and IntelliJ

From Dev

How can I share a monitor between two computers?

From Dev

How can I share assembly references between Autofac modules

From Dev

How can I share files between to routers connected by Ethernet

From Dev

How can I share Tampermonkey scripts between versions of Chrome?

From Dev

How can I share code between my application and my service?

From Dev

Can not declare array of pointers to struct inside struct in C

From Dev

Python3.x how to share a database connection between processes?

From Dev

Comparison between Rust and C/C++ pointers for accessing values in a struct

Related Related

  1. 1

    How can I share a class between processes?

  2. 2

    How can i share a serialport between 2 different processes in Python

  3. 3

    How do I share stdout between multiple processes?

  4. 4

    How can I share a ContextMenu between Components

  5. 5

    Share list between multiple forked processes (C)

  6. 6

    How to share mmap between python and node processes

  7. 7

    Can I use the same pipe between 2 children processes in C?

  8. 8

    How can I use extern to share globe variables between source files in C++?

  9. 9

    How can I use extern to share globe variables between source files in C++?

  10. 10

    How can I return an anonymous struct in C?

  11. 11

    How can I initialize a struct in C?

  12. 12

    How can I allocate a pointer to a struct in C?

  13. 13

    How can I make this struct work in C?

  14. 14

    How can i dereference a pointer to struct in C?

  15. 15

    How can this behaviour of an array of struct pointers be explained?

  16. 16

    How can I share sessions between Chrome and Paw?

  17. 17

    How can I share content-properties between templates

  18. 18

    How can I share clover coverage data between maven and IntelliJ

  19. 19

    How can I share my clipboard between two X servers?

  20. 20

    How can I share my clipboard between two X servers?

  21. 21

    How can I share clover coverage data between maven and IntelliJ

  22. 22

    How can I share a monitor between two computers?

  23. 23

    How can I share assembly references between Autofac modules

  24. 24

    How can I share files between to routers connected by Ethernet

  25. 25

    How can I share Tampermonkey scripts between versions of Chrome?

  26. 26

    How can I share code between my application and my service?

  27. 27

    Can not declare array of pointers to struct inside struct in C

  28. 28

    Python3.x how to share a database connection between processes?

  29. 29

    Comparison between Rust and C/C++ pointers for accessing values in a struct

HotTag

Archive