Read Write Integer Array Into Shared Memory
Asked Answered
R

4

5

The following is the READER-WRITER code for my shared memory.

Read Code-

int main(){
int shmid;
int *array;
int count = 5;
int i = 0;
key_t key = 12345;

shmid = shmget(key, count*sizeof(int), IPC_EXCL);

array = shmat(shmid, 0, SHM_RDONLY);

for(i=0; i<5; i++)
    {
        printf("\n%d---\n", array[i] );
    }

    printf("\nRead to memory succesful--\n");

    shmdt((void *) array);
    return 0;
}

Write Code-

int main()
{
int shmid;
int *array;
int count = 5;
int i = 0;
int SizeMem;
key_t key = 12345;

SizeMem = sizeof(*array)*count;

shmid = shmget(key, count*sizeof(int), IPC_CREAT);

array = (int *)shmat(shmid, 0, 0);

array = malloc(sizeof(int)*count);

for(i=0; i<5; i++)
{
    array[i] = i;
}

for(i=0; i<count; i++)
{
    printf("\n%d---\n", array[i]);
}

printf("\nWritting to memory succesful--\n");

shmdt((void *) array);

return 0;
}

After writing to the memory when I try to read, the output are garbage values. Can someone please explain what I have done wrong(The output shows all zeros) Thankyou

Reboant answered 20/1, 2014 at 6:8 Comment(3)
In the write section you are overlaying the array pointer with the return address from malloc and writing into the heap allocation rather than shared memory. The malloc is unnecessary.Shelbyshelden
You are malloc'ing array after you ret values from shmat. Effectively getting rid of the values.Capriole
@AnshDavid tested your code removing the line I mentioned in my answer and it runs all right on my machine.I should be OK.Rosenblum
W
4

In the write section, you used malloc() after getting share memory address, so it will be overwritten. You should remove the malloc() line

In the read section, the for loop should look like this

printf("\n%d---\n", array[i] );
Webfoot answered 20/1, 2014 at 6:28 Comment(0)
M
1

In write code your trying to attach the shared memory to array variable and also in next step u assigning the same to new location in user space (heap) by calling malloc.

So you are loosing the the location from where is the shared memory and writing into new array loacated by malloc.

array = (int *)shmat(shmid, 0, 0);
array = malloc(sizeof(int)*count);

Use different pointers if you want an array in user space also or remove the that malloc line.

Shared memory will allocate the memory specified by you while creating it. you cant allocate it later by other means.

Metrify answered 20/1, 2014 at 6:33 Comment(0)
R
1

Your malloc overwrites shmat's return value here.You are not writing to share memory but to the memory you just malloced.

array = (int *)shmat(shmid, 0, 0);

by delete this line.your code runs ok on my machine.

array = malloc(sizeof(int)*count);

Rosenblum answered 20/1, 2014 at 6:35 Comment(0)
P
1

Leave the data at a specified offset into the memory segment, which can be fixed at compile-time or placed in a field at some known location in the shared memory segment

The memory you allocate to a pointer using malloc() is private to that process. So, when you try to access the pointer in another process (other than the process which malloced it) you are likely going to access an invalid memory page or a memory page mapped in another process address space. So, you are likely to get a segfault.

If you are using the shared memory, you must make sure all the data you want to expose to other processes is "in" the shared memory segment and not private memory segments of the process.

You could try, leaving the data at a specified offset in the memory segment, which can be concretely defined at compile time or placed in a field at some known location in the shared memory segment.

Your code would work by removing the malloc call from your code. I have tried it and its working fine

Perdurable answered 20/1, 2014 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.