10 /* return a pointer to a anonymous shared memory segment of size "size"
11 which will persist across fork() but will disappear when all processes
14 The memory is not zeroed
16 This function uses system5 shared memory. It takes advantage of a property
17 that the memory is not destroyed if it is attached when the id is removed
19 void *shm_setup(int size)
24 shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
26 printf("can't get shared memory\n");
29 ret = (void *)shmat(shmid, 0, 0);
30 if (!ret || ret == (void *)-1) {
31 printf("can't attach to shared memory\n");
34 /* the following releases the ipc, but note that this process
35 and all its children will still have access to the memory, its
36 just that the shmid is no longer valid for other shm calls. This
37 means we don't leave behind lots of shm segments after we exit
39 See Stevens "advanced programming in unix env" for details
41 shmctl(shmid, IPC_RMID, 0);
55 /* return a pointer to a /dev/zero shared memory segment of size "size"
56 which will persist across fork() but will disappear when all processes
59 The memory is zeroed automatically
61 This relies on /dev/zero being shared mmap capable, which it is
62 only under some OSes (Linux 2.1 _not_ included)
64 void *shm_setup(int size)
69 fd = open("/dev/zero", O_RDWR);
73 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE,
76 /* note that we don't need to keep the file open */
78 if (ret == (void *)-1) return NULL;
87 /* return a pointer to a /dev/zero shared memory segment of size "size"
88 which will persist across fork() but will disappear when all processes
91 The memory is zeroed automatically
93 This relies on anonyous shared mmap, which only some OSes can
94 do. Linux 2.1 is _not_ one of them.
96 void *shm_setup(int size)
99 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
101 if (ret == (void *)-1) return NULL;
108 #include <sys/mman.h>
111 /* return a pointer to a /dev/zero shared memory segment of size "size"
112 which will persist across fork() but will disappear when all processes
115 The memory is zeroed automatically
117 This relies only on shared mmap, which almost any mmap capable OS
118 can do. Linux can do this as of version 1.2 (I believe). certainly
119 all recent versions can do it.
121 void *shm_setup(int size)
125 char template[20] = "/tmp/shm.XXXXXX";
127 /* mkstemp() isn't really portable but use it if it exists as
128 otherwise you are open to some nasty soft-link /tmp based
130 fd = mkstemp(template);
134 lseek(fd, size, SEEK_SET);
135 write(fd, &zero, sizeof(zero));
136 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
140 if (ret == (void *)-1) return NULL;
146 int main(int argc, char *argv[])
153 printf("shm_sample <size>\n");
157 size = atoi(argv[1]);
159 buf = shm_setup(size);
162 printf("shm_setup(%d) failed\n", size);
165 memset(buf, 0, sizeof(int));
171 *(int *)buf = parent;
175 waitpid(child, 0, 0);
177 if (*(int *)buf == 0) {
178 printf("memory not shared?\n");
180 printf("pid via shm is %d real pid is %d\n",
181 *(int *)buf, parent);