开发者

C program to find the virtual memory used by a process on HP-UX?

This question was asked to me in an interview:

  • Write a simple C program to find the virtual memory used by a running process on unix (HP-UX)

I told them that I am not exactly sure but also came up with some ideas like:

  • may be we can get开发者_Go百科 the process id using getpid system call and use it with stat and get the required output
  • or may be we can run the system function call and inside that we can use shell commands like ps and get the details.

Maybe I am not correct; can anybody help me with this?


You can use the pstat_getprocvm function on HP/UX to learn about the virtual memory layout of a process.

Example taken from here

#ifdef PS_RSESTACK      /* 11.22 and later */
#define LAST_VM_TYPE    PS_RSESTACK
#else               /* prior non-IPF */
#define LAST_VM_TYPE    PS_GRAPHICS_DMA
#endif              /* PS_RSESTACK */

uint32_t virt_totals[LAST_VM_TYPE + 1];
uint32_t phys_totals[LAST_VM_TYPE + 1];
uint32_t swap_totals[LAST_VM_TYPE + 1];
uint32_t mlock_totals[LAST_VM_TYPE + 1];

void print_type(int type)
{
    switch (type) {
    case PS_USER_AREA:
      printf(" UAREA ");
      return;
    case PS_TEXT:
      printf(" TEXT ");
      return;
    case PS_DATA:
      printf(" DATA/HEAP ");
      return;
    case PS_STACK:
      printf(" MAIN STACK ");
      return;
#ifdef PS_RSESTACK
      case PS_RSESTACK:
      printf(" RSE STACK ");
    return;
#endif              /* PS_RSESTACK */
    case PS_IO:
      printf(" MEM MAPPED I/O ");
    return;
      case PS_SHARED_MEMORY:
    printf(" SYSV SHMEM ");
    return;
    case PS_NULLDEREF:
    printf(" NULL DEREF ");
    return;
    case PS_MMF:
    printf(" MMAP ");
    return;
    case PS_GRAPHICS:
    case PS_GRAPHICS_DMA:
      printf(" GRAPHICS SPECIFIC ");
      return;
    default:
      printf(" UNUSED TYPE ");
    }
    return;
}

int main(int argc, char *argv[])
{
    int error;
    struct pst_vm_status pvs;
    struct pst_status ps;
    int i, j, k, verbose, get_all;
    pid_t target;
    int valid = 0;
    size_t sys_page_size;
    int done = 0;
    size_t count;
    _T_LONG_T last_pid = -1;

    verbose = 0;
    target = 0;
    get_all = 0;

    if (argc > 3) {
      printf("USAGE: %s <-v> \n", argv[0]);
    }

    if (argc == 2) {
      target = atoi(argv[1]);
    } else if (argc == 3) {
      verbose = 1;
      target = atoi(argv[2]);
    } else {
      get_all = 1;
    }

    sys_page_size = sysconf(_SC_PAGE_SIZE);

    j = 0;

    printf("VIRT/PHYS/LOCKED/SWAP summaries in pages.\n");
    printf("System page size is %ld or 0x%lx bytes.\n",
       sys_page_size, sys_page_size);

    do {
    if (get_all) {
        target = j++;
        count = (size_t) 1;
    } else {
        count = 0;
    }
   done = (pstat_getproc(&ps, sizeof(struct pst_status),
                  count, target) <= 0);
    if (done) {
        break;
    }

    if (ps.pst_pid == last_pid) {
        continue;
    }

    last_pid = ps.pst_pid;

    for (k = 0; k <= LAST_VM_TYPE; k++) {
        virt_totals[k] = 0;
        phys_totals[k] = 0;
        swap_totals[k] = 0;
        mlock_totals[k] = 0;
    }

    i = 0;
    while (pstat_getprocvm(&pvs, sizeof(struct pst_vm_status),
                   (size_t) ps.pst_pid, i++) > 0) {

        valid = 1;

        if (verbose) {
        printf("Object %d: ", i);
        print_type(pvs.pst_type);
        printf(" at VA 0x%lx to VA 0x%lx.\n\t",
               pvs.pst_vaddr,
               pvs.pst_vaddr +
               (pvs.pst_length * sys_page_size) - 1);
        printf("\tVIRT: %ld \tPHYS: %ld \tLOCKED:"
               " %ld\tSWAP: %ld \n",
               pvs.pst_length, pvs.pst_phys_pages,
               pvs.pst_lockmem, pvs.pst_swap);
        }
        virt_totals[pvs.pst_type] += pvs.pst_length;
        phys_totals[pvs.pst_type] += pvs.pst_phys_pages;
        swap_totals[pvs.pst_type] += pvs.pst_swap;
        mlock_totals[pvs.pst_type] += pvs.pst_lockmem;
    }

    if (valid) {
        printf("PID %ld:\n", ps.pst_pid);
    }
 for (k = 0; k <= LAST_VM_TYPE && valid; k++) {
        print_type(k);
        printf(" consumes %ld VIRT, %ld PHYS, %ld LOCKED"
           " and %ld SWAP.\n",
           virt_totals[k], phys_totals[k], mlock_totals[k],
           swap_totals[k]);
        virt_totals[k] = 0;
        phys_totals[k] = 0;
        mlock_totals[k] = 0;
        swap_totals[k] = 0;
    }
    valid = 0;
    } while (get_all);

    exit(0);


Using getpid() won't help - it tells you the PID of the current process, which is probably not the one you are interested in.

Using stat() won't help - it tells you the size of a file.

You have two one main options option - I'm not sure which is most appropriate for HP-UX.

  1. Use the /proc file system and information from that to find the number you need. On the HP-UX 11.23 machine I have access to, there was no /proc, so there is a good chance this is not relevant.
  2. Run an appropriate ps command via popen() and parse the output. The command might be ps -lp PID where PID is (I hope obviously) the PID of the process you are interested in.

Some quick checking shows:

  • AIX, Solaris, Linux have (three divergent implementations of) the /proc file system.
  • HP-UX and MacOS X (and, by inference, other BSD systems) do not have the /proc file system.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