在接受采访时我问过这个问题:
我告诉他们我不太确定,但也提出了一些想法:
getpid
系统调用获取进程ID并将其与stat
一起使用并获取所需的输出system
函数调用,我们可以使用ps
之类的shell命令来获取详细信息。也许我不对;任何人都可以帮我这个吗?
答案 0 :(得分: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);
答案 1 :(得分:0)
使用getpid()
无济于事 - 它会告诉您当前进程的PID,这可能不是您感兴趣的进程。
使用stat()
无济于事 - 它会告诉您文件的大小。
你有两个一个主选项选项 - 我不确定哪个最适合HP-UX。
/proc
文件系统及其中的信息来查找所需的号码。在我有权访问的HP-UX 11.23计算机上,没有/proc
,因此很有可能这不相关。ps
运行适当的popen()
命令并解析输出。命令可能是ps -lp PID
,其中PID(我希望很明显)是您感兴趣的过程的PID。一些快速检查显示:
/proc
文件系统。