Subject: [linux-audio-dev] shared memory problems
From: Paul Davis (pbd_AT_Op.Net)
Date: Sun Nov 11 2001 - 00:14:09 EET
>>shmat(2) claims to try to find an area in the unmapped range (1-1.5GB)
>>when not given an address to try.
>>
>>unfortunately, it doesn't do that. instead, it ends up using an
>
>it does, of course. i was being loose with my words. what it doesn't
>do is operate properly with the ld.so, it also doesn't do what the man
>page claims, which is start at 1.5GB and come down from there.
upon further investigation, even this appears to be wrong. i'd be
interested if anyone running a kernel above 2.4.4 or in the 2.2 series
could run this program on their machine and tell me what it does. on
my machine, shmat(2) will unattach one attached segment if you call it
twice with the same address. this seems like a kernel error to
me. compile with "cc -o overlap overlap.c" ....
--p
#include <stdio.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <errno.h>
int
main (int argc, char *argv[])
{
key_t key1 = random();
size_t size1 = 1048576;
char *addr1;
int id1;
key_t key2 = random();
size_t size2 = 1048576;
char *addr2;
int id2;
char *attempt = (char *) (1048576 * 1536 - (1048576 * 2));
char c;
if ((id1 = shmget (key1, size1, IPC_CREAT|0666)) < 0) {
fprintf (stderr, "cannot create new segment (%s)\n", strerror (errno));
goto cleanup;
}
if ((addr1 = shmat (id1, attempt, SHM_RND)) == (char *) -1) {
fprintf (stderr, "cannot attach new segment at %p (%s)", attempt, strerror (errno));
shmctl (id1, IPC_RMID, 0);
goto cleanup;
}
printf ("attached a segment at %p - check /proc/%d/maps then press any key\n", addr1, getpid());
read (0, &c, 1);
*((int *) addr1) = 0xfeedface;
if ((id2 = shmget (key2, size2, IPC_CREAT|0666)) < 0) {
fprintf (stderr, "cannot create new segment (%s)\n", strerror (errno));
goto cleanup;
}
printf ("in attached segment, initial data is %d\n", *((int *) addr1));
if ((addr2 = shmat (id2, attempt, SHM_RND)) == (char *) -1) {
fprintf (stderr, "cannot attach new segment at %p (%s)", attempt, strerror (errno));
goto cleanup;
}
printf ("post-attach, in attached segment, initial data is %d\n", *((int *) addr1));
printf ("attached a 2nd segment at %p - check /proc/%d/maps then press any key\n", addr2, getpid());
read (0, &c, 1);
cleanup:
shmctl (id1, IPC_RMID, 0);
shmctl (id2, IPC_RMID, 0);
shmdt (addr1);
shmdt (addr2);
exit (0);
}
This archive was generated by hypermail 2b28 : Sun Nov 11 2001 - 00:14:28 EET