Subject: Expected behavior from shmget()?
To: None <current-users@NetBSD.ORG>
From: Randy Terbush <randy@zyzzyva.com>
List: current-users
Date: 02/17/1996 10:02:00
Notice that in the following piece of code shmget() returns
a value incremented by (64 *1024) on each invocation. Is this expected?

#> ./shmem
Shmid: 589824
main:    0x182c
sbrk:    0x2234
mem:   0x10801000
stack: 0xf7bfdc6c

#> ./shmem
Shmid: 655360
main:    0x182c
sbrk:    0x2234
mem:   0x10801000
stack: 0xf7bfdc6c

#> ./shmem
Shmid: 720896
main:    0x182c
sbrk:    0x2234
mem:   0x10801000
stack: 0xf7bfdc6c

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>

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

int
main()
{
    int shmid;
    int size=256, rc;
    void *p, *b;

    b = sbrk(0);
#ifdef __NetBSD__   /* Defaults to Read/Write */
    shmid = shmget(IPC_PRIVATE, size, IPC_CREAT);
#else
    shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | SHM_R | SHM_W);
#endif
    if (shmid == -1)
    {
    perror("shmget");
    exit(1);
    }

    printf("Shmid: %d\n", shmid);

    p = shmat(shmid, NULL, 0);
    if (p == (void *)-1) perror("shmat");
    else
    {
    printf("main:  %8p\n", main);
    printf("sbrk:  %8p\n", b);
    printf("mem:   %8p\n", p);
    printf("stack: %8p\n", &shmid);
    rc = shmdt(p);
    if (rc == -1) perror("shmdt");
    }

    rc = shmctl(shmid, IPC_RMID, NULL);
    if (rc == -1)
    {
    rc = errno;
    perror("shmctl");
    if (rc == EINVAL)
        fprintf(stderr, "%d is not a valid shared memory identifier\n",
            shmid);
    exit(1);
    }

    return 0;
}