os/ossrv/genericopenlibs/openenvcore/include/sys/shm.dosc
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/** @file ../include/sys/shm.h
sl@0
     2
@internalComponent
sl@0
     3
*/
sl@0
     4
sl@0
     5
/** @fn  shmget(key_t key, int size, int shmflg)
sl@0
     6
@param key
sl@0
     7
@param size
sl@0
     8
@param shmflg
sl@0
     9
@return   Upon successful completion, shmget returns the positive integer identifier of a shared memory segment.
sl@0
    10
Otherwise, -1 is returned and errno set to indicate the error.
sl@0
    11
sl@0
    12
@code
sl@0
    13
 SHM_R Read access for user.
sl@0
    14
 SHM_W Write access for user.
sl@0
    15
 ( SHM_R>>3 )
sl@0
    16
  Read access for group.
sl@0
    17
 ( SHM_W>>3 )
sl@0
    18
  Write access for group.
sl@0
    19
 ( SHM_R>>6 )
sl@0
    20
  Read access for other.
sl@0
    21
 ( SHM_W>>6 )
sl@0
    22
  Write access for other.
sl@0
    23
sl@0
    24
@endcode
sl@0
    25
  Based on the values of key and shmflg, shmget returns the identifier of a newly created or previously existing shared
sl@0
    26
memory segment. The key
sl@0
    27
is analogous to a filename: it provides a handle that names an
sl@0
    28
IPC object.
sl@0
    29
There are three ways to specify a key: IPC_PRIVATE may be specified, in which case a new IPC object
sl@0
    30
will be created. An integer constant may be specified.
sl@0
    31
If no IPC object corresponding
sl@0
    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.
sl@0
    33
sl@0
    34
@code
sl@0
    35
sl@0
    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.
sl@0
    37
sl@0
    38
@endcode
sl@0
    39
sl@0
    40
 When creating a new shared memory segment, size indicates the desired size of the new segment in bytes.
sl@0
    41
The size
sl@0
    42
of the segment may be rounded up to a multiple convenient to the
sl@0
    43
kernel (i.e., the page size).
sl@0
    44
sl@0
    45
Examples:
sl@0
    46
@code
sl@0
    47
#include <sys/ipc.h>
sl@0
    48
#include <sys/shm.h>
sl@0
    49
#include <stdio.h>
sl@0
    50
#include <string.h>
sl@0
    51
#include <errno.h>
sl@0
    52
sl@0
    53
#define SHM_SEG_SIZE 1024
sl@0
    54
sl@0
    55
int main(void)
sl@0
    56
{
sl@0
    57
    int shm_id;
sl@0
    58
    int perm;
sl@0
    59
    /*
sl@0
    60
     * Create a shared memory segment
sl@0
    61
     */
sl@0
    62
    perm = SHM_R | SHM_W;
sl@0
    63
    if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
sl@0
    64
                         IPC_CREAT | IPC_EXCL | perm))
sl@0
    65
         == -1) {
sl@0
    66
        printf("Shared memory create failed with errno %d", errno);
sl@0
    67
        return -1;
sl@0
    68
    }
sl@0
    69
    return 0;
sl@0
    70
}
sl@0
    71
sl@0
    72
@endcode
sl@0
    73
 
sl@0
    74
sl@0
    75
@publishedAll
sl@0
    76
@externallyDefinedApi
sl@0
    77
*/
sl@0
    78
sl@0
    79
/** @fn  shmat(int shmid, const void *shmaddr, int shmflg)
sl@0
    80
@param shmid
sl@0
    81
@param shmaddr
sl@0
    82
@param shmflg
sl@0
    83
Note: This description also covers the following functions -
sl@0
    84
 shmdt() 
sl@0
    85
sl@0
    86
@return   Upon success, shmat returns the address where the segment is attached; otherwise, -1
sl@0
    87
is returned and errno is set to indicate the error. The shmdt function returns the value 0 if successful; otherwise the
sl@0
    88
value -1 is returned and errno is set to indicate the error.
sl@0
    89
sl@0
    90
  The shmat system call
sl@0
    91
attaches the shared memory segment identified by shmid to the calling process's address space.
sl@0
    92
The address where the segment
sl@0
    93
is attached is determined as follows: If shmaddr is 0, the segment is attached at an address selected by the
sl@0
    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)
sl@0
    95
sl@0
    96
 The shmdt system call
sl@0
    97
detaches the shared memory segment at the address specified by shmaddr from the calling process's address space.
sl@0
    98
sl@0
    99
Examples:
sl@0
   100
@code
sl@0
   101
#include <sys/ipc.h>
sl@0
   102
#include <sys/shm.h>
sl@0
   103
#include <stdio.h>
sl@0
   104
#include <string.h>
sl@0
   105
#include <errno.h>
sl@0
   106
sl@0
   107
#define SHM_SEG_SIZE 1024
sl@0
   108
sl@0
   109
int main(void)
sl@0
   110
{
sl@0
   111
    int shm_id;
sl@0
   112
    char *shm_addr;
sl@0
   113
    /*
sl@0
   114
     * Create a shared memory segment
sl@0
   115
     */
