2

The following code always returns -1 for creating shared memory. I don't know the reason for it. As far as I know my code is correct. Perror returns not such file or directory. I don't know what it is pointing to, but this file and the header are in the same directory. Here is the code:

#include "MyShared.h"


int main()
{
struct MyShared *obj;

int shmid,i,childpid;

shmid=shmget(MySharedKey,sizeof(struct MyShared),PERM);
if(shmid==-1)
    printf("Failed to create shared mem\n");
obj=(struct MyShared*)shmat(shmid,NULL,0);

obj->ReadFromBuf=0;
....
}

Here is the header file. My Shared.h

#ifndef MYSHARED_H_INCLUDED
#define MYSHARED_H_INCLUDED

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>


#define PERM (S_IRWXU | S_IRGRP)
#define MySharedKey 564732
#define SIZE 512  // 512 bytes

struct MyShared
{
    char buf[SIZE];

    int ReadFromBuf,WriteToBuf,readbytes;
};

#endif

Why can't this code create a shared memory?

I am using ubuntu 10.04.

I am following Unix System programming by Stevens and it doesn't say anything or creation permissions for a shared memory.

Regards

Alfred
  • 1,543
  • 7
  • 33
  • 45

2 Answers2

3

Instead of

printf("Failed to create shared mem\n");

better use

perror("Failed to create shared mem");

This evaluates the errno variable and prints a more useful error message, in your case something like

Failed to create shared mem: No such file or directory

The reason is that you try to attach to a non-existing memory segment.

In order to create a shared memory segment, you need to pass the IPC_CREAT flag to shmget():

shmid=shmget(MySharedKey,sizeof(struct MyShared),PERM | IPC_CREAT);

See also http://www.kernel.org/doc/man-pages/online/pages/man2/shmget.2.html

If you are not passing this flag, it is assumed that the shared memory segment already exists and that your process wants to attach to it. This is what needs to be done by your slave processes which want to access the shared memory segment, once it has been created by some master process.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
0

first, you lose some head file.

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

second, the first argument of shmget() function is a key_t type.So you must use fotk() function to generator key.

Example:
Myshared.h

#ifndef _MYSHARED_H
#define _MYSHARED_H

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

#define FLAG (IPC_CREATE | IPC_EXCL)
#define SIZE 512

typedef struct {
        char buf[SIZE];
        int read_from_buffer, write_to_buffer, read_bytes;
} Shared;

#endif

Myshared.c

#include "Myshared.h"
#include <stdio.h>

int main(int argc, char **argv)
{
        key_t key; 
        int shmid;

        key = ftok("/dev/null", 0);  # /dev/null just a example
        shmid = shmget(key, sizeof(struct Shared), FLAG);
        if (shmid == -1) {
                perror("Create Shared Memory Error:");
        }
        ......
}

the end, you can type man ftok and man shmget to help yourself.