Update contrib.
1 /** @file ../include/sys/shm.h
5 /** @fn shmget(key_t key, int size, int shmflg)
9 @return Upon successful completion, shmget returns the positive integer identifier of a shared memory segment.
10 Otherwise, -1 is returned and errno set to indicate the error.
13 SHM_R Read access for user.
14 SHM_W Write access for user.
16 Read access for group.
18 Write access for group.
20 Read access for other.
22 Write access for other.
25 Based on the values of key and shmflg, shmget returns the identifier of a newly created or previously existing shared
26 memory segment. The key
27 is analogous to a filename: it provides a handle that names an
29 There are three ways to specify a key: IPC_PRIVATE may be specified, in which case a new IPC object
30 will be created. An integer constant may be specified.
31 If no IPC object corresponding
32 to key is specified and the IPC_CREAT bit is set in shmflg, a new one will be created. The ftok may be used to generate a key from a pathname.
36 The mode of a newly created IPC object is determined by OR 'ing the following constants into the shmflg argument: SHM_R Read access for user. SHM_W Write access for user. ( SHM_R>>3 ) Read access for group. ( SHM_W>>3 ) Write access for group. ( SHM_R>>6 ) Read access for other. ( SHM_W>>6 ) Write access for other.
40 When creating a new shared memory segment, size indicates the desired size of the new segment in bytes.
42 of the segment may be rounded up to a multiple convenient to the
43 kernel (i.e., the page size).
53 #define SHM_SEG_SIZE 1024
60 * Create a shared memory segment
63 if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
64 IPC_CREAT | IPC_EXCL | perm))
66 printf("Shared memory create failed with errno %d", errno);
79 /** @fn shmat(int shmid, const void *shmaddr, int shmflg)
83 Note: This description also covers the following functions -
86 @return Upon success, shmat returns the address where the segment is attached; otherwise, -1
87 is returned and errno is set to indicate the error. The shmdt function returns the value 0 if successful; otherwise the
88 value -1 is returned and errno is set to indicate the error.
91 attaches the shared memory segment identified by shmid to the calling process's address space.
92 The address where the segment
93 is attached is determined as follows: If shmaddr is 0, the segment is attached at an address selected by the
94 kernel. If shmaddr is nonzero and SHM_RND is not specified in shmflg, the segment is attached the specified address. (a nonzero addr is not supported) If addr is specified and SHM_RND is specified, addr is rounded down to the nearest multiple of SHMLBA.(a nonzero addr is not supported)
97 detaches the shared memory segment at the address specified by shmaddr from the calling process's address space.
107 #define SHM_SEG_SIZE 1024
114 * Create a shared memory segment
116 if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
117 IPC_CREAT | IPC_EXCL | 0666))
119 printf("Shared memory create failed with errno %d", errno);
123 * Attach the shared memory segment to the
124 * process address space
126 if((shm_addr = (char *)shmat(shm_id, NULL, 0)) == (void *)-1) {
127 printf("Shared memory attach failed with errno %d", errno);
131 * Copy data to shared memory segment
133 strcpy(shm_addr, "some_random_data");
135 * Detach the shared memory segment
137 if(shmdt(shm_addr) == -1) {
138 printf("Shared memory detach failed with errno %d", errno);
141 * Remove the shared memory segment
143 if(shmctl(shm_id, IPC_RMID, NULL) == -1) {
144 printf("Shared memory destroy failed with errno %d", errno);
153 @externallyDefinedApi
156 /** @fn shmdt(const void *shmaddr)
158 Refer to shmat() for the documentation
164 @externallyDefinedApi
168 /** @fn shmctl(int shmid, int cmd, struct shmid_ds *buf)
172 @return The shmctl function returns the value 0 if successful; otherwise the
173 value -1 is returned and errno is set to indicate the error.
176 IPC_STAT Fetch the segment's struct shmid_ds ,
177 storing it in the memory pointed to by buf.
178 IPC_SET Changes the shm_perm.uid, shm_perm.gid, and shm_perm.mode members of the segment's struct shmid_ds to match those of the struct pointed to by buf.
179 IPC_RMID Removes the segment from the system.
180 The removal will not take
181 effect until all processes having attached the segment have exited;
182 however, once the IPC_RMID operation has taken place, no further
183 processes will be allowed to attach the segment.
187 Performs the action specified by cmd on the shared memory segment identified by shmid:
189 IPC_STAT Fetch the segment's struct shmid_ds ,
190 storing it in the memory pointed to by buf.
191 IPC_SET Changes the shm_perm.uid, shm_perm.gid, and shm_perm.mode members of the segment's struct shmid_ds to match those of the struct pointed to by buf.
192 IPC_RMID Removes the segment from the system.
194 The removal will not take
195 effect until all processes having attached the segment have exited;
196 however, once the IPC_RMID operation has taken place, no further
197 processes will be allowed to attach the segment.
200 structure is defined as follows:
203 struct ipc_perm shm_perm; /* operation permission structure */
204 int shm_segsz; /* size of segment in bytes */
205 pid_t shm_lpid; /* process ID of last shared memory op */
206 pid_t shm_cpid; /* process ID of creator */
207 short shm_nattch; /* number of current attaches */
208 time_t shm_atime; /* time of last shmat() */
209 time_t shm_dtime; /* time of last shmdt() */
210 time_t shm_ctime; /* time of last change by shmctl() */
211 void *shm_internal; /* sysv stupidity */
223 #define SHM_SEG_SIZE 1024
230 * Create a shared memory segment
232 if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
233 IPC_CREAT | IPC_EXCL | 0666))
235 printf("Shared memory create failed with errno %d", errno);
239 * Attach the shared memory segment to the
240 * process address space
242 if((shm_addr = (char *)shmat(shm_id, NULL, 0)) == (void *)-1) {
243 printf("Shared memory attach failed with errno %d", errno);
247 * Copy data to shared memory segment
249 strcpy(shm_addr, "some_random_data");
251 * Detach the shared memory segment
253 if(shmdt(shm_addr) == -1) {
254 printf("Shared memory detach failed with errno %d", errno);
257 * Remove the shared memory segment
259 if(shmctl(shm_id, IPC_RMID, NULL) == -1) {
260 printf("Shared memory destroy failed with errno %d", errno);
269 @externallyDefinedApi
275 Defines a shared memory region
278 @externallyDefinedApi
281 /** @var shmid_ds::shm_perm
285 /** @var shmid_ds::shm_segsz
286 size of segment in bytes
289 /** @var shmid_ds::shm_lpid
290 process ID of last shared memory op
293 /** @var shmid_ds::shm_cpid
294 process ID of creator
297 /** @var shmid_ds::shm_nattch
298 number of current attaches
301 /** @var shmid_ds::shm_atime
305 /** @var shmid_ds::shm_dtime
309 /** @var shmid_ds::shm_ctime
310 time of last change by shmctl()
313 /** @var shmid_ds::shm_internal
319 Attach read-only (else read-write)
322 @externallyDefinedApi
327 Round attach address to SHMLBA
330 @externallyDefinedApi
335 Segment low boundary address multiple
338 @externallyDefinedApi