linux显示shm segment信息一例

#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main(void)
{
 int segment_id;
 char* shared_memory;
 const int size = 4096;
 unsigned short mode;
 struct shmid_ds shmbuffer;
 segment_id = shmget(IPC_PRIVATE, size, S_IRUSR|S_IWUSR);
 shared_memory = (char*)shmat(segment_id, NULL, 0);
 sprintf(shared_memory, "shared memory");
 printf("%s\n", shared_memory);
 if (shmctl(segment_id, IPC_STAT, &shmbuffer) == - 1)
 {
  fprintf(stderr, "Unable to access segment %d\n", segment_id);
  return -1;
 }
 printf("ID \t\t KEY \t MODE \t\t OWNER \t SIZE \t ATTTACHES \n");
 printf("-- \t\t --- \t ---- \t\t ----- \t ---- \t --------- \n");
 printf("%d \t %d \t",segment_id,shmbuffer.shm_perm.__key);
 mode = shmbuffer.shm_perm.mode;
 /** OWNER */
 if (mode & 0400)
  printf("r");
 else
  printf("-");
 if (mode & 0200)
  printf("w");
 else
  printf("-");
 if (mode & 0100)
  printf("a");
 else
  printf("-");
 /** GROUP */
 if (mode & 0040)
                printf("r");
        else
                printf("-");
        if (mode & 0020)
                printf("w");
        else
                printf("-");
        if (mode & 0010)
                printf("a");
        else
                printf("-");
 /** WORLD */
 if (mode & 0004)
                printf("r");
        else
                printf("-");
        if (mode & 0002)
                printf("w");
        else
                printf("-");
        if (mode & 0001)
                printf("a");
        else
                printf("-");
 
 printf("    \t%4d\t",shmbuffer.shm_perm.uid);
 printf(" %d\t",shmbuffer.shm_segsz);
 printf(" %d\t",shmbuffer.shm_nattch);
 printf("\n%d", mode);
 
 printf("\n");
 
 shmdt(shared_memory);
 shmctl(segment_id, IPC_RMID, NULL);
 fprintf(stdout, "\n%d", segment_id);
 return 0;
}

你可能感兴趣的:(linux显示shm segment信息一例)