Home > Enterprise >  Different shared memory values between processes
Different shared memory values between processes

Time:12-18

I create a buffer via this function.

int create_buffer(const char *shmem_name, void **shmem_obj)
{
    int returnval = 0;

    int fd;
    fd = shm_open(shmem_name, O_CREAT | O_RDWR, 0666);

    ftruncate(fd, sizeof(ShareStruct));

    *shmem_obj = (ShareStruct *)mmap(NULL, sizeof(ShareStruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    if (shmem_obj < 0)
    {
        returnval = -1;
    }

    return returnval;
}

Then when I use the pointer in process 1 and its functions, everything works.

In process 2 I fetch the buffer using this function:

int fetch_buffer(const char *shmem_name, void **shmem_obj)
{
    int returnval = 0;

    int fd;
    fd = shm_open(shmem_name, O_RDWR, 0666);

    ftruncate(fd, sizeof(ShareStruct));

    *shmem_obj = (ShareStruct *)mmap(NULL, sizeof(ShareStruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    if (shmem_obj < 0)
    {
        returnval = -1;
    }

    printf("buffer fetched \n");

    return returnval;
}

But the contents of the buffer are different (weird values).

What am I doing wrong?

EDIT:

process 1 code:

#define SHMEM_NAME "OS"

int main(void) {
    int exitcode = 0;
    void *shmem_obj;

    /* Initialize the buffer and create the shared memory */
    if( create_buffer(SHMEM_NAME, &shmem_obj) != 0) {
        fprintf(stderr, "Something went wrong while initializing the buffer.\n");
        exitcode = 1;
    } else {

        printf("Shared buffer created.\n");

        initParameters(&shmem_obj);

        subscribe(0, &shmem_obj);

        printf("\n[PRESS ENTER TO CLOSE THE SHARED BUFFER]");
        getchar();

        /* unlink the shared memory */
        if(destroy_buffer(SHMEM_NAME) != 0) {
            fprintf(stderr, "Something went wrong while destroying the buffer.\n");
            exitcode = 2;
        } else {
            printf("Shared buffer closed.\n");
        }
    }
    
    return exitcode;
}

Process 2 code:

#define SHMEM_NAME "OS"

int main(void) {

    void *shmem_obj;
    /* fetch the buffer from shared memory */
    fetch_buffer(SHMEM_NAME, &shmem_obj);

    subscribe(1, &shmem_obj);
    printf("Subscriber 0 subscribed\n");
    
}

Other code:

typedef struct ShareStruct
{
    Subscriber subscriberList[20];
    int subscriberActiveList[20];
    unsigned int writepointer_tail;
    unsigned int readpointer_head;
    Task taskList[21];
    sem_t freePostions;
    pthread_mutex_t mutex;
    unsigned int subscriberCount;
} ShareStruct;

int initParameters(void *shmem_obj)
{

    ShareStruct *shmem = shmem_obj;
    int returnval = 0;

    shmem->writepointer_tail = 0;
    shmem->readpointer_head = 0;
    shmem->subscriberCount = 0;
    pthread_mutex_init(&shmem->mutex, NULL);
    sem_init(&shmem->freePostions, 1, 20);

    return returnval;
}

int destroy_buffer(const char *shmem_name)
{
    int returnval = 0;
    returnval = shm_unlink(shmem_name);
    return returnval;
}

int subscribe(subscriber_id subscriber, void *shmem_obj)
{
    int returnval = 0;
    ShareStruct *shmem = shmem_obj;

    int value;
    sem_getvalue(&shmem->freePostions, &value);
    printf("sem value : %d\n", value);

    pthread_mutex_lock(&shmem->mutex);

    int freeposition = returnNextFreePosition(shmem_obj);

    if (freeposition != -1)
    {
        printf("subscribe logic");

    }
    else
    {
        printf("Subscriber list is full \n");
    }
    pthread_mutex_unlock(&shmem->mutex);

    return returnval;
}

CodePudding user response:

The problem is that you're being inconsistent in the number of layers of pointer indirection there are, and your use of void * rather than more specific pointer types is preventing the compiler from realizing and telling you that you're doing that. Replace all of your void *s with ShareStruct *s, and then fix the resulting errors and warnings your compiler gives you, and your code will start to work.

  • Related