Re: shmat on non-NULL address?

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Thu Feb 17 2000 - 17:43:34 EST


On Thu, 17 Feb 2000, Mark K. Gardner wrote:

> According to the man page for shmat, supplying a non-NULL shmaddr
> (with or without asserting SHM_RND) should attach a shared memory
> segment to the address. However, the shmat call always fails unless
> supplied with a NULL shmaddr. I quickly perused the sys_shmat function
> in linux/ipc/shm.c (RedHat 6.1, kernel 2.2.5) and could not tell if
> non-NULL behavior is even implemented. Would someone who is more
> knowledgeable about shared memory than I tell me if it is possible to
> attach an existing address to shared memory? Thanks.
>
> Mark

Here is a snippet of what I do. Note that SHM_FAIL is not NULL, it's
(void *) -1. Maybe this is why you think it's failing. Just a guess.

#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>

#define FIRST_FLAGS (IPC_EXCL|IPC_CREAT|SHM_R|SHM_W)
#define NEXT_FLAGS (IPC_EXCL|SHM_R|SHM_W)
#define PROT (PROT_READ|PROT_WRITE)
#define FLAGS (MAP_FIXED|MAP_SHARED)

static const char Shmget[]="shmget";
static const char Shmat[]="shmat";
static const char Shmdt[]="shmdt";

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*
 * This gets one page of shared memory or attaches it if it already
 * exists.
 */
void init_shmem(PARS **pars)
{
   int i;
   if(*pars == NULL)
   {
      if((i = shmget(KEY, PAGE_SIZE, FIRST_FLAGS)) < 0)
          if((i = shmget(KEY, PAGE_SIZE, NEXT_FLAGS)) < 0)
              ERRORS(Shmget);
      if((*pars = (PARS *) shmat(i, NULL, 0)) == SHM_FAIL)
          ERRORS(Shmat);
   }
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*
 * This detaches a shared memory segment.
 */
void quit_shmem(PARS **pars)
{
    if(shmdt((const void *) *pars) < 0)
        ERRORS(Shmdt);
    *pars = NULL;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.3.41 on an i686 machine (800.63 BogoMips).

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed Feb 23 2000 - 21:00:19 EST