Thread: IPC using shared memory in C

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    IPC using shared memory in C

    I am implementing IPC using shared memory in c linux. But my client and server are generating different addresses. Why so?? or how to make them point to same memory location?
    Code:
    //server.c
    #include "/home/user/msgbuf.h"
    #define SHMSZ    127
    int main()
    {
    key_t key;
    message_buf *m;
    char ans='y';
    key = 9876;
    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)
            {
                    perror("shmget");
                    exit(1);
            }
            printf("\nShared Memory Id = %d\n",shmid);
            if ((m = shmat(shmid, NULL, 0)) == (message_buf *) -1)
            {       perror("shmat");
                    exit(1);
            }
          printf("\n m= %d\n",m);
          
    while(ans=='y')
          {
                 sleep(1);
                 printf("Enter your choice");
                 scanf("%c",&ans);
                 getchar();
          }
          return 0;
    }
    Code:
    //client.c
    #include "/home/user/msgbuf.h"
    #define SHMSZ    127
    int main()
    {
    key_t key;
    message_buf *rbuf;
    key = 9876;
    if ((shmid = shmget(key, SHMSZ, 0666)) < 0)
            {
                    perror("shmget");
                    exit(1);
            }
            printf("\nShared Memory Id = %d\n",shmid);
            if ((rbuf = shmat(shmid, NULL, 0)) == (message_buf *) -1)
            {       perror("shmat");
                    exit(1);
            }
          printf("\n rbuf= %d\n",rbuf);
          return 0;
    }
    Code:
    //msgbuf.h
    typedef struct msgbuf1
    {
            int     msglen;
            unsigned char *cp;
    }message_buf;
    Thanks
    Last edited by Babita Sandooja; 04-01-2014 at 01:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > unsigned char *cp;
    The answer would be to not store virtual addresses inside the shared memory area.


    If you do this, then the whole problem of "different addresses" goes away. All that matters is that the data has the same offset within the shared memory segment. It's mapped address becomes irrelevant.
    Code:
    //msgbuf.h
    typedef struct msgbuf1
    {
            int     msglen;
            char cp[SHMSZ-sizeof(int)];
    }message_buf;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared Memory
    By schrei in forum C Programming
    Replies: 2
    Last Post: 12-07-2011, 11:02 AM
  2. STL in Shared Memory
    By AlenDev in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2010, 04:04 AM
  3. ICP shared memory
    By cavemandave in forum C Programming
    Replies: 1
    Last Post: 11-20-2007, 06:08 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM
  5. using shared memory in c
    By flawildcat in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 12:25 PM

Tags for this Thread