os/ossrv/genericopenlibs/openenvcore/include/sys/shm.dosc
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /** @file ../include/sys/shm.h
     2 @internalComponent
     3 */
     4 
     5 /** @fn  shmget(key_t key, int size, int shmflg)
     6 @param key
     7 @param size
     8 @param 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.
    11 
    12 @code
    13  SHM_R Read access for user.
    14  SHM_W Write access for user.
    15  ( SHM_R>>3 )
    16   Read access for group.
    17  ( SHM_W>>3 )
    18   Write access for group.
    19  ( SHM_R>>6 )
    20   Read access for other.
    21  ( SHM_W>>6 )
    22   Write access for other.
    23 
    24 @endcode
    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
    28 IPC object.
    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.
    33 
    34 @code
    35 
    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.
    37 
    38 @endcode
    39 
    40  When creating a new shared memory segment, size indicates the desired size of the new segment in bytes.
    41 The size
    42 of the segment may be rounded up to a multiple convenient to the
    43 kernel (i.e., the page size).
    44 
    45 Examples:
    46 @code
    47 #include <sys/ipc.h>
    48 #include <sys/shm.h>
    49 #include <stdio.h>
    50 #include <string.h>
    51 #include <errno.h>
    52 
    53 #define SHM_SEG_SIZE 1024
    54 
    55 int main(void)
    56 {
    57     int shm_id;
    58     int perm;
    59     /*
    60      * Create a shared memory segment
    61      */
    62     perm = SHM_R | SHM_W;
    63     if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
    64                          IPC_CREAT | IPC_EXCL | perm))
    65          == -1) {
    66         printf("Shared memory create failed with errno %d", errno);
    67         return -1;
    68     }
    69     return 0;
    70 }
    71 
    72 @endcode
    73  
    74 
    75 @publishedAll
    76 @externallyDefinedApi
    77 */
    78 
    79 /** @fn  shmat(int shmid, const void *shmaddr, int shmflg)
    80 @param shmid
    81 @param shmaddr
    82 @param shmflg
    83 Note: This description also covers the following functions -
    84  shmdt() 
    85 
    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.
    89 
    90   The shmat system call
    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)
    95 
    96  The shmdt system call
    97 detaches the shared memory segment at the address specified by shmaddr from the calling process's address space.
    98 
    99 Examples:
   100 @code
   101 #include <sys/ipc.h>
   102 #include <sys/shm.h>
   103 #include <stdio.h>
   104 #include <string.h>
   105 #include <errno.h>
   106 
   107 #define SHM_SEG_SIZE 1024
   108 
   109 int main(void)
   110 {
   111     int shm_id;
   112     char *shm_addr;
   113     /*
   114      * Create a shared memory segment
   115      */
   116     if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
   117                          IPC_CREAT | IPC_EXCL | 0666))
   118          == -1) {
   119         printf("Shared memory create failed with errno %d", errno);
   120         return -1;
   121     }
   122     /*
   123      * Attach the shared memory segment to the
   124      * process address space
   125      */
   126     if((shm_addr = (char *)shmat(shm_id, NULL, 0)) == (void *)-1) {
   127        printf("Shared memory attach failed with errno %d", errno);
   128        return -1;
   129     }
   130     /*
   131      * Copy data to shared memory segment
   132      */
   133     strcpy(shm_addr, "some_random_data");
   134     /*
   135      * Detach the shared memory segment
   136      */
   137     if(shmdt(shm_addr) == -1) {
   138        printf("Shared memory detach failed with errno %d", errno);
   139     }
   140     /*
   141      * Remove the shared memory segment
   142      */
   143     if(shmctl(shm_id, IPC_RMID, NULL) == -1) {
   144        printf("Shared memory destroy failed with errno %d", errno);
   145     }
   146     return 0;
   147 }
   148 
   149 @endcode
   150  
   151 
   152 @publishedAll
   153 @externallyDefinedApi
   154 */
   155 
   156 /** @fn  shmdt(const void *shmaddr)
   157 @param shmaddr
   158 Refer to  shmat() for the documentation
   159 
   160 
   161  
   162 
   163 @publishedAll
   164 @externallyDefinedApi
   165 */
   166 
   167 
   168 /** @fn  shmctl(int shmid, int cmd, struct shmid_ds *buf)
   169 @param shmid
   170 @param cmd
   171 @param 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.
   174 
   175 @code
   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.
   184 
   185 @endcode
   186 
   187 Performs the action specified by cmd on the shared memory segment identified by shmid: 
   188   
   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.
   193 
   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.
   198 
   199  The shmid_ds
   200 structure is defined as follows: 
   201 @code
   202 struct shmid_ds {
   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 */
   212 };
   213 @endcode
   214 
   215 Examples:
   216 @code
   217 #include <sys/ipc.h>
   218 #include <sys/shm.h>
   219 #include <stdio.h>
   220 #include <string.h>
   221 #include <errno.h>
   222 
   223 #define SHM_SEG_SIZE 1024
   224 
   225 int main(void)
   226 {
   227     int shm_id;
   228     char *shm_addr;
   229     /*
   230      * Create a shared memory segment
   231      */
   232     if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
   233                          IPC_CREAT | IPC_EXCL | 0666))
   234          == -1) {
   235         printf("Shared memory create failed with errno %d", errno);
   236         return -1;
   237     }
   238     /*
   239      * Attach the shared memory segment to the
   240      * process address space
   241      */
   242     if((shm_addr = (char *)shmat(shm_id, NULL, 0)) == (void *)-1) {
   243        printf("Shared memory attach failed with errno %d", errno);
   244        return -1;
   245     }
   246     /*
   247      * Copy data to shared memory segment
   248      */
   249     strcpy(shm_addr, "some_random_data");
   250     /*
   251      * Detach the shared memory segment
   252      */
   253     if(shmdt(shm_addr) == -1) {
   254        printf("Shared memory detach failed with errno %d", errno);
   255     }
   256     /*
   257      * Remove the shared memory segment
   258      */
   259     if(shmctl(shm_id, IPC_RMID, NULL) == -1) {
   260        printf("Shared memory destroy failed with errno %d", errno);
   261     }
   262     return 0;
   263 }
   264 
   265 @endcode
   266  
   267 
   268 @publishedAll
   269 @externallyDefinedApi
   270 */
   271 
   272 
   273 /** @struct shmid_ds
   274 
   275 Defines a shared memory region
   276 
   277 @publishedAll
   278 @externallyDefinedApi
   279 */
   280 
   281 /** @var shmid_ds::shm_perm
   282 inode's device
   283 */
   284 
   285 /** @var shmid_ds::shm_segsz
   286 size of segment in bytes
   287 */
   288 
   289 /** @var shmid_ds::shm_lpid
   290 process ID of last shared memory op
   291 */
   292 
   293 /** @var shmid_ds::shm_cpid
   294 process ID of creator
   295 */
   296 
   297 /** @var shmid_ds::shm_nattch
   298 number of current attaches
   299 */
   300 
   301 /** @var shmid_ds::shm_atime
   302 time of last shmat()
   303 */
   304 
   305 /** @var shmid_ds::shm_dtime
   306 time of last shmdt()
   307 */
   308 
   309 /** @var shmid_ds::shm_ctime
   310 time of last change by shmctl()
   311 */
   312 
   313 /** @var shmid_ds::shm_internal
   314 sysv stupidity
   315 */
   316 
   317 /** @def SHM_RDONLY 
   318 
   319 Attach read-only (else read-write)
   320 
   321 @publishedAll
   322 @externallyDefinedApi
   323 */
   324 
   325 /** @def SHM_RND 
   326 
   327 Round attach address to SHMLBA
   328 
   329 @publishedAll
   330 @externallyDefinedApi
   331 */
   332 
   333 /** @def SHMLBA 
   334 
   335 Segment low boundary address multiple
   336 
   337 @publishedAll
   338 @externallyDefinedApi
   339 */
   340 
   341