2011-05-06 41 views
3

这个问题是问我在接受采访时:找到HP-UX上的进程使用的虚拟内存的C程序?

  • 编写一个简单的C程序来找到关于UNIX(HP-UX)

我告诉他们使用正在运行的进程的虚拟内存我不完全肯定,但也提出了类似的一些想法:

  • 可能是我们可以使用getpid系统调用获得进程ID和stat使用,并得到所需要的输出
  • 或者可能我们可以运行system函数调用,在里面我们可以使用shell命令如ps并获取详细信息。

也许我是不正确的;有人可以帮助我吗?

回答

0

使用getpid()不会帮助 - 它会告诉你当前进程的PID,这可能不是一个你有兴趣

使用stat()不会帮助 - 它会告诉你的尺寸文件。

你有一个主要选项选项 - 我不知道这是最合适的HP-UX。

  1. 使用/proc文件系统和信息从找到您所需要的数量。在我有权访问的HP-UX 11.23计算机上,没有/proc,所以很可能这与此无关。
  2. 通过popen()运行适当的ps命令并解析输出。该命令可能是ps -lp PID其中PID是(我希望显然)进程的PID你有兴趣

一些快速检查显示:

  • AIX,Solaris和Linux的有(三级分歧的实现)的/proc文件系统。
  • HP-UX和MacOS X(以及推断的其他BSD系统)没有/proc文件系统。
1

您可以使用HP/UX上的pstat_getprocvm函数来了解进程的虚拟内存布局。从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); 
采取

实施例