0

Write:

#include "apue.h"
#include "errorlog.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main ()
{
        int segment_id;
        char* shared_memory;
        struct shmid_ds *shmbuffer;
        int segment_size;
        const int shared_segment_size = 0x6400;
        shmbuffer=malloc(sizeof(struct shmid_ds));
        /* Allocate a shared memory segment.  */
        if((segment_id = shmget (12345, shared_segment_size,IPC_CREAT|S_IRUSR | S_IWUSR))==-1)
        perror("shmget");
        /* Attach the shared memory segment.  */
        shared_memory = (char*) shmat (segment_id, 0, 0);
        printf ("shared memory attached at address %p\n", shared_memory);
        /* Determine the segment's size. */
        shmctl (segment_id, IPC_STAT, shmbuffer);
        segment_size  =  shmbuffer->shm_segsz;
        printf ("segment size: %d\n", segment_size);
        /* Write a string to the shared memory segment.  */
        sprintf (shared_memory, "Hello, world.");
        /* Detach the shared memory segment.  */
        shmdt (shared_memory);
        return 0;
}

Output:

shared memory attached at address 0xb77ab000
segment size: 25600

Read:

#include "apue.h"
#include "errorlog.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main ()
{
        int segment_id;
        char* shared_memory;
        struct shmid_ds *shmbuffer;
        int segment_size;
        const int shared_segment_size = 0x6400;
        shmbuffer=malloc(sizeof(struct shmid_ds));
        /* Allocate a shared memory segment.  */
        segment_id = shmget (12345, shared_segment_size,
                        IPC_CREAT  | S_IRUSR | S_IWUSR);

        /* Attach the shared memory segment, at a different address.  */
        shared_memory = (char*) shmat (segment_id, (void*)0, 0);
        printf ("shared memory reattached at address %p\n", shared_memory);
        /* Print out the string from shared memory.  */
        printf ("%s\n", shared_memory);
        /* Detach the shared memory segment.  */
        shmdt (shared_memory);
        printf("current proc id %d",getpid());
        return 0;
}

Output:

shared memory reattached at address 0xb7783000
Hello, world.
current proc id 6530

I have executed this program of shared memory. I have a doubt that how does the different memory address get the same data. Here the writing address is 0xb77ab000 and the reading address is 0xb7783000, but the correct data "Hello, world" is giving. Please anyone explain this..

1 Answers1

0

The kernel handles this.

All the memory you see from userspace is called "virtual memory". They can be unallocated (if nothing was written), allocated on real RAM, allocated on swap, or pointing to a memory mapped file, or shared memory. The operating system is responsible to map them to physical memory addresses.

When a memory is shared, the page is mapped to different virtual memory address, but internally they share the same physical page.

Tatsuyuki Ishi
  • 3,883
  • 3
  • 29
  • 41