-1

shmget.c:

#include<sys/types.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
main()
{
    key_t key;
    int shmid;
    char* addr1;
    key = ftok("/home/tamil/myc/pws.c",'T');
    shmid = shmget(key,128*1024,IPC_CREAT|SHM_R|SHM_W);

    addr1 = shmat(shmid,0,0);

    printf("\nIPC SHARED MEMORY");
    printf("\n SENDER ADDRESS");
    printf("\nTHE ADDRESS IS %p",addr1);
    printf("\nENTER THE MESSAGE:");
    scanf("%s",addr1);
    printf("\nMESSAGE STORED IN %p IS %s",addr1,addr1);  
}

shmget2.c:

#include<sys/types.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>

main()
{
    int shmid;
    char* addr1;
    key_t key;


    key = ftok("/home/tamil/myc/pws.c",'T');
    shmid = shmget(key,128*1024,SHM_R|SHM_W);

    addr1 = shmat(shmid,0,0);


    printf("\nIPC SHARED MEMORY");
    printf("\n SENDER ADDRESS");
    printf("\nTHE ADDRESSS IS %p",addr1);
    printf("\nMESSAGE STORED IN %p IS %s",addr1,addr1);

}

Output:

tamil@ubuntu:~/myc$ cc shmget.c
tamil@ubuntu:~/myc$ ./a.out

IPC SHARED MEMORY
 SENDER ADDRESS
**THE ADDRESS IS **0xb786c000****
ENTER THE MESSAGE:helloworld

MESSAGE STORED IN **0xb786c000** IS helloworldtamil@ubuntu:~/myc$ cc shmget2.c
tamil@ubuntu:~/myc$ ./a.out

IPC SHARED MEMORY
 SENDER ADDRESS
**THE ADDRESSS IS **0xb7706000****
MESSAGE STORED IN **0xb7706000** IS helloworldtamil@ubuntu:~/myc$ 

This all works well. But the address is not the same. Why is this?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77
  • 2
    Translated to seriousness: you've been yelling close to the top of your voice in **all your questions**, not just this one. If you have anger management issues you may want to do something about that ASAP. The louder you are on the Internet, the more people are just going to ignore you. – BoltClock Mar 07 '11 at 17:48
  • Maybe I shouldn't have edited the question to tone down the yelling, the comments are pretty good this way. :) – Clinton Pierce Mar 07 '11 at 17:50
  • @clintp: You can roll it back if you're so inclined :) – BoltClock Mar 07 '11 at 17:51
  • 2
    @clintp: DO NOT WORRY **I WILL COMPENSATE AGAIN**!!!! – Thomas Mar 07 '11 at 18:00
  • @BoltClock: Seriously sorry. I never done it willingly.Probably my English mayn't be good. My way of asking the things is not good. But that is not my character. If it so, I`ll surely change myself. – Muthu Ganapathy Nathan Mar 13 '11 at 08:01
  • OK, in any case I wasn't the one who suspended you. I forwarded your email to team@stackoverflow.com so you should get a reply from them soon. – BoltClock Mar 13 '11 at 08:05

2 Answers2

6

Shared memory can be mapped to different regions in the address space of different processes. This is perfectly normal. But if you're storing pointers inside the shared memory to other parts of the shared memory, then you're in trouble. You'll need to use something like an offset pointer in that case.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Note that the [man page](http://linux.die.net/man/2/shmat) says the same thing: "Using shmat() with shmaddr equal to NULL is the preferred, portable way of attaching a shared memory segment. Be aware that the shared memory segment attached in this way may be attached at different addresses in different processes. Therefore, any pointers maintained within the shared memory must be made relative (typically to the starting address of the segment), rather than absolute." – Robᵩ Mar 07 '11 at 19:58
1

The addresses you're seeing are different because they are virtual. Each process gets a 'pretend' address space that is contiguous (no gaps) and can be made larger over time. In reality, the virtual addresses can be mapped to different parts of RAM in chunks. Have a look here for more detail (in particular, this diagram). In the case of shared memory, there is a single area of RAM that can be 'seen' by both processes. However, it is perfectly normal for the addresses to look different to each process.

Here's the idea:

                      0x00   0x01             0x07               0xff         
 Process 2 Virtual:       +--+-----------------+------------------+
                             |                 |
 RAM Physical Addr:     0x04 +-----shared------+ 0x0a
                             |                 | 
 Process 1 Virtual:  +-------+-----------------+---------+
                    0x00    0x09              0x0f       0xff

(Not drawn to scale :) Note that process 1 and 2 share the same area of RAM (physical address 0x04 through 0x0a), but that shared piece of RAM is mapped to different parts of their virtual address spaces (0x09 through 0x0f for P1; 0x01 through 0x07 for P2).

phooji
  • 10,086
  • 2
  • 38
  • 45
  • ya i got it thank you. But If I want the same physical address and not this different logical address (to satisfy the college professor )is there is any way? – Muthu Ganapathy Nathan Mar 08 '11 at 12:55
  • @pooji: ya i got it thank you. But If I want the same physical address and not this different logical address (to satisfy the college professor )is there is any way? – Muthu Ganapathy Nathan Mar 09 '11 at 14:46