sl@0
   116
    if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
sl@0
   117
                         IPC_CREAT | IPC_EXCL | 0666))
sl@0
   118
         == -1) {
sl@0
   119
        printf("Shared memory create failed with errno %d", errno);
sl@0
   120
        return -1;
sl@0
   121
    }
sl@0
   122
    /*
sl@0
   123
     * Attach the shared memory segment to the
sl@0
   124
     * process address space
sl@0
   125
     */
sl@0
   126
    if((shm_addr = (char *)shmat(shm_id, NULL, 0)) == (void *)-1) {
sl@0
   127
       printf("Shared memory attach failed with errno %d", errno);
sl@0
   128
       return -1;
sl@0
   129
    }
sl@0
   130
    /*
sl@0
   131
     * Copy data to shared memory segment
sl@0
   132
     */
sl@0
   133
    strcpy(shm_addr, "some_random_data");
sl@0
   134
    /*
sl@0
   135
     * Detach the shared memory segment
sl@0
   136
     */
sl@0
   137
    if(shmdt(shm_addr) == -1) {
sl@0
   138
       printf("Shared memory detach failed with errno %d", errno);
sl@0
   139
    }
sl@0
   140
    /*
sl@0
   141
     * Remove the shared memory segment
sl@0
   142
     */
sl@0
   143
    if(shmctl(shm_id, IPC_RMID, NULL) == -1) {
sl@0
   144
       printf("Shared memory destroy failed with errno %d", errno);
sl@0
   145
    }
sl@0
   146
    return 0;
sl@0
   147
}
sl@0
   148
sl@0
   149
@endcode
sl@0
   150
 
sl@0
   151
sl@0
   152
@publishedAll
sl@0
   153
@externallyDefinedApi
sl@0
   154
*/
sl@0
   155
sl@0
   156
/** @fn  shmdt(const void *shmaddr)
sl@0
   157
@param shmaddr
sl@0
   158
Refer to  shmat() for the documentation
sl@0
   159
sl@0
   160
sl@0
   161
 
sl@0
   162
sl@0
   163
@publishedAll
sl@0
   164
@externallyDefinedApi
sl@0
   165
*/
sl@0
   166
sl@0
   167
sl@0
   168
/** @fn  shmctl(int shmid, int cmd, struct shmid_ds *buf)
sl@0
   169
@param shmid
sl@0
   170
@param cmd
sl@0
   171
@param buf
sl@0
   172
@return   The shmctl function returns the value 0 if successful; otherwise the
sl@0
   173
value -1 is returned and errno is set to indicate the error.
sl@0
   174
sl@0
   175
@code
sl@0
   176
 IPC_STAT Fetch the segment's struct shmid_ds ,
sl@0
   177
 storing it in the memory pointed to by buf.
sl@0
   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.
sl@0
   179
 IPC_RMID Removes the segment from the system.
sl@0
   180
 The removal will not take
sl@0
   181
 effect until all processes having attached the segment have exited;
sl@0
   182
 however, once the IPC_RMID operation has taken place, no further
sl@0
   183
 processes will be allowed to attach the segment.
sl@0
   184
sl@0
   185
@endcode
sl@0
   186
sl@0
   187
Performs the action specified by cmd on the shared memory segment identified by shmid: 
sl@0
   188
  
sl@0
   189
  IPC_STAT Fetch the segment's struct shmid_ds ,
sl@0
   190
storing it in the memory pointed to by buf. 
sl@0
   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. 
sl@0
   192
IPC_RMID Removes the segment from the system.
sl@0
   193
sl@0
   194
The removal will not take
sl@0
   195
effect until all processes having attached the segment have exited;
sl@0
   196
however, once the IPC_RMID operation has taken place, no further
sl@0
   197
processes will be allowed to attach the segment.
sl@0
   198
sl@0
   199
 The shmid_ds
sl@0
   200
structure is defined as follows: 
sl@0
   201
@code
sl@0
   202
struct shmid_ds {
sl@0
   203
    struct ipc_perm shm_perm;   /* operation permission structure */
sl@0
   204
    int             shm_segsz;  /* size of segment in bytes */
sl@0
   205
    pid_t           shm_lpid;   /* process ID of last shared memory op */
sl@0
   206
    pid_t           shm_cpid;   /* process ID of creator */
sl@0
   207
    short           shm_nattch; /* number of current attaches */
sl@0
   208
    time_t          shm_atime;  /* time of last shmat() */
sl@0
   209
    time_t          shm_dtime;  /* time of last shmdt() */
sl@0
   210
    time_t          shm_ctime;  /* time of last change by shmctl() */
sl@0
   211
    void           *shm_internal; /* sysv stupidity */
sl@0
   212
};
sl@0
   213
@endcode
sl@0
   214
