2

I would like to create a server-client program in which the two processes pass information between each other using shared memory

information to be passed:

typedef struct shared_mem{
int board[BOARD_SIZE * BOARD_SIZE];
int goal;
}shared_mem;
shared_mem *msg;

server:

int main(int argc, char **argv) {

int shmid;
key_t key=ftok("2048_client", 42);
if(key == -1) {
        printf("ftok failed");
        return -1;
    }


shared_mem *shm;
msg=(shared_mem *)malloc(sizeof(shared_mem));

/*
     * Create the segment
    */
    if ((shmid = shmget(key, sizeof(msg), IPC_CREAT)) < 0) {
        perror("shmget");
        exit(1);
    }

    /*
    * Now we attach the segment to our data space.
    */
  if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }

msg=shm;

(*msg).goal=64;
}

client:

int main(int argc, char **argv) {

int shmid;
key_t key=ftok("2048_client", 42);
if(key == -1) {
        printf("ftok failed");
        return -1;
    }
shared_mem *shm;
msg=(shared_mem *)malloc(sizeof(shared_mem));
/*
     * Create the segment.
    */
    if ((shmid = shmget(key, sizeof(msg), 0)) < 0) {
        perror("shmget");
        exit(1);
    }

    /*
    * Now we attach the segment to our data space.
    */
  if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }

msg=shm;
printf("dscsadc:  %d",msg->goal);
   }

I am new to shared memory and i would like to understand why it doesn't work and how it is supposed to work. I am getting "shmat: Permission denied"

7
  • How doesn't it work ? What errors do you get ? Commented May 26, 2014 at 18:34
  • The only things that seem obviously wrong here are that you have to supply a value for key (ftok may help with that), and you may also be required to round sizeof(msg) up to a multiple of sysconf(_SC_PAGESIZE). Beyond that: what is this doing that you did not expect it to do? Commented May 26, 2014 at 18:35
  • @Zack I tried using ftok but i get the error: shmat: Permission denied Commented May 26, 2014 at 19:15
  • In the server, the line msg=shm; leaks the carefully allocated memory; remove the malloc() line. Similarly in the client. The line (*msg).goal=64; would conventionally be written msg->goal = 64; — the -> operator was invented for a reason. Commented May 26, 2014 at 19:22
  • @JonathanLeffler I tried doing that but I get an error saying every function is defined more than once:2048.c:120:40: warning: comparison of distinct pointer types lacks a cast [enabled by default] /tmp/ccNMeNPm.o: In function main': 2048.c:(.text+0x45): multiple definition of main' /tmp/ccLsff3I.o:2048_client.c:(.text+0x0): first defined here 2048: In function move_board': (.text+0xaff): multiple definition of move_board' /tmp/ccNMeNPm.o:2048.c:(.text+0xa1b): first defined here And so on for all the other functions. Commented May 26, 2014 at 19:28

2 Answers 2

2

The problem is that you create the shared memory segment with 0000 permissions, so no-one can read or write it.

Change the shmget() call from:

if ((shmid = shmget(key, sizeof(msg), IPC_CREAT)) < 0) {

to:

if ((shmid = shmget(key, sizeof(msg), IPC_CREAT|0600)) < 0) {

Only the user running the program can access the shared memory that is created.

Note that POSIX shmget() says:

  • The low-order nine bits of shm_perm.mode are set to the low-order nine bits of shmflg.
Sign up to request clarification or add additional context in comments.

Comments

0

If you're not limited to C only, look at the boost library. It enables you to create shared memory segments for interprocess communication.

using boost::interprocess;
shared_memory_object shm_obj
   (create_only,                  //only create
   "shared_memory",              //name
   read_write                   //read-write mode
   );

http://www.boost.org/doc/libs/1_54_0/doc/html/interprocess/sharedmemorybetweenprocesses.html

Other then that, you can always use pipes, or if you're thinking about windows - COM.

2 Comments

I am limited to C only and not allowed to use Pipes and I am really trying to understand shared memory.
sorry I couldn't be more help then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.