shared memory include workaround?

Andrew Veliath (veliaa@frontiernet.net)
Mon, 15 Dec 1997 02:43:03 -0500


Is there a posted workaround for using Linux 2.0 and glibc 2.0 with
SysV IPC? I've searched to no avail, sorry if it has come up before
(I would guess it has).

The shmid_ds structure defined in /usr/include/sys/shm_buf.h for glibc
2 is defined differently than the one in /usr/include/linux/shm.h.
Specifically, the shm_cpid and shm_lpid fields are of different sizes
in Linux 2.0 (not 2.2).

Unfortunately a given solution might break binary compatibility (which
is why I'm asking if there is some sort of agreed upon workaround), so
I'm not in a position to offer a solution, but at least on my own
system to keep something I'm working on chugging along I just changed
the glibc header file /usr/include/sys/shm_buf.h to size match for the
time being.

I see this not the case in Linux 2.2, but for RH5 it is important
since it uses the 2.0 kernel and glibc (as well as I guess a few other
distributions are going to soon). See the following test case under
RH5.

--
Regards,
Andrew Veliath
veliaa@frontiernet.net, veliaa@rpi.edu

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/stat.h>

int main(int argc, char **argv) { key_t k; int id; char *ptr; struct shmid_ds shmstat;

if ((k = ftok(argv[0], 0)) < 0) { perror("ftok"); exit(1); } if ((id = shmget(k, 1024, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)) < 0) { perror("shmget"); exit(1); }

if ((int)(ptr = shmat(id, 0, 0)) < 0) { perror("shmat"); exit(1); }

if (shmctl(id, IPC_STAT, &shmstat) < 0) { perror("shmctl 1"); exit(1); } printf("shm_nattch, should be 1 now: %d\n", shmstat.shm_nattch);

if (shmdt(ptr) < 0) { perror("shmdt"); exit(1); }

if (shmctl(id, IPC_STAT, &shmstat) < 0) { perror("shmctl 2"); exit(1); } printf("shm_nattch, should be 0 now: %d\n", shmstat.shm_nattch);

/* if (shmstat.shm_nattch == 0) <-- problem */ if (shmctl(id, IPC_RMID, NULL) < 0) { perror("shmctl 3"); exit(1); }

return 0; }