Click here to Skip to main content
14,207,305 members
Rate this:
 
Please Sign up or sign in to vote.
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.

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


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



//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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.

//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.
   

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



Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190612.1 | Last Updated 1 Apr 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100