Chapter Contents

Previous

Next
shmget

shmget



Create or Find a Shared Memory Segment

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
USAGE NOTES
EXAMPLE 1
EXAMPLE 2
EXAMPLE 3
RELATED FUNCTIONS


SYNOPSIS

#include <sys/shm.h>
int shmget(key_t key, size_t size,
           int flags);


DESCRIPTION

The shmget function is used to create a new shared memory segment or to locate an existing one based on a key. Shared memory segments are memory areas which can be shared by several processes and which, once created, continue to exist until explicitly deleted using the shmctl function.

The key argument is an integral value which identifies the shared memory segment desired. A key value of IPC_PRIVATE requests a new shared memory segment without an associated key, and which can be accessed only by the segment id returned by shmget .

The size argument specifies the required size of the segment. If the segment already exists, size must be no greater than the size specified when the segment was created.

The flags argument specifies zero or more option flags specifying whether or not the segment already exists, and how access to the segment should be regulated. The argument should be specified as 0 for no flags, or as one or more of the following symbolic constants, combined using the or operator (|):

Additionally, any of the permission bits S_IRUSR , S_IWUSR , S_IRGRP , S_IWGRP , S_IROTH and S_IWOTH may be specified, to define what users are permitted to access or modify the memory segment. See the umask function description for more information about the meaning of these flags.


RETURN VALUE

shmget returns the identifier of the shared memory segment if successful, or -1 if unsuccessful.


USAGE NOTES

The shmget function can only be used with MVS 5.2.2 or a later release.

Note:   A site can impose limits on the size and number of shared memory segments created.  [cautionend]


EXAMPLE 1

Refer to shmat for an example that uses the functions shmat() , shmctl() , shmdt(), and shmget() to establish an IPC Client using a Shared Memory Segment.


EXAMPLE 2

Refer to shmat for an example that uses the functions shmat() , shmctl() , shmdt() , and shmget() to establish an IPC Server using a Shared Memory Segment.


EXAMPLE 3

This example is compiled using sascc370 -Krent -o . This program demonstrates the functions shmget() and shmstat() . It uses the shmget() function to create IPC shared memory segment and then uses the shmctl() function to retrieve statistics on the newly created memory segment.

/*-------------------------------------+
| POSIX/UNIX header files              |
+-------------------------------------*/
#include <sys/types.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/ipc.h>

#include <sys/shm.h>

/*-------------------------------------+
| ISO/ANSI header files                |
+-------------------------------------*/
#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <time.h>

#include <errno.h

/*-------------------------------------+
| Types                                |
+-------------------------------------*/
/* shared memory segment structure */
typedef struct shmid_ds ShMemSeg;  

/*--------------------------------------+
| Constants                             |
+--------------------------------------*/
/* shared memory key                   */
#define SHM_KEY      (key_t)1097    

/* size of memory segment (bytes)      */
#define SHM_SIZE     (size_t)256    

/* give everyone read/write             */
/* permission to shared memory          */
#define SHM_PERM  (S_IRUSR|S_IWUSR
 |S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)

/*--------------------------------------+
| Name:      main                       |
| Returns:   exit(EXIT_SUCCESS) or      |
|            exit(EXIT_FAILURE)         |
+--------------------------------------*/
int main()
{
/* shared memory segment id            */
    int shMemSegID;             

/* shared memory flags                 */
    int shmFlags;               

/* command to shared memory            */
    int shmCmd;                 

/* ptr to shared mem id structure      */
    ShMemSeg * shMemSeg;        

/*--------------------------------------*/
/* Create shared memory segment         */
/* Give everyone read/write permissions */
/*--------------------------------------*/
   shmFlags = IPC_CREAT | SHM_PERM;

if ( (shMemSegID = 
shmget(SHM_KEY, SHM_SIZE, shmFlags)) < 0 )
   {
       perror("SHMSTAT: shmget");
       exit(EXIT_FAILURE);
   }

/*-------------------------------------------*/
/* Allocate memory to store information      */
/* from shared memory seg                    */
/*-------------------------------------------*/
   shMemSeg = malloc(sizeof(ShMemSeg));

   if (shMemSeg == NULL)
   {
   fprintf(stderr,
   "ERROR: 
   Cannot allocate memory for 
   shared mem stats\n");
   exit(EXIT_FAILURE);
   }

/*---------------------------------------------*/
/* Call shmctl to retrieve information from    */
/* shared memory seg                           */
/*---------------------------------------------*/
   shmCmd = IPC_STAT;

   if (shmctl(shMemSegID, shmCmd, shMemSeg) < 0)
   {
   perror("SHMSTAT: shmctl failed to 
   retrieve shared mem stats");
   free(shMemSeg);
   exit(EXIT_FAILURE);
   }
 
/*---------------------------------------------*/
/* Print infomation retrieved from shared      */
/* memory segment                              */
/*---------------------------------------------*/
 printf("Shared Memory Segment statistics:\n\n");

 printf("\tSegment owner's effective user ID: 
   %d\n",(int)shMemSeg->shm_perm.uid);
 printf("\tSegment owner's effective group ID: 
   %d\n",(int)shMemSeg->shm_perm.gid);
 printf("\tSegment creator's effective user ID: 
   %d\n",(int)shMemSeg->shm_perm.cuid);
 printf("\tSegment creator's effective group ID: 
   %d\n",(int)shMemSeg->shm_perm.cgid);
 printf("\tSegment permission mode bits: 
   %#.3o\n\n",(int)shMemSeg->shm_perm.mode);

 printf("\tShared Memory Segment size: 
   %d\n",shMemSeg->shm_segsz);
 printf("\tProcess id of last Segment operation: 
   %d\n",(int)shMemSeg->shm_lpid);
 printf("\tProcess id of Segment creator: 
   %d\n",(int)shMemSeg->shm_cpid);
 printf("\tNumber of times Segment attached: 
   %u\n",shMemSeg->shm_nattch);
 printf("\tTime of last shmat call: 
   %s", ctime(&shMemSeg->shm_atime));
 printf("\tTime of last shmdt call: 
   %s", ctime(&shMemSeg->shm_dtime));
   printf("\tTime of last change: 
   %s", ctime(&shMemSeg->shm_ctime));

/*-------------------------------------------*/
/* Free memory used to store information     */
/* from shared memory seg                    */
/*-------------------------------------------*/
   free(shMemSeg);

/*---------------------------------------------*/
/* Call shmctl to remove shared memory segment */
/*---------------------------------------------*/
   shmCmd = IPC_RMID;

 if (shmctl(shMemSegID, shmCmd, NULL) < 0)
 {
    perror("SHMSTAT: 
    shmctl failed to remove shared mem");
    exit(EXIT_FAILURE);
   }
 
   exit(EXIT_SUCCESS);

}  /* end of main() */


RELATED FUNCTIONS

shmat , shmctl , shmdt


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.