日期:2014-05-16  浏览次数:20664 次

共享内存的问题 请高手指教
extern   "C "   {
        #include   <sys/types.h>
        #include   <sys/ipc.h>
        #include   <sys/shm.h>
        #include   <sys/sem.h>
        #include   <stdio.h>
        #include   <stdlib.h>
        #include   <errno.h>
        #include   <string.h>
        #include   <getopt.h>
}

void   usage(int   argc,   char**   argv)
{
        printf( "%s   arguments\n ",   argv[0]);
        printf( "-h                           help   mode.\n ");
        printf( "-c                           create   shm.\n ");
        printf( "-m   <message>       write   message   into   shm.\n ");
        printf( "-r                           read   message   from   shm.\n ");
        printf( "-d                           destroy   shm.\n ");
}

typedef   enum   {SHM_UNKNOW   =   0,   SHM_CREATE,   SHM_DESTROY,   SHM_WRITE,   SHM_READ}   SHM_MODE;

#define   SHM_KEY   1234
#define   SHM_SIZE   1000

/**IPC_ALLOC   is   solaris   specific
  */
#if   !defined(   IPC_ALLOC   )
        #define   IPC_ALLOC   IPC_CREAT
#endif

/**   create   shm   with   SHM_KEY
  *   @return   shmid
  */
int   shm_create();

/**   open   shm   with   SHM_KEY
  *   @return   shmid
  */
int   shm_open();

/**   destroy   shm   which   shm   key   is   SHM_KEY
  */
void   shm_destroy(int   shmid);

/**   write   a   string   into   shm,   the   string   should   be   teminated   by   null.
  */
void   shm_write(int,   char*);

/**   read   message   from   shm
  @param   the   shmid
  */
void   shm_read(int   shmid);

/**   detach   from   shm
  */
void   shm_close(const   void*   root);

int   main(int   argc,   char**   argv)
{
        SHM_MODE   shm_mode   =   SHM_UNKNOW;
        char*   message   =   NULL;
        int   shmid   =   0;

        int   opt   =   0   ;
        extern   int   optind   ;
        extern   char*   optarg   ;