sl@0
   215
Examples:
sl@0
   216
@code
sl@0
   217
#include <sys/ipc.h>
sl@0
   218
#include <sys/shm.h>
sl@0
   219
#include <stdio.h>
sl@0
   220
#include <string.h>
sl@0
   221
#include <errno.h>
sl@0
   222
sl@0
   223
#define SHM_SEG_SIZE 1024
sl@0
   224
sl@0
   225
int main(void)
sl@0
   226
{
sl@0
   227
    int shm_id;
sl@0
   228
    char *shm_addr;
sl@0
   229
    /*
sl@0
   230
     * Create a shared memory segment
sl@0
   231
     */
sl@0
   232
    if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
sl@0
   233
                         IPC_CREAT | IPC_EXCL | 0666))
sl@0
   234
         == -1) {
sl@0
   235
        printf("Shared memory create failed with errno %d", errno);
sl@0
   236
        return -1;
sl@0
   237
    }
sl@0
   238
    /*
sl@0
   239
     * Attach the shared memory segment to the
sl@0
   240
     * process address space
sl@0
   241
     */
sl@0
   242
    if((shm_addr = (char *)shmat(shm_id, NULL, 0)) == (void *)-1) {
sl@0
   243
       printf("Shared memory attach failed with errno %d", errno);
sl@0
   244
       return -1;
sl@0
   245
    }
sl@0
   246
    /*
sl@0
   247
     * Copy data to shared memory segment
sl@0
   248
     */
sl@0
   249
    strcpy(shm_addr, "some_random_data");
sl@0
   250
    /*
sl@0
   251
     * Detach the shared memory segment
sl@0
   252
     */
sl@0
   253
    if(shmdt(shm_addr) == -1) {
sl@0
   254
       printf("Shared memory detach failed with errno %d", errno);
sl@0
   255
    }
sl@0
   256
    /*
sl@0
   257
     * Remove the shared memory segment
sl@0
   258
     */
sl@0
   259
    if(shmctl(shm_id, IPC_RMID, NULL) == -1) {
sl@0
   260
       printf("Shared memory destroy failed with errno %d", errno);
sl@0
   261
    }
sl@0
   262
    return 0;
sl@0
   263
}
sl@0
   264
sl@0
   265
@endcode
sl@0
   266
 
sl@0
   267
sl@0
   268
@publishedAll
sl@0
   269
@externallyDefinedApi
sl@0
   270
*/
sl@0
   271
sl@0
   272
sl@0
   273
/** @struct shmid_ds
sl@0
   274
sl@0
   275
Defines a shared memory region
sl@0
   276
sl@0
   277
@publishedAll
sl@0
   278
@externallyDefinedApi
sl@0
   279
*/
sl@0
   280
sl@0
   281
/** @var shmid_ds::shm_perm
sl@0
   282
inode's device
sl@0
   283
*/
sl@0
   284
sl@0
   285
/** @var shmid_ds::shm_segsz
sl@0
   286
size of segment in bytes
sl@0
   287
*/
sl@0
   288
sl@0
   289
/** @var shmid_ds::shm_lpid
sl@0
   290
process ID of last shared memory op
sl@0
   291
*/
sl@0
   292
sl@0
   293
/** @var shmid_ds::shm_cpid
sl@0
   294
process ID of creator
sl@0
   295
*/
sl@0
   296
sl@0
   297
/** @var shmid_ds::shm_nattch
sl@0
   298
number of current attaches
sl@0
   299
*/
sl@0
   300
sl@0
   301
/** @var shmid_ds::shm_atime
sl@0
   302
time of last shmat()
sl@0
   303
*/
sl@0
   304
sl@0
   305
/** @var shmid_ds::shm_dtime
sl@0
   306
time of last shmdt()
sl@0
   307
*/
sl@0
   308
sl@0
   309
/** @var shmid_ds::shm_ctime
sl@0
   310
time of last change by shmctl()
sl@0
   311
*/
sl@0
   312
sl@0
   313
/** @var shmid_ds::shm_internal
sl@0
   314
sysv stupidity
sl@0
   315
*/
sl@0
   316
sl@0
   317
/** @def SHM_RDONLY 
sl@0
   318
sl@0
   319
Attach read-only (else read-write)
sl@0
   320
sl@0
   321
@publishedAll
sl@0
   322
@externallyDefinedApi
sl@0
   323
*/
sl@0
   324
sl@0
   325
/** @def SHM_RND 
sl@0
   326
sl@0
   327
Round attach address to SHMLBA
sl@0
   328
sl@0
   329
@publishedAll
sl@0
   330
@externallyDefinedApi
sl@0
   331
*/
sl@0
   332
sl@0
   333
/** @def SHMLBA 
sl@0
   334
sl@0
   335
Segment low boundary address multiple
sl@0
   336
sl@0
   337
@publishedAll
sl@0
   338
@externallyDefinedApi
sl@0
   339
*/
sl@0
   340
sl@0
   341