Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!caip!pyrnj!esquire!cmcl2!rna!dan From: dan@rna.UUCP (Dan Ts'o) Newsgroups: net.unix-wizards Subject: Re: System V shared memory and Ultrix ? Message-ID: <548@rna.UUCP> Date: Tue, 23-Sep-86 17:44:50 EDT Article-I.D.: rna.548 Posted: Tue Sep 23 17:44:50 1986 Date-Received: Wed, 24-Sep-86 05:09:53 EDT References: <547@rna.UUCP> Reply-To: dan@rna.UUCP (Dan Ts'o) Organization: Rockefeller Neurobiology Lines: 95 In article <547@rna.UUCP> dan@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@cmcl2.arpa #include #include #include #include #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); }