Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends

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?
I am first executing server.c and stopping after one iteration, and then client.c. And I am getting different addresses.

Objective-C
//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;
}


Objective-C
//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;
}



Objective-C
//msgbuf.h
typedef struct msgbuf1
{
        int     msglen;
        unsigned char *cp;
}message_buf;


Thanks :)
Posted
Updated 31-Mar-14 21:04pm
v5
Comments
CPallini 1-Apr-14 4:50am    
Your code is practically identical to the one in the following page:
http://www.cs.cf.ac.uk/Dave/C/node27.html#SECTION002731000000000000000
I made a test, it works and both processes report the same shmid
Babita22 1-Apr-14 5:14am    
yes shmid's are same. but m and rbuf i.e the address of memory segments are same at the link you provided but not in my program. Please check this. :)

1 solution

This is the usual challenge in writing shared memory code. In some cases, you can force the shared memory to map to some high, unused address. In general, this may not be practical or convenient.

Since the addresses are different, you can't store pointers there. Instead - you can store byte offsets. That's worked well for me.

C++
//msgbuf.h
typedef struct msgbuf1
{
    size_t msglen;
    size_t offset;
} message_buf;

/* return pointer to the buffer contents in shared memory. */
static char *as_pointer(char *base, message_buf *desc)
{
    return base+desc->offset;
}


This also works for memory mapped files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900