systemV shareMemory limit

目录

    #include "unpipc.h"
    
    #define MAX_NIDS 4096
    
    int main(int argc, char **argv)
    /* $$.bp$$ */
    {
      int i, j, shmid[MAX_NIDS];
      void *addr[MAX_NIDS];
      unsigned long size;
      int maxShmget = 0;
    
      /* 4see how many identifiers we can "open" */
      for (i = 0; i < MAX_NIDS; i++) {
        shmid[i] = shmget(IPC_PRIVATE, 10, SVSHM_MODE | IPC_CREAT);
        if (shmid[i] == -1) {
          maxShmget = i;
          printf("%d identifiers open at once\n", i);
          break;
        }
      }
      for (j = 0; j < maxShmget; j++) {
        int ret = shmctl(shmid[j], IPC_RMID, NULL);
        (void)ret;
      }
      /* 4now see how many we can "attach" */
      for (i = 0; i < MAX_NIDS && i < maxShmget; i++) {
        shmid[i] = shmget(IPC_PRIVATE, 1024, SVSHM_MODE | IPC_CREAT);
        // printf("shmid num:index%d, id:%d\n", i, shmid[i]);
        addr[i] = shmat(shmid[i], NULL, 0);
        if (addr[i] == (void *)-1) {
          printf("%d shared memory segments attached at once\n", i);
          Shmctl(shmid[i], IPC_RMID, NULL); /* the one that failed */
          break;
        }
      }
    
      for (j = 0; j < i; j++) {
        Shmdt(addr[j]);
        Shmctl(shmid[j], IPC_RMID, NULL);
      }
    
      /* 4see how small a shared memory segment we can create */
      for (size = 1;; size++) {
        shmid[0] = shmget(IPC_PRIVATE, size, SVSHM_MODE | IPC_CREAT);
        if (shmid[0] != -1) { /* stop on first success */
          printf("minimum size of shared memory segment = %lu\n", size);
          Shmctl(shmid[0], IPC_RMID, NULL);
          break;
        }
      }
    
      /* 4see how large a shared memory segment we can create */
      for (size = 65536;; size += 4096) {
        shmid[0] = shmget(IPC_PRIVATE, size, SVSHM_MODE | IPC_CREAT);
        if (shmid[0] == -1) { /* stop on first failure */
          printf("maximum size of shared memory segment = %lu\n", size - 4096);
          break;
        }
        Shmctl(shmid[0], IPC_RMID, NULL);
      }
    
      exit(0);
    }
    
    

    posted on 2020-10-12 11:09  ims-  阅读(118)  评论(0编辑  收藏  举报

    导航