gtpc2m6iC/C++ Language Support User's Guide

shmat-Attach Shared Memory

This function attaches the shared memory associated with the specified shared memory identifier to the address space of the calling process.

Format

#include  <sys/shm.h>
void     *shmat   int       shmid,
                const void *shmaddr,
                  int       shmflg);

shmid
The shared memory identifier associated with the shared memory to be attached. The shared memory identifier is returned by the shmget function.

shmaddr
Reserved for future IBM use. This must be set to NULL.

shmflg
Reserved for future IBM use. This must be set to 0.
TPF deviation from POSIX

This parameter is provided for compatibility with a UNIX environment. The TPF system does not use this parameter.

Normal Return

If successful, the shmat function returns the address of the shared memory and the shm_nattch field in the shmid_ds data structure is incremented by 1.

Error Return

If unsuccessful, the shmat function returns a value of -1 and sets errno to one of the following:

EACCES
Operation permission is denied to the calling process.

EINVAL
The value of the shmid parameter is not a valid shared memory identifier or the shmaddr parameter is not set to NULL.

EMFILE
The number of shared memory segments attached to the calling process would exceed the system-imposed limit.

Programming Considerations

The shmid_ds data structure is defined in the sys/shm.h header file.

Examples

The following example attaches shared memory to the address space of the calling process by using the shared memory identifier returned by the shmget function. The shared memory identifier is removed from the TPF system by using the shmctl function when it is no longer needed.

#include <sys/ipc.h>
#include <sys/shm.h>
 
void main(void)
{
    key_t i;
    int  shm;
    void *addr;
    struct shmid_ds buf;
 
    i  = ftok("/usr",3);
    shm = shmget(i,8000,IPC_CREAT+S_IRUSR+S_IWUSR);
    addr = shmat(shm,NULL,0);
 
    i = shmctl(shm,IPC_RMID,&buf);
 
  }

Related Information