System V shared memory and Ultrix ?
Dan Ts'o
dan at rna.UUCP
Wed Sep 24 07:44:50 AEST 1986
In article <547 at rna.UUCP> dan at rna.UUCP (Dan Ts'o) writes:
>x
> Has anyone used the System V-style shared memory facility in
>Ultrix-32m (Microvax II), especially in double-buffered I/O operations ?
>For example, the continuous DMA'ing in of data while DMA'ing out to
>magtape. one process DMA's into shared memory and then signals the second
>process for it to DMA's to magtape while the first process fills the second
>buffer.
>
> Can someone mail me a simple C program in which to processes communicate
>using these System V shared memory calls ? Please educate a poor 4.XBSD user
>to the wonders of shared memory. Thanks.
Well, here I am answering my own question - sort of. I came up with a
simple shm*() program below which seems to work. However I'm still confused
about a number of the facilities of shm*(). For example, what role does "key"
play ? How do I pass the shared memory segment to non-related process ?
Perhaps what I should do (it doesn't seem to be documented) is stick a non
zero long in key and require that the communicating process shmget() with the
same key. Right ?
Is the shared memory segment automatically destroyed if no process has
it "open" ? What constitutes the "open" - the shmget() or the shmat() ? If it
isn't destroyed then probably the shmat() is the "open" and the shmget() is
a lookup/create.
Sorry if these questions are trivial but the documentation in Ultrix
seems scarce. I would still like to know if there are problems with using
these operations for the style of I/O mentioned above.
Cheers,
Dan Ts'o
Dept. Neurobiology
Rockefeller Univ.
1230 York Ave.
NY, NY 10021
212-570-7671
...cmcl2!rna!dan
rna!dan at cmcl2.arpa
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define NSHM 10
char *shmat();
main()
{
key_t key;
int size, shmflg;
int shmid;
int pid;
register char *p;
register int *v, i;
size = NSHM * (sizeof *v);
shmflg = IPC_CREAT + 0666;
key = IPC_PRIVATE;
shmid = shmget(key, size, shmflg);
if (shmid < 0) {
fprintf(stderr, "%d: Bad shmget(), ", shmid);
perror("");
exit(1);
}
pid = fork();
if (pid == -1) {
perror("Bad fork()");
exit(1);
}
shmflg = SHM_RND;
p = shmat(shmid, (char *)0, shmflg);
if (p == (char *)-1) {
perror("Bad shmat()");
exit(1);
}
v = (int *)p;
if (pid == 0) {
for (i = 0; i < NSHM; i++)
printf("%d ", v[i]);
printf("\n");
sleep(10);
for (i = 0; i < NSHM; i++)
printf("%d ", v[i]);
printf("\n");
exit(0);
}
sleep(3);
for (i = 0; i < NSHM; i++)
v[i] = i*i;
sleep(10);
exit(0);
}
More information about the Comp.unix.wizards
mailing list